blob: 81bcc8bc3c23ee4ceacd20681747ec88af4f2b2e [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michalis Spyroua4f378d2019-04-26 14:54:54 +01002 * Copyright (c) 2016-2019 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "arm_compute/core/NEON/kernels/NEFastCornersKernel.h"
25
26#include "arm_compute/core/Coordinates.h"
27#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/Validate.h"
30
31#include <algorithm>
32#include <arm_neon.h>
33#include <cstddef>
34#include <limits>
35
36using namespace arm_compute;
37
38NEFastCornersKernel::NEFastCornersKernel()
39 : INEKernel(), _input(nullptr), _output(nullptr), _threshold(0), _non_max_suppression(false)
40{
41}
42
43namespace
44{
45constexpr size_t PERMUTATIONS = 16;
46constexpr size_t PERM_SIZE = 16;
47
48inline uint8x8x2_t create_permutation_index(size_t k)
49{
50 ARM_COMPUTE_ERROR_ON(k >= PERMUTATIONS);
51
Michalis Spyroua4f378d2019-04-26 14:54:54 +010052 static const std::array<std::array<uint8_t, PERMUTATIONS>, PERM_SIZE> permutations_table{ { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 255, 255, 255, 255, 255, 255, 255 },
53 { 15, 0, 1, 2, 3, 4, 5, 6, 7, 255, 255, 255, 255, 255, 255, 255 },
54 { 14, 15, 0, 1, 2, 3, 4, 5, 6, 255, 255, 255, 255, 255, 255, 255 },
55 { 13, 14, 15, 0, 1, 2, 3, 4, 5, 255, 255, 255, 255, 255, 255, 255 },
56 { 12, 13, 14, 15, 0, 1, 2, 3, 4, 255, 255, 255, 255, 255, 255, 255 },
57 { 11, 12, 13, 14, 15, 0, 1, 2, 3, 255, 255, 255, 255, 255, 255, 255 },
58 { 10, 11, 12, 13, 14, 15, 0, 1, 2, 255, 255, 255, 255, 255, 255, 255 },
59 { 9, 10, 11, 12, 13, 14, 15, 0, 1, 255, 255, 255, 255, 255, 255, 255 },
60 { 8, 9, 10, 11, 12, 13, 14, 15, 0, 255, 255, 255, 255, 255, 255, 255 },
61 { 7, 8, 9, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255 },
62 { 6, 7, 8, 9, 10, 11, 12, 13, 14, 255, 255, 255, 255, 255, 255, 255 },
63 { 5, 6, 7, 8, 9, 10, 11, 12, 13, 255, 255, 255, 255, 255, 255, 255 },
64 { 4, 5, 6, 7, 8, 9, 10, 11, 12, 255, 255, 255, 255, 255, 255, 255 },
65 { 3, 4, 5, 6, 7, 8, 9, 10, 11, 255, 255, 255, 255, 255, 255, 255 },
66 { 2, 3, 4, 5, 6, 7, 8, 9, 10, 255, 255, 255, 255, 255, 255, 255 },
67 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, 255, 255, 255, 255, 255, 255 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010068
Michalis Spyroua4f378d2019-04-26 14:54:54 +010069 } };
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070
71 const uint8x8x2_t index =
72 {
73 {
Michalis Spyroua4f378d2019-04-26 14:54:54 +010074 vld1_u8(permutations_table[k].data()),
75 vld1_u8(permutations_table[k].data() + 8)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076 }
77 };
78
79 return index;
80}
81
82inline uint8x8x4_t create_circle_index_register()
83{
84 /*
85 This function creates the index registers to retrieve the 16 texels in the Bresenham circle of radius 3 with center in P.
86
87 . . F 0 1 . . .
88 . E . . . 2 . .
89 D . . . . . 3 .
90 C . . P . . 4 .
91 B . . . . . 5 .
92 . A . . . 6 . .
93 . . 9 8 7 . . .
94
95 Where . is an irrelevant texel value
96
97 We want to retrieve all texels [0,F]
98
99 The 4 registers in r will then be used to get these texels out of two tables in the function get_circle_texels()
100
101 The first table holds the top 4 rows of texels
102 . . F 0 1 . . .
103 . E . . . 2 . .
104 D . . . . . 3 .
105 C . . P . . 4 .
106
107 The second table the bottom 3 rows of texels
108 B . . . . . 5 .
109 . A . . . 6 . .
110 . . 9 8 7 . . .
111
112 */
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100113 static const std::array<uint8_t, 8> top_right =
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114 {
115 /* The register r.val[0] will be used to retrieve these texels:
116 . . . 0 1 . . .
117 . . . . . 2 . .
118 . . . . . . 3 .
119 . . . . . . 4 .
120 */
121 3 /* top table, first row, elem 4, value 0 in the diagram above */,
122 4 /* top table, first row, elem 5, value 1 in the diagram above */,
123 13 /* top table, second row, elem 6, value 2 in the diagram above */,
124 22 /* top table, third row, elem 7, value 3 in the diagram above*/,
125 30 /* top table, fourth row, elem 7, value 4 in the diagram above*/,
126 255,
127 255,
128 255
129 };
130
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100131 static const std::array<uint8_t, 8> bottom_right =
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100132 {
133 /* The register r.val[1] will be used to retrieve these texels:
134 . . . . . . 5 .
135 . . . . . 6 . .
136 . . . . 7 . . .
137 */
138 255,
139 255,
140 255,
141 255,
142 255,
143 6 /* low table, first row, elem 7, value 5 in the diagram above*/,
144 13 /* low table, second row, elem 6, value 6 in the diagram above*/,
145 20 /* low table, third row, elem 5, value 7 in the diagram above*/
146 };
147
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100148 static const std::array<uint8_t, 8> top_left =
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149 {
150 /* The register r.val[2] will be used to retrieve these texels:
151 . . F . . . . .
152 . E . . . . . .
153 D . . . . . . .
154 C . . . . . . .
155 */
156 255,
157 255,
158 255,
159 255,
160 24 /* top table, fourth row, elem 1, value C in the diagram above */,
161 16 /* top table, third row, elem 1, value D in the diagram above*/,
162 9 /* top table, second row, elem 2, value E in the diagram above*/,
163 2 /* top table, first row, elem 3, value F in the diagram above*/
164 };
165
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100166 static const std::array<uint8_t, 8> bottom_left =
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100167 {
168 /* The register r.val[3] will be used to retrieve these texels:
169 B . . . . . . .
170 . A . . . . . .
171 . . 9 8 . . . .
172 */
173 19 /* low table, third row, elem 4, value 8 in the diagram above */,
174 18 /* low table, third row, elem 3, value 9 in the diagram above */,
175 9 /* low table, second row, elem 2, value A in the diagram above */,
176 0 /* low table, first row, elem 1, value B in the diagram above */,
177 255,
178 255,
179 255,
180 255
181 };
182
183 const uint8x8x4_t reg =
184 {
185 {
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100186 vld1_u8(top_right.data()),
187 vld1_u8(bottom_right.data()),
188 vld1_u8(top_left.data()),
189 vld1_u8(bottom_left.data())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100190 }
191 };
192
193 return reg;
194}
195
196inline uint8x16_t get_circle_texels(const uint8x8x4_t &index, const uint8x8x4_t &tbl_hi, const uint8x8x3_t &tbl_lo)
197{
198 /*
199 This function loads the 16 texels in the Bresenham circle of radius 3 into the register 'texels'.
200 The parameter 'index' is an array of indices which was previously setup in setup_circle_index_register().
201 tbl_hi and tbl_lo are the two tables holding the texels in the window [(-3,-3),(+3,+3)] for a given texel P
202 */
203 return vcombine_u8(vtbx3_u8(vtbl4_u8(tbl_hi, index.val[0]), tbl_lo, index.val[1]),
204 vtbx3_u8(vtbl4_u8(tbl_hi, index.val[2]), tbl_lo, index.val[3]));
205}
206
207inline uint8x16_t get_permutation_texels(const uint8x8x2_t &permutation_index, const uint8x8x2_t &tbl_circle)
208{
209 /*
210 This function stores the 9 texels of a give permutation X in the neon register 'texels'
211
212 'tbl_circle' is a LUT with the texels 0 to F
213
214 . . F 0 1 . . .
215 . E . . . 2 . .
216 D . . . . . 3 .
217 C . . P . . 4 .
218 B . . . . . 5 .
219 . A . . . 6 . .
220 . . 9 8 7 . . .
221
222 'permutation_index' is one of the permutations below:
223
224 { 0, 1, 2, 3, 4, 5, 6, 7, 8},
225 { F, 0, 1, 2, 3, 4, 5, 6, 7},
226 { E, F, 0, 1, 2, 3, 4, 5, 6},
227 { D, E, F, 0, 1, 2, 3, 4, 5},
228 { C, D, E, F, 0, 1, 2, 3, 4},
229 { B, C, D, E, F, 0, 1, 2, 3},
230 { A, B, C, D, E, F, 0, 1, 2},
231 { 9, A, B, C, D, E, F, 0, 1},
232 { 8, 9, A, B, C, D, E, F, 0},
233 { 7, 8, 9, A, B, C, D, E, F},
234 { 6, 7, 8, 9, A, B, C, D, E},
235 { 5, 6, 7, 8, 9, A, B, C, D},
236 { 4, 5, 6, 7, 8, 9, A, B, C},
237 { 3, 4, 5, 6, 7, 8, 9, A, B},
238 { 2, 3, 4, 5, 6, 7, 8, 9, A},
239 { 1, 2, 3, 4, 5, 6, 7, 8, 9},
240 */
241 static const uint8x8_t perm_right = vdup_n_u8(255); // init to 255 so that vtbx preserves the original values of the lanes
242
243 return vcombine_u8(vtbl2_u8(tbl_circle, permutation_index.val[0]),
244 vtbx2_u8(perm_right, tbl_circle, permutation_index.val[1]));
245}
246
247inline bool is_permutation_brighter(const uint8x16_t &permutation, const uint8x16_t &pg)
248{
249 const uint8x16_t res_gt = vcgtq_u8(permutation, pg);
250
251 return vget_lane_u64(vreinterpret_u64_u8(vand_u8(vget_high_u8(res_gt), vget_low_u8(res_gt))), 0) == std::numeric_limits<uint64_t>::max();
252}
253
254inline bool is_permutation_darker(const uint8x16_t &permutation, const uint8x16_t &pl)
255{
256 const uint8x16_t res_lt = vcltq_u8(permutation, pl);
257 const uint64x2_t u64res_lt = vreinterpretq_u64_u8(res_lt);
258 const uint64_t t3 = vgetq_lane_u64(u64res_lt, 0);
259 const uint64_t t4 = vgetq_lane_u64(u64res_lt, 1);
260
261 return std::numeric_limits<uint64_t>::max() == t3 && 255 == t4;
262}
263
264inline bool is_permutation_corner(const uint8x16_t &permutation, const uint8x16_t &pg, const uint8x16_t &pl)
265{
266 return is_permutation_brighter(permutation, pg) || is_permutation_darker(permutation, pl);
267}
268
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100269inline bool point_is_fast_corner(uint8_t p, uint8_t threshold, const uint8x8x2_t &tbl_circle_texels, std::array<uint8x8x2_t, PERMUTATIONS> &perm_indices)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100270{
271 /*
272 This function determines whether the point 'p' is a corner.
273 */
274 uint8x16_t pg = vqaddq_u8(vdupq_n_u8(p), vdupq_n_u8(threshold));
275 uint8x16_t pl = vqsubq_u8(vdupq_n_u8(p), vdupq_n_u8(threshold));
276
277 bool corner_detected = false;
278
279 for(size_t j = 0; !corner_detected && j < PERMUTATIONS; ++j)
280 {
281 const uint8x16_t pe_texels = get_permutation_texels(perm_indices[j], tbl_circle_texels);
282 corner_detected = is_permutation_corner(pe_texels, pg, pl);
283 }
284
285 return corner_detected;
286}
287
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100288inline uint8x8x2_t create_circle_tbl(const std::array<uint8_t *const __restrict, 7> &buffer, size_t in_offset, const uint8x8x4_t &circle_index_r)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100289{
290 /*
291 This function builds a LUT holding the 16 texels in the Brensenham circle radius 3.
292 circle_index_r is a vector of 4 registers to retrieve the texels from the two tables mentioned above.
293 */
294
295 //Load the texels in the window [(x-3,y-3),(x+3,y+3)].
296 //The top 4 rows are loaded in tbl_hi and the low 3 rows in tbl_lo.
297 //These two tables are then used to retrieve the texels in the Bresenham circle of radius 3.
298 const uint8x8x4_t tbl_window_hi =
299 {
300 {
301 vld1_u8(buffer[0] + in_offset),
302 vld1_u8(buffer[1] + in_offset),
303 vld1_u8(buffer[2] + in_offset),
304 vld1_u8(buffer[3] + in_offset)
305 }
306 };
307
308 const uint8x8x3_t tbl_window_lo =
309 {
310 {
311 vld1_u8(buffer[4] + in_offset),
312 vld1_u8(buffer[5] + in_offset),
313 vld1_u8(buffer[6] + in_offset)
314 }
315 };
316
317 const uint8x16_t circle_texels = get_circle_texels(circle_index_r, tbl_window_hi, tbl_window_lo);
318
319 const uint8x8x2_t tbl_circle_texels =
320 {
321 {
322 vget_low_u8(circle_texels),
323 vget_high_u8(circle_texels)
324 }
325 };
326
327 return tbl_circle_texels;
328}
329
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100330inline uint8_t get_point_score(uint8_t p, uint8_t tolerance, const uint8x8x2_t &tbl_circle, std::array<uint8x8x2_t, PERMUTATIONS> &perm_indices)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100331{
332 uint8_t b = 255;
333 uint8_t a = tolerance;
334
335 while(b - a > 1)
336 {
337 const uint16_t ab = a + b;
338 const uint8_t c = ab >> 1;
339
340 if(point_is_fast_corner(p, c, tbl_circle, perm_indices))
341 {
342 a = c;
343 }
344 else
345 {
346 b = c;
347 }
348 }
349
350 return a;
351}
352} // namespace
353
354BorderSize NEFastCornersKernel::border_size() const
355{
356 return BorderSize(3);
357}
358
359void NEFastCornersKernel::configure(const IImage *input, IImage *output, uint8_t threshold, bool non_max_suppression, bool border_undefined)
360{
361 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input);
362 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(output);
363 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
364 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
365 ARM_COMPUTE_ERROR_ON_MSG(border_undefined == false, "Not implemented");
366
367 _input = input;
368 _output = output;
369 _threshold = threshold;
370 _non_max_suppression = non_max_suppression;
371
372 constexpr unsigned int num_elems_processed_per_iteration = 1;
373 constexpr unsigned int num_elems_read_per_iteration = 8;
374 constexpr unsigned int num_elems_written_per_iteration = 1;
375 constexpr unsigned int num_rows_read_per_iteration = 7;
376
377 // Configure kernel window
378 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
379 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
380 AccessWindowRectangle input_access(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration);
381
382 update_window_and_padding(win, input_access, output_access);
383
384 output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
385
386 INEKernel::configure(win);
387}
388
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100389void NEFastCornersKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100390{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100391 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100392 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
393 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
394
395 std::array<uint8x8x2_t, PERMUTATIONS> perm_index{ {} };
396 /*
397 We use a LUT loaded with 7 rows of uint8_t from the input image [-3,-3]...[+3,+3] to retrieve the texels in the Brensenham circle radius 3 and put them in one neon register uint8x16_t.
398 The three lines below setup the neon index registers to get these texels out from the table
399 */
400 const uint8x8x4_t circle_index_r = create_circle_index_register();
401 /*
402 We put the 16 texels (circle) in a LUT to easily generate all the permutations. The for block below setups the indices for each permutation.
403 */
404 for(size_t k = 0; k < PERMUTATIONS; ++k)
405 {
406 perm_index[k] = create_permutation_index(k);
407 }
408
409 Iterator in(_input, window);
410 Iterator out(_output, window);
411
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100412 const std::array<uint8_t *const __restrict, 7> in_row
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100413 {
414 _input->ptr_to_element(Coordinates(-3, -3)),
415 _input->ptr_to_element(Coordinates(-3, -2)),
416 _input->ptr_to_element(Coordinates(-3, -1)),
417 _input->ptr_to_element(Coordinates(-3, 0)),
418 _input->ptr_to_element(Coordinates(-3, 1)),
419 _input->ptr_to_element(Coordinates(-3, 2)),
420 _input->ptr_to_element(Coordinates(-3, 3))
421 };
422
423 auto is_rejected = [](uint8_t p, uint8_t q, uint8_t a, uint8_t b)
424 {
425 const bool p_is_in_ab = (a <= p) && (p <= b);
426 const bool q_is_in_ab = (a <= q) && (q <= b);
427 return p_is_in_ab && q_is_in_ab;
428 };
429
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100430 execute_window_loop(window, [&](const Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100431 {
432 const size_t in_offset = in.offset();
433 const uint8_t p0 = *in.ptr();
434 const uint8_t b = std::min(p0 + _threshold, 255);
435 const uint8_t a = std::max(p0 - _threshold, 0);
436 uint8_t score = 0;
437 /*
438 Fast check to discard points which cannot be corners and avoid the expensive computation of the potential 16 permutations
439
440 pixels 1 and 9 are examined, if both I1 and I9 are within [Ip - t, Ip + t], then candidate p is not a corner.
441 */
442 const uint8_t p1 = (in_offset + in_row[0])[3];
443 const uint8_t p9 = (in_offset + in_row[6])[3];
444
445 if(!is_rejected(p1, p9, a, b))
446 {
447 /* pixels 5 and 13 are further examined to check whether three of them are brighter than Ip + t or darker than Ip - t */
448 const uint8_t p5 = (in_offset + in_row[3])[6];
449 const uint8_t p13 = (in_offset + in_row[3])[0];
450
451 if(!is_rejected(p5, p13, a, b))
452 {
453 /* at this stage we use the full test with the 16 permutations to classify the point as corner or not */
454 const uint8x8x2_t tbl_circle_texel = create_circle_tbl(in_row, in_offset, circle_index_r);
455
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100456 if(point_is_fast_corner(p0, _threshold, tbl_circle_texel, perm_index))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100457 {
458 if(_non_max_suppression)
459 {
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100460 score = get_point_score(p0, _threshold, tbl_circle_texel, perm_index);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100461 }
462 else
463 {
464 score = 1;
465 }
466 }
467 }
468 }
469
470 *out.ptr() = score;
471 },
472 in, out);
473}