2 * Copyright (c) 2016, 2017 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
24 #include "arm_compute/core/NEON/kernels/NEFastCornersKernel.h"
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"
36 using namespace arm_compute;
38 NEFastCornersKernel::NEFastCornersKernel()
39 : INEKernel(), _input(nullptr), _output(nullptr), _threshold(0), _non_max_suppression(false)
45 constexpr size_t PERMUTATIONS = 16;
46 constexpr size_t PERM_SIZE = 16;
48 inline uint8x8x2_t create_permutation_index(size_t k)
50 ARM_COMPUTE_ERROR_ON(k >= PERMUTATIONS);
52 static const uint8_t permutations_table[PERMUTATIONS][PERM_SIZE]
54 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 255, 255, 255, 255, 255, 255, 255 },
55 { 15, 0, 1, 2, 3, 4, 5, 6, 7, 255, 255, 255, 255, 255, 255, 255 },
56 { 14, 15, 0, 1, 2, 3, 4, 5, 6, 255, 255, 255, 255, 255, 255, 255 },
57 { 13, 14, 15, 0, 1, 2, 3, 4, 5, 255, 255, 255, 255, 255, 255, 255 },
58 { 12, 13, 14, 15, 0, 1, 2, 3, 4, 255, 255, 255, 255, 255, 255, 255 },
59 { 11, 12, 13, 14, 15, 0, 1, 2, 3, 255, 255, 255, 255, 255, 255, 255 },
60 { 10, 11, 12, 13, 14, 15, 0, 1, 2, 255, 255, 255, 255, 255, 255, 255 },
61 { 9, 10, 11, 12, 13, 14, 15, 0, 1, 255, 255, 255, 255, 255, 255, 255 },
62 { 8, 9, 10, 11, 12, 13, 14, 15, 0, 255, 255, 255, 255, 255, 255, 255 },
63 { 7, 8, 9, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255 },
64 { 6, 7, 8, 9, 10, 11, 12, 13, 14, 255, 255, 255, 255, 255, 255, 255 },
65 { 5, 6, 7, 8, 9, 10, 11, 12, 13, 255, 255, 255, 255, 255, 255, 255 },
66 { 4, 5, 6, 7, 8, 9, 10, 11, 12, 255, 255, 255, 255, 255, 255, 255 },
67 { 3, 4, 5, 6, 7, 8, 9, 10, 11, 255, 255, 255, 255, 255, 255, 255 },
68 { 2, 3, 4, 5, 6, 7, 8, 9, 10, 255, 255, 255, 255, 255, 255, 255 },
69 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, 255, 255, 255, 255, 255, 255 }
73 const uint8x8x2_t index =
76 vld1_u8(permutations_table[k]),
77 vld1_u8(permutations_table[k] + 8)
84 inline uint8x8x4_t create_circle_index_register()
87 This function creates the index registers to retrieve the 16 texels in the Bresenham circle of radius 3 with center in P.
97 Where . is an irrelevant texel value
99 We want to retrieve all texels [0,F]
101 The 4 registers in r will then be used to get these texels out of two tables in the function get_circle_texels()
103 The first table holds the top 4 rows of texels
109 The second table the bottom 3 rows of texels
115 static const uint8_t top_right[8] =
117 /* The register r.val[0] will be used to retrieve these texels:
123 3 /* top table, first row, elem 4, value 0 in the diagram above */,
124 4 /* top table, first row, elem 5, value 1 in the diagram above */,
125 13 /* top table, second row, elem 6, value 2 in the diagram above */,
126 22 /* top table, third row, elem 7, value 3 in the diagram above*/,
127 30 /* top table, fourth row, elem 7, value 4 in the diagram above*/,
133 static const uint8_t bottom_right[8] =
135 /* The register r.val[1] will be used to retrieve these texels:
145 6 /* low table, first row, elem 7, value 5 in the diagram above*/,
146 13 /* low table, second row, elem 6, value 6 in the diagram above*/,
147 20 /* low table, third row, elem 5, value 7 in the diagram above*/
150 static const uint8_t top_left[8] =
152 /* The register r.val[2] will be used to retrieve these texels:
162 24 /* top table, fourth row, elem 1, value C in the diagram above */,
163 16 /* top table, third row, elem 1, value D in the diagram above*/,
164 9 /* top table, second row, elem 2, value E in the diagram above*/,
165 2 /* top table, first row, elem 3, value F in the diagram above*/
168 static const uint8_t bottom_left[8] =
170 /* The register r.val[3] will be used to retrieve these texels:
175 19 /* low table, third row, elem 4, value 8 in the diagram above */,
176 18 /* low table, third row, elem 3, value 9 in the diagram above */,
177 9 /* low table, second row, elem 2, value A in the diagram above */,
178 0 /* low table, first row, elem 1, value B in the diagram above */,
185 const uint8x8x4_t reg =
189 vld1_u8(bottom_right),
198 inline uint8x16_t get_circle_texels(const uint8x8x4_t &index, const uint8x8x4_t &tbl_hi, const uint8x8x3_t &tbl_lo)
201 This function loads the 16 texels in the Bresenham circle of radius 3 into the register 'texels'.
202 The parameter 'index' is an array of indices which was previously setup in setup_circle_index_register().
203 tbl_hi and tbl_lo are the two tables holding the texels in the window [(-3,-3),(+3,+3)] for a given texel P
205 return vcombine_u8(vtbx3_u8(vtbl4_u8(tbl_hi, index.val[0]), tbl_lo, index.val[1]),
206 vtbx3_u8(vtbl4_u8(tbl_hi, index.val[2]), tbl_lo, index.val[3]));
209 inline uint8x16_t get_permutation_texels(const uint8x8x2_t &permutation_index, const uint8x8x2_t &tbl_circle)
212 This function stores the 9 texels of a give permutation X in the neon register 'texels'
214 'tbl_circle' is a LUT with the texels 0 to F
224 'permutation_index' is one of the permutations below:
226 { 0, 1, 2, 3, 4, 5, 6, 7, 8},
227 { F, 0, 1, 2, 3, 4, 5, 6, 7},
228 { E, F, 0, 1, 2, 3, 4, 5, 6},
229 { D, E, F, 0, 1, 2, 3, 4, 5},
230 { C, D, E, F, 0, 1, 2, 3, 4},
231 { B, C, D, E, F, 0, 1, 2, 3},
232 { A, B, C, D, E, F, 0, 1, 2},
233 { 9, A, B, C, D, E, F, 0, 1},
234 { 8, 9, A, B, C, D, E, F, 0},
235 { 7, 8, 9, A, B, C, D, E, F},
236 { 6, 7, 8, 9, A, B, C, D, E},
237 { 5, 6, 7, 8, 9, A, B, C, D},
238 { 4, 5, 6, 7, 8, 9, A, B, C},
239 { 3, 4, 5, 6, 7, 8, 9, A, B},
240 { 2, 3, 4, 5, 6, 7, 8, 9, A},
241 { 1, 2, 3, 4, 5, 6, 7, 8, 9},
243 static const uint8x8_t perm_right = vdup_n_u8(255); // init to 255 so that vtbx preserves the original values of the lanes
245 return vcombine_u8(vtbl2_u8(tbl_circle, permutation_index.val[0]),
246 vtbx2_u8(perm_right, tbl_circle, permutation_index.val[1]));
249 inline bool is_permutation_brighter(const uint8x16_t &permutation, const uint8x16_t &pg)
251 const uint8x16_t res_gt = vcgtq_u8(permutation, pg);
253 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();
256 inline bool is_permutation_darker(const uint8x16_t &permutation, const uint8x16_t &pl)
258 const uint8x16_t res_lt = vcltq_u8(permutation, pl);
259 const uint64x2_t u64res_lt = vreinterpretq_u64_u8(res_lt);
260 const uint64_t t3 = vgetq_lane_u64(u64res_lt, 0);
261 const uint64_t t4 = vgetq_lane_u64(u64res_lt, 1);
263 return std::numeric_limits<uint64_t>::max() == t3 && 255 == t4;
266 inline bool is_permutation_corner(const uint8x16_t &permutation, const uint8x16_t &pg, const uint8x16_t &pl)
268 return is_permutation_brighter(permutation, pg) || is_permutation_darker(permutation, pl);
271 inline bool point_is_fast_corner(uint8_t p, uint8_t threshold, const uint8x8x2_t &tbl_circle_texels, uint8x8x2_t perm_indices[PERMUTATIONS])
274 This function determines whether the point 'p' is a corner.
276 uint8x16_t pg = vqaddq_u8(vdupq_n_u8(p), vdupq_n_u8(threshold));
277 uint8x16_t pl = vqsubq_u8(vdupq_n_u8(p), vdupq_n_u8(threshold));
279 bool corner_detected = false;
281 for(size_t j = 0; !corner_detected && j < PERMUTATIONS; ++j)
283 const uint8x16_t pe_texels = get_permutation_texels(perm_indices[j], tbl_circle_texels);
284 corner_detected = is_permutation_corner(pe_texels, pg, pl);
287 return corner_detected;
290 inline uint8x8x2_t create_circle_tbl(const uint8_t *const __restrict buffer[7], size_t in_offset, const uint8x8x4_t &circle_index_r)
293 This function builds a LUT holding the 16 texels in the Brensenham circle radius 3.
294 circle_index_r is a vector of 4 registers to retrieve the texels from the two tables mentioned above.
297 //Load the texels in the window [(x-3,y-3),(x+3,y+3)].
298 //The top 4 rows are loaded in tbl_hi and the low 3 rows in tbl_lo.
299 //These two tables are then used to retrieve the texels in the Bresenham circle of radius 3.
300 const uint8x8x4_t tbl_window_hi =
303 vld1_u8(buffer[0] + in_offset),
304 vld1_u8(buffer[1] + in_offset),
305 vld1_u8(buffer[2] + in_offset),
306 vld1_u8(buffer[3] + in_offset)
310 const uint8x8x3_t tbl_window_lo =
313 vld1_u8(buffer[4] + in_offset),
314 vld1_u8(buffer[5] + in_offset),
315 vld1_u8(buffer[6] + in_offset)
319 const uint8x16_t circle_texels = get_circle_texels(circle_index_r, tbl_window_hi, tbl_window_lo);
321 const uint8x8x2_t tbl_circle_texels =
324 vget_low_u8(circle_texels),
325 vget_high_u8(circle_texels)
329 return tbl_circle_texels;
332 inline uint8_t get_point_score(uint8_t p, uint8_t tolerance, const uint8x8x2_t &tbl_circle, uint8x8x2_t perm_indices[PERMUTATIONS])
335 uint8_t a = tolerance;
339 const uint16_t ab = a + b;
340 const uint8_t c = ab >> 1;
342 if(point_is_fast_corner(p, c, tbl_circle, perm_indices))
356 BorderSize NEFastCornersKernel::border_size() const
358 return BorderSize(3);
361 void NEFastCornersKernel::configure(const IImage *input, IImage *output, uint8_t threshold, bool non_max_suppression, bool border_undefined)
363 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input);
364 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(output);
365 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
366 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
367 ARM_COMPUTE_ERROR_ON_MSG(border_undefined == false, "Not implemented");
371 _threshold = threshold;
372 _non_max_suppression = non_max_suppression;
374 constexpr unsigned int num_elems_processed_per_iteration(1);
375 constexpr unsigned int num_elems_read_per_iteration(8);
376 constexpr unsigned int num_elems_written_per_iteration(1);
377 constexpr unsigned int num_rows_read(7);
379 // Configure kernel window
380 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
381 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
382 AccessWindowRectangle input_access(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read);
384 update_window_and_padding(win, input_access, output_access);
386 output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
388 INEKernel::configure(win);
391 void NEFastCornersKernel::run(const Window &window)
393 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
394 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
396 std::array<uint8x8x2_t, PERMUTATIONS> perm_index{ {} };
398 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.
399 The three lines below setup the neon index registers to get these texels out from the table
401 const uint8x8x4_t circle_index_r = create_circle_index_register();
403 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.
405 for(size_t k = 0; k < PERMUTATIONS; ++k)
407 perm_index[k] = create_permutation_index(k);
410 Iterator in(_input, window);
411 Iterator out(_output, window);
413 const uint8_t *const __restrict in_row[7] =
415 _input->ptr_to_element(Coordinates(-3, -3)),
416 _input->ptr_to_element(Coordinates(-3, -2)),
417 _input->ptr_to_element(Coordinates(-3, -1)),
418 _input->ptr_to_element(Coordinates(-3, 0)),
419 _input->ptr_to_element(Coordinates(-3, 1)),
420 _input->ptr_to_element(Coordinates(-3, 2)),
421 _input->ptr_to_element(Coordinates(-3, 3))
424 auto is_rejected = [](uint8_t p, uint8_t q, uint8_t a, uint8_t b)
426 const bool p_is_in_ab = (a <= p) && (p <= b);
427 const bool q_is_in_ab = (a <= q) && (q <= b);
428 return p_is_in_ab && q_is_in_ab;
431 execute_window_loop(window, [&](const Coordinates & id)
433 const size_t in_offset = in.offset();
434 const uint8_t p0 = *in.ptr();
435 const uint8_t b = std::min(p0 + _threshold, 255);
436 const uint8_t a = std::max(p0 - _threshold, 0);
439 Fast check to discard points which cannot be corners and avoid the expensive computation of the potential 16 permutations
441 pixels 1 and 9 are examined, if both I1 and I9 are within [Ip - t, Ip + t], then candidate p is not a corner.
443 const uint8_t p1 = (in_offset + in_row[0])[3];
444 const uint8_t p9 = (in_offset + in_row[6])[3];
446 if(!is_rejected(p1, p9, a, b))
448 /* pixels 5 and 13 are further examined to check whether three of them are brighter than Ip + t or darker than Ip - t */
449 const uint8_t p5 = (in_offset + in_row[3])[6];
450 const uint8_t p13 = (in_offset + in_row[3])[0];
452 if(!is_rejected(p5, p13, a, b))
454 /* at this stage we use the full test with the 16 permutations to classify the point as corner or not */
455 const uint8x8x2_t tbl_circle_texel = create_circle_tbl(in_row, in_offset, circle_index_r);
457 if(point_is_fast_corner(p0, _threshold, tbl_circle_texel, perm_index.data()))
459 if(_non_max_suppression)
461 score = get_point_score(p0, _threshold, tbl_circle_texel, perm_index.data());