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/NECannyEdgeKernel.h"
26 #include "arm_compute/core/AccessWindowStatic.h"
27 #include "arm_compute/core/Error.h"
28 #include "arm_compute/core/Helpers.h"
29 #include "arm_compute/core/ITensor.h"
30 #include "arm_compute/core/TensorInfo.h"
31 #include "arm_compute/core/Types.h"
32 #include "arm_compute/core/Utils.h"
33 #include "arm_compute/core/Validate.h"
40 using namespace arm_compute;
45 } // namespace arm_compute
49 constexpr int NO_EDGE = 0;
50 constexpr int EDGE = 255;
51 constexpr int MAYBE = 127;
54 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
57 inline uint8x8_t phase_quantization(const float32x4x2_t &gx, const float32x4x2_t &gy)
59 // Constant use for evaluating score1 and score3
60 static const float32x4_t const45 = vdupq_n_f32(0.70710678118655f);
61 static const float32x4_t zero = vdupq_n_f32(0.0f);
62 static const float32x4_t one = vdupq_n_f32(1.0f);
63 static const float32x4_t two = vdupq_n_f32(2.0f);
64 static const float32x4_t three = vdupq_n_f32(3.0f);
67 const float32x4x2_t score0 =
74 const float32x4x2_t score2 =
80 // Score1 and Score3: ( sqrt(2) / 2, sqrt(2) / 2 ) - ( -sqrt(2) / 2, sqrt(2) / 2 )
81 float32x4x2_t score1 =
83 vmulq_f32(gy.val[0], const45),
84 vmulq_f32(gy.val[1], const45)
87 float32x4x2_t score3 = score1;
89 score1.val[0] = vmlaq_f32(score1.val[0], gx.val[0], const45);
90 score1.val[1] = vmlaq_f32(score1.val[1], gx.val[1], const45);
91 score3.val[0] = vmlsq_f32(score3.val[0], gx.val[0], const45);
92 score3.val[1] = vmlsq_f32(score3.val[1], gx.val[1], const45);
94 score1.val[0] = vabsq_f32(score1.val[0]);
95 score1.val[1] = vabsq_f32(score1.val[1]);
96 score3.val[0] = vabsq_f32(score3.val[0]);
97 score3.val[1] = vabsq_f32(score3.val[1]);
105 float32x4x2_t old_score = score0;
107 // score1 > old_score?
110 vcgtq_f32(score1.val[0], old_score.val[0]),
111 vcgtq_f32(score1.val[1], old_score.val[1])
114 phase.val[0] = vbslq_f32(mask.val[0], one, phase.val[0]);
115 phase.val[1] = vbslq_f32(mask.val[1], one, phase.val[1]);
116 old_score.val[0] = vbslq_f32(mask.val[0], score1.val[0], old_score.val[0]);
117 old_score.val[1] = vbslq_f32(mask.val[1], score1.val[1], old_score.val[1]);
119 // score2 > old_score?
120 mask.val[0] = vcgtq_f32(score2.val[0], old_score.val[0]);
121 mask.val[1] = vcgtq_f32(score2.val[1], old_score.val[1]);
123 phase.val[0] = vbslq_f32(mask.val[0], two, phase.val[0]);
124 phase.val[1] = vbslq_f32(mask.val[1], two, phase.val[1]);
125 old_score.val[0] = vbslq_f32(mask.val[0], score2.val[0], old_score.val[0]);
126 old_score.val[1] = vbslq_f32(mask.val[1], score2.val[1], old_score.val[1]);
128 // score3 > old_score?
129 mask.val[0] = vcgtq_f32(score3.val[0], old_score.val[0]);
130 mask.val[1] = vcgtq_f32(score3.val[1], old_score.val[1]);
132 phase.val[0] = vbslq_f32(mask.val[0], three, phase.val[0]);
133 phase.val[1] = vbslq_f32(mask.val[1], three, phase.val[1]);
134 old_score.val[0] = vbslq_f32(mask.val[0], score3.val[0], old_score.val[0]);
135 old_score.val[1] = vbslq_f32(mask.val[1], score3.val[1], old_score.val[1]);
137 // Convert from float32x4_t to uint8x8_t
138 return vmovn_u16(vcombine_u16(vmovn_u32(vcvtq_u32_f32(phase.val[0])),
139 vmovn_u32(vcvtq_u32_f32(phase.val[1]))));
142 inline uint8x8_t phase_quantization(float16x8_t gx, float16x8_t gy)
144 // Constant use for evaluating score1 and score3
145 static const float16x8_t const45 = vdupq_n_f16(0.70710678118655f);
146 static const float16x8_t zero = vdupq_n_f16(0.0f);
147 static const float16x8_t one = vdupq_n_f16(1.0f);
148 static const float16x8_t two = vdupq_n_f16(2.0f);
149 static const float16x8_t three = vdupq_n_f16(3.0f);
152 const float16x8_t score0 = vabsq_f16(gx);
155 const float16x8_t score2 = vabsq_f16(gy);
157 // Score1 and Score3: ( sqrt(2) / 2, sqrt(2) / 2 ) - ( -sqrt(2) / 2, sqrt(2) / 2 )
158 float16x8_t score1 = vmulq_f16(gy, const45);
159 float16x8_t score3 = score1;
161 score1 = vfmaq_f16(score1, gx, const45);
162 score3 = vfmsq_f16(score3, gx, const45);
164 score1 = vabsq_f16(score1);
165 score3 = vabsq_f16(score3);
167 float16x8_t phase = zero;
168 float16x8_t old_score = score0;
170 // score1 > old_score?
171 uint16x8_t mask = vcgtq_f16(score1, old_score);
173 phase = vbslq_f16(mask, one, phase);
174 old_score = vbslq_f16(mask, score1, old_score);
176 // score2 > old_score?
177 mask = vcgtq_f16(score2, old_score);
179 phase = vbslq_f16(mask, two, phase);
180 old_score = vbslq_f16(mask, score2, old_score);
182 // score3 > old_score?
183 mask = vcgtq_f16(score3, old_score);
185 phase = vbslq_f16(mask, three, phase);
187 // Convert from float16x8_t to uint8x8_t
188 return vmovn_u16(vcvtq_u16_f16(phase));
191 /** Computes the gradient phase if gradient_size = 3 or 5. The output is quantized.
192 * 0 = 0°, 1 = 45°, 2 = 90°, 3 = 135°
194 * @param[in] gx Gx component
195 * @param[in] gy Gy component
197 * @return quantized phase for 8 pixels
199 inline uint8x8_t phase_quantization_S16_S16(int16x8_t gx, int16x8_t gy)
201 return phase_quantization(vcvtq_f16_s16(gx), vcvtq_f16_s16(gy));
204 /** Computes the gradient phase if gradient_size = 7. The output is quantized.
205 * 0 = 0°, 1 = 45°, 2 = 90°, 3 = 135°
207 * @param[in] gx Gx component
208 * @param[in] gy Gy component
210 * @return quantized phase for 8 pixels
212 inline uint8x8_t phase_quantization_S32_S32(const int32x4x2_t &gx, const int32x4x2_t &gy)
215 const float32x4x2_t gx_f32 =
217 vcvtq_f32_s32(gx.val[0]),
218 vcvtq_f32_s32(gx.val[1])
221 const float32x4x2_t gy_f32 =
223 vcvtq_f32_s32(gy.val[0]),
224 vcvtq_f32_s32(gy.val[1])
227 return phase_quantization(gx_f32, gy_f32);
230 /** Computes the magnitude using the L1-norm type if gradient_size = 3 or 5
232 * @param[in] gx Gx component
233 * @param[in] gy Gy component
235 * @return magnitude for 8 pixels
237 inline uint16x8_t mag_l1_S16_S16(int16x8_t gx, int16x8_t gy)
239 return vaddq_u16(vreinterpretq_u16_s16(vabsq_s16(gx)),
240 vreinterpretq_u16_s16(vabsq_s16(gy)));
243 /** Computes the magnitude using the L1-norm type if gradient_size = 7
245 * @param[in] gx Gx component
246 * @param[in] gy Gy component
248 * @return magnitude for 8 pixels
250 inline uint32x4x2_t mag_l1_S32_S32(const int32x4x2_t &gx, const int32x4x2_t &gy)
252 const uint32x4x2_t gx_abs =
254 vreinterpretq_u32_s32(vabsq_s32(gx.val[0])),
255 vreinterpretq_u32_s32(vabsq_s32(gx.val[1]))
258 const uint32x4x2_t gy_abs =
260 vreinterpretq_u32_s32(vabsq_s32(gy.val[0])),
261 vreinterpretq_u32_s32(vabsq_s32(gy.val[1]))
264 const uint32x4x2_t out =
266 vaddq_u32(gx_abs.val[0], gy_abs.val[0]),
267 vaddq_u32(gx_abs.val[1], gy_abs.val[1])
273 inline float32x4x2_t mag_l2(const float32x4x2_t &gx, const float32x4x2_t &gy)
278 vmulq_f32(gx.val[0], gx.val[0]),
279 vmulq_f32(gx.val[1], gx.val[1])
283 mag.val[0] = vmlaq_f32(mag.val[0], gy.val[0], gy.val[0]);
284 mag.val[1] = vmlaq_f32(mag.val[1], gy.val[1], gy.val[1]);
287 mag.val[0] = vmulq_f32(vrsqrteq_f32(mag.val[0]), mag.val[0]);
288 mag.val[1] = vmulq_f32(vrsqrteq_f32(mag.val[1]), mag.val[1]);
293 inline float16x8_t mag_l2(float16x8_t gx, float16x8_t gy)
296 float16x8_t mag = vmulq_f16(gx, gx);
299 mag = vfmaq_f16(mag, gy, gy);
302 mag = vmulq_f16(vrsqrteq_f16(mag), mag);
307 /** Computes the magnitude using L2-norm if gradient_size = 3 or 5
309 * @param[in] gx Gx component
310 * @param[in] gy Gy component
312 * @return magnitude for 8 pixels
314 inline uint16x8_t mag_l2_S16_S16(int16x8_t gx, int16x8_t gy)
316 /* Compute magnitude using L2 normalization */
317 const float16x8_t gx2 = vcvtq_f16_s16(gx);
318 const float16x8_t gy2 = vcvtq_f16_s16(gy);
319 const float16x8_t mag = mag_l2(gx2, gy2);
321 /* Store magnitude - Convert to uint16x8 */
322 return vcvtq_u16_f16(mag);
325 /** Computes the magnitude using L2-norm if gradient_size = 7
327 * @param[in] gx Gx component
328 * @param[in] gy Gy component
330 * @return magnitude for 8 pixels
332 inline uint32x4x2_t mag_l2_S32_S32(const int32x4x2_t &gx, const int32x4x2_t &gy)
334 // Compute magnitude using L2 normalization
337 vcvtq_f32_s32(gx.val[0]),
338 vcvtq_f32_s32(gx.val[1])
343 vcvtq_f32_s32(gy.val[0]),
344 vcvtq_f32_s32(gy.val[1])
347 const float32x4x2_t mag = mag_l2(gx2, gy2);
348 const uint32x4x2_t mag32 =
350 vcvtq_u32_f32(mag.val[0]),
351 vcvtq_u32_f32(mag.val[1])
357 /** Gradient function used when the gradient size = 3 or 5 and when the norm_type = L1-norm
359 * @param[in] in1_ptr Pointer to source image. Gx image. Data type supported S16
360 * @param[in] in2_ptr Pointer to source image. Gy image. Data type supported S16
361 * @param[out] out1_ptr Pointer to destination image. Magnitude. Data type supported U16
362 * @param[out] out2_ptr Pointer to destination image. Quantized phase. Data type supported U8
364 void mag_phase_l1norm_S16_S16_U16_U8(const void *__restrict in1_ptr, const void *__restrict in2_ptr, void *__restrict out1_ptr, void *__restrict out2_ptr)
366 const auto in1 = static_cast<const int16_t *__restrict>(in1_ptr);
367 const auto in2 = static_cast<const int16_t *__restrict>(in2_ptr);
368 const auto out1 = static_cast<uint16_t *__restrict>(out1_ptr);
369 const auto out2 = static_cast<uint8_t *__restrict>(out2_ptr);
371 const int16x8x4_t gx =
379 const int16x8x4_t gy =
387 // Compute and store phase
388 vst1_u8(out2 + 0, phase_quantization_S16_S16(gx.val[0], gy.val[0]));
389 vst1_u8(out2 + 8, phase_quantization_S16_S16(gx.val[1], gy.val[1]));
390 vst1_u8(out2 + 16, phase_quantization_S16_S16(gx.val[2], gy.val[2]));
391 vst1_u8(out2 + 24, phase_quantization_S16_S16(gx.val[3], gy.val[3]));
393 // Compute ans store magnitude using L1 normalization
394 vst1q_u16(out1 + 0, mag_l1_S16_S16(gx.val[0], gy.val[0]));
395 vst1q_u16(out1 + 8, mag_l1_S16_S16(gx.val[1], gy.val[1]));
396 vst1q_u16(out1 + 16, mag_l1_S16_S16(gx.val[2], gy.val[2]));
397 vst1q_u16(out1 + 24, mag_l1_S16_S16(gx.val[3], gy.val[3]));
400 /** Gradient function used when the gradient size = 3 or 5 and when the norm_type = L2-norm
402 * @param[in] in1_ptr Pointer to source image. Gx image. Data type supported S16
403 * @param[in] in2_ptr Pointer to source image. Gy image. Data type supported S16
404 * @param[out] out1_ptr Pointer to destination image. Magnitude. Data type supported U16
405 * @param[out] out2_ptr Pointer to destination image. Quantized phase. Data type supported U8
407 void mag_phase_l2norm_S16_S16_U16_U8(const void *__restrict in1_ptr, const void *__restrict in2_ptr, void *__restrict out1_ptr, void *__restrict out2_ptr)
409 const auto in1 = static_cast<const int16_t *__restrict>(in1_ptr);
410 const auto in2 = static_cast<const int16_t *__restrict>(in2_ptr);
411 const auto out1 = static_cast<uint16_t *__restrict>(out1_ptr);
412 const auto out2 = static_cast<uint8_t *__restrict>(out2_ptr);
414 const int16x8x4_t gx =
422 const int16x8x4_t gy =
430 // Compute and store phase
431 vst1_u8(out2 + 0, phase_quantization_S16_S16(gx.val[0], gy.val[0]));
432 vst1_u8(out2 + 8, phase_quantization_S16_S16(gx.val[1], gy.val[1]));
433 vst1_u8(out2 + 16, phase_quantization_S16_S16(gx.val[2], gy.val[2]));
434 vst1_u8(out2 + 24, phase_quantization_S16_S16(gx.val[3], gy.val[3]));
436 // Compute and store magnitude using L2 normalization
437 vst1q_u16(out1 + 0, mag_l2_S16_S16(gx.val[0], gy.val[0]));
438 vst1q_u16(out1 + 8, mag_l2_S16_S16(gx.val[1], gy.val[1]));
439 vst1q_u16(out1 + 16, mag_l2_S16_S16(gx.val[2], gy.val[2]));
440 vst1q_u16(out1 + 24, mag_l2_S16_S16(gx.val[3], gy.val[3]));
443 /** Gradient function used when the gradient size = 7 and when the norm_type = L1-norm
445 * @param[in] in1_ptr Pointer to source image. Gx image. Data type supported S32
446 * @param[in] in2_ptr Pointer to source image. Gy image. Data type supported S32
447 * @param[out] out1_ptr Pointer to destination image. Magnitude. Data type supported U32
448 * @param[out] out2_ptr Pointer to destination image. Quantized phase. Data type supported U8
450 void mag_phase_l1norm_S32_S32_U32_U8(const void *__restrict in1_ptr, const void *__restrict in2_ptr, void *__restrict out1_ptr, void *__restrict out2_ptr)
452 auto in1 = static_cast<const int32_t *__restrict>(in1_ptr);
453 auto in2 = static_cast<const int32_t *__restrict>(in2_ptr);
454 auto out1 = static_cast<uint32_t *__restrict>(out1_ptr);
455 auto out2 = static_cast<uint8_t *__restrict>(out2_ptr);
457 // Process low and high part
458 for(size_t i = 0; i < 2; ++i, in1 += 16, in2 += 16, out1 += 16, out2 += 16)
460 const int32x4x2_t gx0 =
466 const int32x4x2_t gx1 =
472 const int32x4x2_t gy0 =
478 const int32x4x2_t gy1 =
484 // Compute and store phase
485 vst1_u8(out2 + 0, phase_quantization_S32_S32(gx0, gy0));
486 vst1_u8(out2 + 8, phase_quantization_S32_S32(gx1, gy1));
488 // Compute magnitude using L1 normalization
489 const uint32x4x2_t mag0 = mag_l1_S32_S32(gx0, gy0);
490 const uint32x4x2_t mag1 = mag_l1_S32_S32(gx1, gy1);
493 vst1q_u32(out1 + 0, mag0.val[0]);
494 vst1q_u32(out1 + 4, mag0.val[1]);
495 vst1q_u32(out1 + 8, mag1.val[0]);
496 vst1q_u32(out1 + 12, mag1.val[1]);
500 /** Gradient function used when the gradient size = 7 and when the norm_type = L2-norm
502 * @param[in] in1_ptr Pointer to source image. Gx image. Data type supported S32
503 * @param[in] in2_ptr Pointer to source image. Gy image. Data type supported S32
504 * @param[out] out1_ptr Pointer to destination image. Magnitude. Data type supported U32
505 * @param[out] out2_ptr Pointer to destination image. Quantized phase. Data type supported U8
507 void mag_phase_l2norm_S32_S32_U32_U8(const void *__restrict in1_ptr, const void *__restrict in2_ptr, void *__restrict out1_ptr, void *__restrict out2_ptr)
509 auto in1 = static_cast<const int32_t *__restrict>(in1_ptr);
510 auto in2 = static_cast<const int32_t *__restrict>(in2_ptr);
511 auto out1 = static_cast<uint32_t *__restrict>(out1_ptr);
512 auto out2 = static_cast<uint8_t *__restrict>(out2_ptr);
514 // Process low and high part
515 for(size_t i = 0; i < 2; ++i, in1 += 16, in2 += 16, out1 += 16, out2 += 16)
517 const int32x4x2_t gx0 =
523 const int32x4x2_t gx1 =
529 const int32x4x2_t gy0 =
535 const int32x4x2_t gy1 =
541 // Compute and store phase
542 vst1_u8(out2 + 0, phase_quantization_S32_S32(gx0, gy0));
543 vst1_u8(out2 + 8, phase_quantization_S32_S32(gx1, gy1));
545 // Compute magnitude using L2 normalization
546 const uint32x4x2_t mag0 = mag_l2_S32_S32(gx0, gy0);
547 const uint32x4x2_t mag1 = mag_l2_S32_S32(gx1, gy1);
550 vst1q_u32(out1 + 0, mag0.val[0]);
551 vst1q_u32(out1 + 4, mag0.val[1]);
552 vst1q_u32(out1 + 8, mag1.val[0]);
553 vst1q_u32(out1 + 12, mag1.val[1]);
557 inline uint16x4_t non_max_U32_helper(const uint32_t *in, const uint16x4_t pc, const uint32_t stride_mag, const int32_t lower_thr, const int32_t upper_thr)
560 const uint32x4_t pc32 = vmovl_u16(pc);
562 // Get magnitude for 4 pixel
563 uint32x4_t mc = vld1q_u32(in);
565 // Angle_quantized: 0 = 0°, 1 = 45°, 2 = 90°, 3 = 135°
567 const uint32x4_t mk0_0 = vld1q_u32(in - 1);
568 const uint32x4_t mk0_1 = vld1q_u32(in + 1);
569 uint32x4_t mask0 = vceqq_u32(pc32, vdupq_n_u32(0));
570 mask0 = vandq_u32(mask0, vcgeq_u32(mc, mk0_0));
571 mask0 = vandq_u32(mask0, vcgeq_u32(mc, mk0_1));
574 const uint32x4_t mk45_0 = vld1q_u32(in - stride_mag - 1);
575 const uint32x4_t mk45_1 = vld1q_u32(in + stride_mag + 1);
576 uint32x4_t mask1 = vceqq_u32(pc32, vdupq_n_u32(1));
577 mask1 = vandq_u32(mask1, vcgeq_u32(mc, mk45_0));
578 mask1 = vandq_u32(mask1, vcgeq_u32(mc, mk45_1));
581 const uint32x4_t mk90_0 = vld1q_u32(in - stride_mag);
582 const uint32x4_t mk90_1 = vld1q_u32(in + stride_mag);
583 uint32x4_t mask2 = vceqq_u32(pc32, vdupq_n_u32(2));
584 mask2 = vandq_u32(mask2, vcgeq_u32(mc, mk90_0));
585 mask2 = vandq_u32(mask2, vcgeq_u32(mc, mk90_1));
588 const uint32x4_t mk135_0 = vld1q_u32(in - stride_mag + 1);
589 const uint32x4_t mk135_1 = vld1q_u32(in + stride_mag - 1);
590 uint32x4_t mask3 = vceqq_u32(pc32, vdupq_n_u32(3));
591 mask3 = vandq_u32(mask3, vcgeq_u32(mc, mk135_0));
592 mask3 = vandq_u32(mask3, vcgeq_u32(mc, mk135_1));
595 mask0 = vorrq_u32(mask0, mask1);
596 mask2 = vorrq_u32(mask2, mask3);
597 mask0 = vorrq_u32(mask0, mask2);
599 mc = vbslq_u32(mask0, mc, vdupq_n_u32(0));
602 mask0 = vcgtq_u32(mc, vdupq_n_u32(upper_thr));
605 mask1 = vcleq_u32(mc, vdupq_n_u32(lower_thr));
607 // mc <= upper_thr && mc > lower_thr
608 mask2 = vcleq_u32(mc, vdupq_n_u32(upper_thr));
609 mask2 = vandq_u32(mask2, vcgtq_u32(mc, vdupq_n_u32(lower_thr)));
611 mc = vbslq_u32(mask0, vdupq_n_u32(EDGE), mc);
612 mc = vbslq_u32(mask1, vdupq_n_u32(NO_EDGE), mc);
613 mc = vbslq_u32(mask2, vdupq_n_u32(MAYBE), mc);
615 return vmovn_u32(mc);
618 /** Computes edge tracing when is called by edge_trace_U8_U8 recursively
620 * @param[in] in Pointer to source image. Data type supported U8
621 * @param[out] out Pointer to destination image. Data type supported U8
622 * @param[in] in_stride Stride of the input image
623 * @param[in] out_stride Stride of the output image
625 void edge_trace_recursive_U8_U8(uint8_t *__restrict in, uint8_t *__restrict out, const int32_t in_stride, const int32_t out_stride)
627 // Look for MAYBE pixels in 8 directions
631 uint8_t pixel = *(in - 1);
635 // Touched a MAYBE point. MAYBE becomes EDGE
638 edge_trace_recursive_U8_U8(in - 1, out - 1, in_stride, out_stride);
646 // Touched a MAYBE point. MAYBE becomes EDGE
649 edge_trace_recursive_U8_U8(in + 1, out + 1, in_stride, out_stride);
660 // Touched a MAYBE point. MAYBE becomes EDGE
663 edge_trace_recursive_U8_U8(in - 1, out - 1, in_stride, out_stride);
671 // Touched a MAYBE point. MAYBE becomes EDGE
674 edge_trace_recursive_U8_U8(in, out, in_stride, out_stride);
682 // Touched a MAYBE point. MAYBE becomes EDGE
685 edge_trace_recursive_U8_U8(in + 1, out + 1, in_stride, out_stride);
689 out += out_stride * 2;
696 // Touched a MAYBE point. MAYBE becomes EDGE
699 edge_trace_recursive_U8_U8(in - 1, out - 1, in_stride, out_stride);
707 // Touched a MAYBE point. MAYBE becomes EDGE
710 edge_trace_recursive_U8_U8(in, out, in_stride, out_stride);
718 // Touched a MAYBE point. MAYBE becomes EDGE
721 edge_trace_recursive_U8_U8(in + 1, out + 1, in_stride, out_stride);
726 void NEGradientFP16Kernel::configure(const ITensor *gx, const ITensor *gy, ITensor *magnitude, ITensor *phase, int32_t norm_type)
728 ARM_COMPUTE_ERROR_ON_NULLPTR(gx, gy, magnitude, phase);
730 set_shape_if_empty(*magnitude->info(), gx->info()->tensor_shape());
731 set_shape_if_empty(*phase->info(), gx->info()->tensor_shape());
733 Format magnitude_format = gx->info()->data_type() == DataType::S16 ? Format::U16 : Format::U32;
734 set_format_if_unknown(*magnitude->info(), magnitude_format);
735 set_format_if_unknown(*phase->info(), Format::U8);
737 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(gx, gy, magnitude, phase);
738 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(gx, 1, DataType::S16, DataType::S32);
739 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(gy, 1, DataType::S16, DataType::S32);
740 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(magnitude, 1, DataType::U16, DataType::U32);
741 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(phase, 1, DataType::U8);
742 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(gx, gy);
743 ARM_COMPUTE_ERROR_ON_MSG(element_size_from_data_type(gx->info()->data_type()) != element_size_from_data_type(magnitude->info()->data_type()), "Magnitude must have the same element size as Gx and Gy");
747 _magnitude = magnitude;
750 if(_gx->info()->data_type() == DataType::S16)
754 _func = &fp16::mag_phase_l1norm_S16_S16_U16_U8;
758 _func = &fp16::mag_phase_l2norm_S16_S16_U16_U8;
765 _func = &fp16::mag_phase_l1norm_S32_S32_U32_U8;
769 _func = &fp16::mag_phase_l2norm_S32_S32_U32_U8;
773 constexpr unsigned int num_elems_processed_per_iteration = 32;
775 // Configure kernel window
776 Window win = calculate_max_window(*_gx->info(), Steps(num_elems_processed_per_iteration));
778 AccessWindowHorizontal gx_access(_gx->info(), 0, num_elems_processed_per_iteration);
779 AccessWindowHorizontal gy_access(_gy->info(), 0, num_elems_processed_per_iteration);
780 AccessWindowHorizontal mag_access(_magnitude->info(), 0, num_elems_processed_per_iteration);
781 AccessWindowHorizontal phase_access(_phase->info(), 0, num_elems_processed_per_iteration);
783 update_window_and_padding(win, gx_access, gy_access, mag_access, phase_access);
785 mag_access.set_valid_region(win, _gx->info()->valid_region());
786 phase_access.set_valid_region(win, _gx->info()->valid_region());
788 INEKernel::configure(win);
790 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
794 inline uint8x8_t phase_quantization(const float32x4x2_t &gx, const float32x4x2_t &gy)
796 // Constant use for evaluating score1 and score3
797 static const float32x4_t const45 = vdupq_n_f32(0.70710678118655f);
798 static const float32x4_t zero = vdupq_n_f32(0.0f);
799 static const float32x4_t one = vdupq_n_f32(1.0f);
800 static const float32x4_t two = vdupq_n_f32(2.0f);
801 static const float32x4_t three = vdupq_n_f32(3.0f);
804 const float32x4x2_t score0 =
807 vabsq_f32(gx.val[0]),
813 const float32x4x2_t score2 =
816 vabsq_f32(gy.val[0]),
821 // Score1 and Score3: ( sqrt(2) / 2, sqrt(2) / 2 ) - ( -sqrt(2) / 2, sqrt(2) / 2 )
822 float32x4x2_t score1 =
825 vmulq_f32(gy.val[0], const45),
826 vmulq_f32(gy.val[1], const45)
830 float32x4x2_t score3 = score1;
832 score1.val[0] = vmlaq_f32(score1.val[0], gx.val[0], const45);
833 score1.val[1] = vmlaq_f32(score1.val[1], gx.val[1], const45);
834 score3.val[0] = vmlsq_f32(score3.val[0], gx.val[0], const45);
835 score3.val[1] = vmlsq_f32(score3.val[1], gx.val[1], const45);
837 score1.val[0] = vabsq_f32(score1.val[0]);
838 score1.val[1] = vabsq_f32(score1.val[1]);
839 score3.val[0] = vabsq_f32(score3.val[0]);
840 score3.val[1] = vabsq_f32(score3.val[1]);
842 float32x4x2_t phase =
850 float32x4x2_t old_score = score0;
852 // score1 > old_score?
856 vcgtq_f32(score1.val[0], old_score.val[0]),
857 vcgtq_f32(score1.val[1], old_score.val[1])
861 phase.val[0] = vbslq_f32(mask.val[0], one, phase.val[0]);
862 phase.val[1] = vbslq_f32(mask.val[1], one, phase.val[1]);
863 old_score.val[0] = vbslq_f32(mask.val[0], score1.val[0], old_score.val[0]);
864 old_score.val[1] = vbslq_f32(mask.val[1], score1.val[1], old_score.val[1]);
866 // score2 > old_score?
867 mask.val[0] = vcgtq_f32(score2.val[0], old_score.val[0]);
868 mask.val[1] = vcgtq_f32(score2.val[1], old_score.val[1]);
870 phase.val[0] = vbslq_f32(mask.val[0], two, phase.val[0]);
871 phase.val[1] = vbslq_f32(mask.val[1], two, phase.val[1]);
872 old_score.val[0] = vbslq_f32(mask.val[0], score2.val[0], old_score.val[0]);
873 old_score.val[1] = vbslq_f32(mask.val[1], score2.val[1], old_score.val[1]);
875 // score3 > old_score?
876 mask.val[0] = vcgtq_f32(score3.val[0], old_score.val[0]);
877 mask.val[1] = vcgtq_f32(score3.val[1], old_score.val[1]);
879 phase.val[0] = vbslq_f32(mask.val[0], three, phase.val[0]);
880 phase.val[1] = vbslq_f32(mask.val[1], three, phase.val[1]);
881 old_score.val[0] = vbslq_f32(mask.val[0], score3.val[0], old_score.val[0]);
882 old_score.val[1] = vbslq_f32(mask.val[1], score3.val[1], old_score.val[1]);
884 // Convert from float32x4_t to uint8x8_t
885 return vmovn_u16(vcombine_u16(vmovn_u32(vcvtq_u32_f32(phase.val[0])),
886 vmovn_u32(vcvtq_u32_f32(phase.val[1]))));
889 /* Computes the gradient phase if gradient_size = 3 or 5. The output is quantized.
890 * 0 = 0°, 1 = 45°, 2 = 90°, 3 = 135°
892 * @param[in] gx Gx component
893 * @param[in] gy Gy component
895 * @return quantized phase for 8 pixels
897 inline uint8x8_t phase_quantization_S16_S16(int16x8_t gx, int16x8_t gy)
900 const float32x4x2_t gx_f32 =
903 vcvtq_f32_s32(vmovl_s16(vget_low_s16(gx))),
904 vcvtq_f32_s32(vmovl_s16(vget_high_s16(gx)))
908 const float32x4x2_t gy_f32 =
911 vcvtq_f32_s32(vmovl_s16(vget_low_s16(gy))),
912 vcvtq_f32_s32(vmovl_s16(vget_high_s16(gy)))
916 return phase_quantization(gx_f32, gy_f32);
919 /* Computes the gradient phase if gradient_size = 7. The output is quantized.
920 * 0 = 0°, 1 = 45°, 2 = 90°, 3 = 135°
922 * @param[in] gx Gx component
923 * @param[in] gy Gy component
925 * @return quantized phase for 8 pixels
927 inline uint8x8_t phase_quantization_S32_S32(const int32x4x2_t &gx, const int32x4x2_t &gy)
930 const float32x4x2_t gx_f32 =
933 vcvtq_f32_s32(gx.val[0]),
934 vcvtq_f32_s32(gx.val[1])
938 const float32x4x2_t gy_f32 =
941 vcvtq_f32_s32(gy.val[0]),
942 vcvtq_f32_s32(gy.val[1])
946 return phase_quantization(gx_f32, gy_f32);
949 /* Computes the magnitude using the L1-norm type if gradient_size = 3 or 5
951 * @param[in] gx Gx component
952 * @param[in] gy Gy component
954 * @return magnitude for 8 pixels
956 inline uint16x8_t mag_l1_S16_S16(int16x8_t gx, int16x8_t gy)
958 return vaddq_u16(vreinterpretq_u16_s16(vabsq_s16(gx)),
959 vreinterpretq_u16_s16(vabsq_s16(gy)));
962 /* Computes the magnitude using the L1-norm type if gradient_size = 7
964 * @param[in] gx Gx component
965 * @param[in] gy Gy component
967 * @return magnitude for 8 pixels
969 inline uint32x4x2_t mag_l1_S32_S32(const int32x4x2_t &gx, const int32x4x2_t &gy)
971 const uint32x4x2_t gx_abs =
974 vreinterpretq_u32_s32(vabsq_s32(gx.val[0])),
975 vreinterpretq_u32_s32(vabsq_s32(gx.val[1]))
979 const uint32x4x2_t gy_abs =
982 vreinterpretq_u32_s32(vabsq_s32(gy.val[0])),
983 vreinterpretq_u32_s32(vabsq_s32(gy.val[1]))
987 const uint32x4x2_t output =
990 vaddq_u32(gx_abs.val[0], gy_abs.val[0]),
991 vaddq_u32(gx_abs.val[1], gy_abs.val[1])
998 inline float32x4x2_t mag_l2(const float32x4x2_t &gx, const float32x4x2_t &gy)
1001 float32x4x2_t magnitude =
1004 vmulq_f32(gx.val[0], gx.val[0]),
1005 vmulq_f32(gx.val[1], gx.val[1])
1010 magnitude.val[0] = vmlaq_f32(magnitude.val[0], gy.val[0], gy.val[0]);
1011 magnitude.val[1] = vmlaq_f32(magnitude.val[1], gy.val[1], gy.val[1]);
1014 magnitude.val[0] = vmulq_f32(vrsqrteq_f32(magnitude.val[0]), magnitude.val[0]);
1015 magnitude.val[1] = vmulq_f32(vrsqrteq_f32(magnitude.val[1]), magnitude.val[1]);
1020 /* Computes the magnitude using L2-norm if gradient_size = 3 or 5
1022 * @param[in] gx Gx component
1023 * @param[in] gy Gy component
1025 * @return magnitude for 8 pixels
1027 inline uint16x8_t mag_l2_S16_S16(int16x8_t gx, int16x8_t gy)
1029 // Compute magnitude using L2 normalization
1030 const float32x4x2_t gx2 =
1033 vcvtq_f32_s32(vmovl_s16(vget_low_s16(gx))),
1034 vcvtq_f32_s32(vmovl_s16(vget_high_s16(gx)))
1038 const float32x4x2_t gy2 =
1041 vcvtq_f32_s32(vmovl_s16(vget_low_s16(gy))),
1042 vcvtq_f32_s32(vmovl_s16(vget_high_s16(gy)))
1046 const float32x4x2_t magnitude = mag_l2(gx2, gy2);
1048 // Store magnitude - Convert to uint16x8
1049 return vcombine_u16(vmovn_u32(vcvtq_u32_f32(magnitude.val[0])),
1050 vmovn_u32(vcvtq_u32_f32(magnitude.val[1])));
1053 /* Computes the magnitude using L2-norm if gradient_size = 7
1055 * @param[in] gx Gx component
1056 * @param[in] gy Gy component
1058 * @return magnitude for 8 pixels
1060 inline uint32x4x2_t mag_l2_S32_S32(const int32x4x2_t &gx, const int32x4x2_t &gy)
1062 // Compute magnitude using L2 normalization
1066 vcvtq_f32_s32(gx.val[0]),
1067 vcvtq_f32_s32(gx.val[1])
1074 vcvtq_f32_s32(gy.val[0]),
1075 vcvtq_f32_s32(gy.val[1])
1079 const float32x4x2_t magnitude = mag_l2(gx2, gy2);
1080 const uint32x4x2_t mag32 =
1083 vcvtq_u32_f32(magnitude.val[0]),
1084 vcvtq_u32_f32(magnitude.val[1])
1091 /* Gradient function used when the gradient size = 3 or 5 and when the norm_type = L1-norm
1093 * @param[in] gx_ptr Pointer to source image. Gx image. Data type supported S16
1094 * @param[in] gy_ptr Pointer to source image. Gy image. Data type supported S16
1095 * @param[out] magnitude_ptr Pointer to destination image. Magnitude. Data type supported U16
1096 * @param[out] phase_ptr Pointer to destination image. Quantized phase. Data type supported U8
1098 void mag_phase_l1norm_S16_S16_U16_U8(const void *__restrict gx_ptr, const void *__restrict gy_ptr, void *__restrict magnitude_ptr, void *__restrict phase_ptr)
1100 const auto gx = static_cast<const int16_t *__restrict>(gx_ptr);
1101 const auto gy = static_cast<const int16_t *__restrict>(gy_ptr);
1102 const auto magnitude = static_cast<uint16_t *__restrict>(magnitude_ptr);
1103 const auto phase = static_cast<uint8_t *__restrict>(phase_ptr);
1105 const int16x8x4_t gx_val =
1115 const int16x8x4_t gy_val =
1125 // Compute and store phase
1126 vst1_u8(phase + 0, phase_quantization_S16_S16(gx_val.val[0], gy_val.val[0]));
1127 vst1_u8(phase + 8, phase_quantization_S16_S16(gx_val.val[1], gy_val.val[1]));
1128 vst1_u8(phase + 16, phase_quantization_S16_S16(gx_val.val[2], gy_val.val[2]));
1129 vst1_u8(phase + 24, phase_quantization_S16_S16(gx_val.val[3], gy_val.val[3]));
1131 // Compute ans store magnitude using L1 normalization
1132 vst1q_u16(magnitude + 0, mag_l1_S16_S16(gx_val.val[0], gy_val.val[0]));
1133 vst1q_u16(magnitude + 8, mag_l1_S16_S16(gx_val.val[1], gy_val.val[1]));
1134 vst1q_u16(magnitude + 16, mag_l1_S16_S16(gx_val.val[2], gy_val.val[2]));
1135 vst1q_u16(magnitude + 24, mag_l1_S16_S16(gx_val.val[3], gy_val.val[3]));
1138 /* Gradient function used when the gradient size = 3 or 5 and when the norm_type = L2-norm
1140 * @param[in] gx_ptr Pointer to source image. Gx image. Data type supported S16
1141 * @param[in] gy_ptr Pointer to source image. Gy image. Data type supported S16
1142 * @param[out] magnitude_ptr Pointer to destination image. Magnitude. Data type supported U16
1143 * @param[out] phase_ptr Pointer to destination image. Quantized phase. Data type supported U8
1145 void mag_phase_l2norm_S16_S16_U16_U8(const void *__restrict gx_ptr, const void *__restrict gy_ptr, void *__restrict magnitude_ptr, void *__restrict phase_ptr)
1147 const auto gx = static_cast<const int16_t *__restrict>(gx_ptr);
1148 const auto gy = static_cast<const int16_t *__restrict>(gy_ptr);
1149 const auto magnitude = static_cast<uint16_t *__restrict>(magnitude_ptr);
1150 const auto phase = static_cast<uint8_t *__restrict>(phase_ptr);
1152 const int16x8x4_t gx_val =
1162 const int16x8x4_t gy_val =
1172 // Compute and store phase
1173 vst1_u8(phase + 0, phase_quantization_S16_S16(gx_val.val[0], gy_val.val[0]));
1174 vst1_u8(phase + 8, phase_quantization_S16_S16(gx_val.val[1], gy_val.val[1]));
1175 vst1_u8(phase + 16, phase_quantization_S16_S16(gx_val.val[2], gy_val.val[2]));
1176 vst1_u8(phase + 24, phase_quantization_S16_S16(gx_val.val[3], gy_val.val[3]));
1178 // Compute and store magnitude using L2 normalization
1179 vst1q_u16(magnitude + 0, mag_l2_S16_S16(gx_val.val[0], gy_val.val[0]));
1180 vst1q_u16(magnitude + 8, mag_l2_S16_S16(gx_val.val[1], gy_val.val[1]));
1181 vst1q_u16(magnitude + 16, mag_l2_S16_S16(gx_val.val[2], gy_val.val[2]));
1182 vst1q_u16(magnitude + 24, mag_l2_S16_S16(gx_val.val[3], gy_val.val[3]));
1185 /* Gradient function used when the gradient size = 7 and when the norm_type = L1-norm
1187 * @param[in] gx_ptr Pointer to source image. Gx image. Data type supported S32
1188 * @param[in] gy_ptr Pointer to source image. Gy image. Data type supported S32
1189 * @param[out] magnitude_ptr Pointer to destination image. Magnitude. Data type supported U32
1190 * @param[out] phase_ptr Pointer to destination image. Quantized phase. Data type support U8
1192 void mag_phase_l1norm_S32_S32_U32_U8(const void *__restrict gx_ptr, const void *__restrict gy_ptr, void *__restrict magnitude_ptr, void *__restrict phase_ptr)
1194 auto gx = static_cast<const int32_t *__restrict>(gx_ptr);
1195 auto gy = static_cast<const int32_t *__restrict>(gy_ptr);
1196 auto magnitude = static_cast<uint32_t *__restrict>(magnitude_ptr);
1197 auto phase = static_cast<uint8_t *__restrict>(phase_ptr);
1199 // Process low and high part
1200 for(size_t i = 0; i < 2; ++i, gx += 16, gy += 16, magnitude += 16, phase += 16)
1202 const int32x4x2_t gx0 =
1210 const int32x4x2_t gx1 =
1218 const int32x4x2_t gy0 =
1226 const int32x4x2_t gy1 =
1234 // Compute and store phase
1235 vst1_u8(phase + 0, phase_quantization_S32_S32(gx0, gy0));
1236 vst1_u8(phase + 8, phase_quantization_S32_S32(gx1, gy1));
1238 // Compute magnitude using L1 normalization
1239 const uint32x4x2_t mag0 = mag_l1_S32_S32(gx0, gy0);
1240 const uint32x4x2_t mag1 = mag_l1_S32_S32(gx1, gy1);
1243 vst1q_u32(magnitude + 0, mag0.val[0]);
1244 vst1q_u32(magnitude + 4, mag0.val[1]);
1245 vst1q_u32(magnitude + 8, mag1.val[0]);
1246 vst1q_u32(magnitude + 12, mag1.val[1]);
1250 /* Gradient function used when the gradient size = 7 and when the norm_type = L2-norm
1252 * @param[in] gx_ptr Pointer to source image. Gx image. Data type supported S32
1253 * @param[in] gy_ptr Pointer to source image. Gy image. Data type supported S32
1254 * @param[out] magnitude_ptr Pointer to destination image. Magnitude. Data type supported U32
1255 * @param[out] phase_ptr Pointer to destination image. Quantized phase. Data type supported U8
1257 void mag_phase_l2norm_S32_S32_U32_U8(const void *__restrict gx_ptr, const void *__restrict gy_ptr, void *__restrict magnitude_ptr, void *__restrict phase_ptr)
1259 auto gx = static_cast<const int32_t *__restrict>(gx_ptr);
1260 auto gy = static_cast<const int32_t *__restrict>(gy_ptr);
1261 auto magnitude = static_cast<uint32_t *__restrict>(magnitude_ptr);
1262 auto phase = static_cast<uint8_t *__restrict>(phase_ptr);
1264 // Process low and high part
1265 for(size_t i = 0; i < 2; ++i, gx += 16, gy += 16, magnitude += 16, phase += 16)
1267 const int32x4x2_t gx0 =
1275 const int32x4x2_t gx1 =
1283 const int32x4x2_t gy0 =
1291 const int32x4x2_t gy1 =
1299 // Compute and store phase
1300 vst1_u8(phase + 0, phase_quantization_S32_S32(gx0, gy0));
1301 vst1_u8(phase + 8, phase_quantization_S32_S32(gx1, gy1));
1303 // Compute magnitude using L2 normalization
1304 const uint32x4x2_t mag0 = mag_l2_S32_S32(gx0, gy0);
1305 const uint32x4x2_t mag1 = mag_l2_S32_S32(gx1, gy1);
1308 vst1q_u32(magnitude + 0, mag0.val[0]);
1309 vst1q_u32(magnitude + 4, mag0.val[1]);
1310 vst1q_u32(magnitude + 8, mag1.val[0]);
1311 vst1q_u32(magnitude + 12, mag1.val[1]);
1315 /* Computes non-maxima suppression and hysteresis when the gradient size = 3 or 5
1317 * @param[in] magnitude_ptr Pointer to source image. Magnitude. Data type supported U16
1318 * @param[in] phase_ptr Pointer to source image. Quantized phase. Data type supported U8
1319 * @param[out] output_ptr Pointer to output image. Data type supported U8
1320 * @param[in] stride_mag Stride of magnitude image
1321 * @param[in] lower_thr Lower threshold used for the hysteresis
1322 * @param[in] upper_thr Upper threshold used for the hysteresis
1324 void non_max_suppression_U16_U8_U8(const void *__restrict magnitude_ptr, const void *__restrict phase_ptr, void *__restrict output_ptr, const uint32_t stride_mag, const int32_t lower_thr,
1325 const int32_t upper_thr)
1327 const auto magnitude = static_cast<const uint16_t *__restrict>(magnitude_ptr);
1328 const auto phase = static_cast<const uint8_t *__restrict>(phase_ptr);
1329 const auto output = static_cast<uint8_t *__restrict>(output_ptr);
1331 // Get magnitude and phase of the centre pixels
1332 uint16x8_t mc = vld1q_u16(magnitude);
1334 // Angle_quantized: 0 = 0°, 1 = 45°, 2 = 90°, 3 = 135°
1335 const uint16x8_t pc16 = vmovl_u8(vld1_u8(phase));
1338 const uint16x8_t mk0_0 = vld1q_u16(magnitude - 1);
1339 const uint16x8_t mk0_1 = vld1q_u16(magnitude + 1);
1340 uint16x8_t mask0 = vceqq_u16(pc16, vdupq_n_u16(0));
1341 mask0 = vandq_u16(mask0, vcgeq_u16(mc, mk0_0));
1342 mask0 = vandq_u16(mask0, vcgeq_u16(mc, mk0_1));
1345 const uint16x8_t mk45_0 = vld1q_u16(magnitude - stride_mag - 1);
1346 const uint16x8_t mk45_1 = vld1q_u16(magnitude + stride_mag + 1);
1347 uint16x8_t mask1 = vceqq_u16(pc16, vdupq_n_u16(1));
1348 mask1 = vandq_u16(mask1, vcgeq_u16(mc, mk45_0));
1349 mask1 = vandq_u16(mask1, vcgeq_u16(mc, mk45_1));
1352 const uint16x8_t mk90_0 = vld1q_u16(magnitude - stride_mag);
1353 const uint16x8_t mk90_1 = vld1q_u16(magnitude + stride_mag);
1354 uint16x8_t mask2 = vceqq_u16(pc16, vdupq_n_u16(2));
1355 mask2 = vandq_u16(mask2, vcgeq_u16(mc, mk90_0));
1356 mask2 = vandq_u16(mask2, vcgeq_u16(mc, mk90_1));
1359 const uint16x8_t mk135_0 = vld1q_u16(magnitude - stride_mag + 1);
1360 const uint16x8_t mk135_1 = vld1q_u16(magnitude + stride_mag - 1);
1361 uint16x8_t mask3 = vceqq_u16(pc16, vdupq_n_u16(3));
1362 mask3 = vandq_u16(mask3, vcgeq_u16(mc, mk135_0));
1363 mask3 = vandq_u16(mask3, vcgeq_u16(mc, mk135_1));
1366 mask0 = vorrq_u16(mask0, mask1);
1367 mask2 = vorrq_u16(mask2, mask3);
1368 mask0 = vorrq_u16(mask0, mask2);
1370 mc = vbslq_u16(mask0, mc, vdupq_n_u16(0));
1373 mask0 = vcgtq_u16(mc, vdupq_n_u16(upper_thr));
1376 mask1 = vcleq_u16(mc, vdupq_n_u16(lower_thr));
1378 // mc <= upper_thr && mc > lower_thr
1379 mask2 = vcleq_u16(mc, vdupq_n_u16(upper_thr));
1380 mask2 = vandq_u16(mask2, vcgtq_u16(mc, vdupq_n_u16(lower_thr)));
1382 mc = vbslq_u16(mask0, vdupq_n_u16(EDGE), mc);
1383 mc = vbslq_u16(mask1, vdupq_n_u16(NO_EDGE), mc);
1384 mc = vbslq_u16(mask2, vdupq_n_u16(MAYBE), mc);
1386 vst1_u8(output, vmovn_u16(mc));
1389 inline uint16x4_t non_max_U32_helper(const uint32_t *input, const uint16x4_t pc, const uint32_t stride_mag, const int32_t lower_thr, const int32_t upper_thr)
1391 // Phase for 4 pixel
1392 const uint32x4_t pc32 = vmovl_u16(pc);
1394 // Get magnitude for 4 pixel
1395 uint32x4_t mc = vld1q_u32(input);
1397 // Angle_quantized: 0 = 0°, 1 = 45°, 2 = 90°, 3 = 135°
1399 const uint32x4_t mk0_0 = vld1q_u32(input - 1);
1400 const uint32x4_t mk0_1 = vld1q_u32(input + 1);
1401 uint32x4_t mask0 = vceqq_u32(pc32, vdupq_n_u32(0));
1402 mask0 = vandq_u32(mask0, vcgeq_u32(mc, mk0_0));
1403 mask0 = vandq_u32(mask0, vcgeq_u32(mc, mk0_1));
1406 const uint32x4_t mk45_0 = vld1q_u32(input - stride_mag - 1);
1407 const uint32x4_t mk45_1 = vld1q_u32(input + stride_mag + 1);
1408 uint32x4_t mask1 = vceqq_u32(pc32, vdupq_n_u32(1));
1409 mask1 = vandq_u32(mask1, vcgeq_u32(mc, mk45_0));
1410 mask1 = vandq_u32(mask1, vcgeq_u32(mc, mk45_1));
1413 const uint32x4_t mk90_0 = vld1q_u32(input - stride_mag);
1414 const uint32x4_t mk90_1 = vld1q_u32(input + stride_mag);
1415 uint32x4_t mask2 = vceqq_u32(pc32, vdupq_n_u32(2));
1416 mask2 = vandq_u32(mask2, vcgeq_u32(mc, mk90_0));
1417 mask2 = vandq_u32(mask2, vcgeq_u32(mc, mk90_1));
1420 const uint32x4_t mk135_0 = vld1q_u32(input - stride_mag + 1);
1421 const uint32x4_t mk135_1 = vld1q_u32(input + stride_mag - 1);
1422 uint32x4_t mask3 = vceqq_u32(pc32, vdupq_n_u32(3));
1423 mask3 = vandq_u32(mask3, vcgeq_u32(mc, mk135_0));
1424 mask3 = vandq_u32(mask3, vcgeq_u32(mc, mk135_1));
1427 mask0 = vorrq_u32(mask0, mask1);
1428 mask2 = vorrq_u32(mask2, mask3);
1429 mask0 = vorrq_u32(mask0, mask2);
1431 mc = vbslq_u32(mask0, mc, vdupq_n_u32(0));
1434 mask0 = vcgtq_u32(mc, vdupq_n_u32(upper_thr));
1437 mask1 = vcleq_u32(mc, vdupq_n_u32(lower_thr));
1439 // mc <= upper_thr && mc > lower_thr
1440 mask2 = vcleq_u32(mc, vdupq_n_u32(upper_thr));
1441 mask2 = vandq_u32(mask2, vcgtq_u32(mc, vdupq_n_u32(lower_thr)));
1443 mc = vbslq_u32(mask0, vdupq_n_u32(EDGE), mc);
1444 mc = vbslq_u32(mask1, vdupq_n_u32(NO_EDGE), mc);
1445 mc = vbslq_u32(mask2, vdupq_n_u32(MAYBE), mc);
1447 return vmovn_u32(mc);
1450 /* Computes non-maxima suppression and hysteresis when the gradient_size = 7
1452 * @param[in] magnitude_ptr Pointer to source image. Magnitude. Data type supported U32
1453 * @param[in] phase_ptr Pointer to source image. Quantized phase. Data type supported U8
1454 * @param[out] output_ptr Pointer to destination image. Data type supported U8
1455 * @param[in] stride_mag Stride of magnitude image
1456 * @param[in] lower_thr Lower threshold used for the hysteresis
1457 * @param[in] upper_thr Upper threshold used for the hysteresis
1459 void non_max_suppression_U32_U8_U8(const void *__restrict magnitude_ptr, const void *__restrict phase_ptr, void *__restrict output_ptr, const uint32_t stride_mag, const int32_t lower_thr,
1460 const int32_t upper_thr)
1462 const auto magnitude = static_cast<const uint32_t *__restrict>(magnitude_ptr);
1463 const auto phase = static_cast<const uint8_t *__restrict>(phase_ptr);
1464 const auto output = static_cast<uint8_t *__restrict>(output_ptr);
1466 // Get phase for 8 pixel
1467 const uint16x8_t pc16 = vmovl_u8(vld1_u8(phase));
1469 // Compute non maxima suppression
1470 const uint16x4x2_t res =
1473 non_max_U32_helper(magnitude, vget_low_u16(pc16), stride_mag, lower_thr, upper_thr),
1474 non_max_U32_helper(magnitude + 4, vget_high_u16(pc16), stride_mag, lower_thr, upper_thr)
1479 vst1_u8(output, vmovn_u16(vcombine_u16(res.val[0], res.val[1])));
1482 /* Computes edge tracing when is called by edge_trace_U8_U8 recursively
1484 * @param[in] input Pointer to source image. Data type supported U8
1485 * @param[out] output Pointer to destination image. Data type supported U8
1486 * @param[in] input_stride Stride of the input image
1487 * @param[in] output_stride Stride of the output image
1489 void edge_trace_recursive_U8_U8(uint8_t *__restrict input, uint8_t *__restrict output, const int32_t input_stride, const int32_t output_stride)
1491 // Look for MAYBE pixels in 8 directions
1495 uint8_t pixel = *(input - 1);
1499 // Touched a MAYBE point. MAYBE becomes EDGE
1500 *(input - 1) = EDGE;
1502 edge_trace_recursive_U8_U8(input - 1, output - 1, input_stride, output_stride);
1506 pixel = *(input + 1);
1510 // Touched a MAYBE point. MAYBE becomes EDGE
1511 *(input + 1) = EDGE;
1513 edge_trace_recursive_U8_U8(input + 1, output + 1, input_stride, output_stride);
1516 input -= input_stride;
1517 output -= output_stride;
1520 pixel = *(input - 1);
1524 // Touched a MAYBE point. MAYBE becomes EDGE
1525 *(input - 1) = EDGE;
1527 edge_trace_recursive_U8_U8(input - 1, output - 1, input_stride, output_stride);
1535 // Touched a MAYBE point. MAYBE becomes EDGE
1538 edge_trace_recursive_U8_U8(input, output, input_stride, output_stride);
1542 pixel = *(input + 1);
1546 // Touched a MAYBE point. MAYBE becomes EDGE
1547 *(input + 1) = EDGE;
1549 edge_trace_recursive_U8_U8(input + 1, output + 1, input_stride, output_stride);
1552 input += input_stride * 2;
1553 output += output_stride * 2;
1556 pixel = *(input - 1);
1560 // Touched a MAYBE point. MAYBE becomes EDGE
1561 *(input - 1) = EDGE;
1563 edge_trace_recursive_U8_U8(input - 1, output - 1, input_stride, output_stride);
1571 // Touched a MAYBE point. MAYBE becomes EDGE
1574 edge_trace_recursive_U8_U8(input, output, input_stride, output_stride);
1578 pixel = *(input + 1);
1582 // Touched a MAYBE point. MAYBE becomes EDGE
1583 *(input + 1) = EDGE;
1585 edge_trace_recursive_U8_U8(input + 1, output + 1, input_stride, output_stride);
1589 /* Computes edge tracing
1591 * @param[in] input Pointer to source image. Data type supported U8
1592 * @param[out] output Pointer to destination image. Data type supported U8
1593 * @param[in] input_stride Stride of the input image
1594 * @param[in] output_stride Stride of the output image
1596 void edge_trace_U8_U8(uint8_t *__restrict input, uint8_t *__restrict output, const int32_t input_stride, const int32_t output_stride)
1598 if(*input == NO_EDGE)
1602 // Check if EDGE and not yet touched
1603 else if((*input == EDGE) && (*output == NO_EDGE))
1605 edge_trace_recursive_U8_U8(input, output, input_stride, output_stride);
1610 NEGradientKernel::NEGradientKernel()
1611 : _func(nullptr), _gx(nullptr), _gy(nullptr), _magnitude(nullptr), _phase(nullptr)
1615 void NEGradientKernel::configure(const ITensor *gx, const ITensor *gy, ITensor *magnitude, ITensor *phase, int32_t norm_type)
1617 ARM_COMPUTE_ERROR_ON_NULLPTR(gx, gy, magnitude, phase);
1619 set_shape_if_empty(*magnitude->info(), gx->info()->tensor_shape());
1620 set_shape_if_empty(*phase->info(), gx->info()->tensor_shape());
1622 Format magnitude_format = gx->info()->data_type() == DataType::S16 ? Format::U16 : Format::U32;
1623 set_format_if_unknown(*magnitude->info(), magnitude_format);
1624 set_format_if_unknown(*phase->info(), Format::U8);
1626 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(gx, gy, magnitude, phase);
1627 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(gx, 1, DataType::S16, DataType::S32);
1628 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(gy, 1, DataType::S16, DataType::S32);
1629 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(magnitude, 1, DataType::U16, DataType::U32);
1630 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(phase, 1, DataType::U8);
1631 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(gx, gy);
1632 ARM_COMPUTE_ERROR_ON_MSG(element_size_from_data_type(gx->info()->data_type()) != element_size_from_data_type(magnitude->info()->data_type()), "Magnitude must have the same element size as Gx and Gy");
1636 _magnitude = magnitude;
1639 if(_gx->info()->data_type() == DataType::S16)
1643 _func = &mag_phase_l1norm_S16_S16_U16_U8;
1647 _func = &mag_phase_l2norm_S16_S16_U16_U8;
1654 _func = &mag_phase_l1norm_S32_S32_U32_U8;
1658 _func = &mag_phase_l2norm_S32_S32_U32_U8;
1662 constexpr unsigned int num_elems_processed_per_iteration = 32;
1664 // Configure kernel window
1665 Window win = calculate_max_window(*_gx->info(), Steps(num_elems_processed_per_iteration));
1667 AccessWindowHorizontal gx_access(_gx->info(), 0, num_elems_processed_per_iteration);
1668 AccessWindowHorizontal gy_access(_gy->info(), 0, num_elems_processed_per_iteration);
1669 AccessWindowHorizontal mag_access(_magnitude->info(), 0, num_elems_processed_per_iteration);
1670 AccessWindowHorizontal phase_access(_phase->info(), 0, num_elems_processed_per_iteration);
1672 update_window_and_padding(win, gx_access, gy_access, mag_access, phase_access);
1674 mag_access.set_valid_region(win, _gx->info()->valid_region());
1675 phase_access.set_valid_region(win, _gx->info()->valid_region());
1677 INEKernel::configure(win);
1680 void NEGradientKernel::run(const Window &window, const ThreadInfo &info)
1682 ARM_COMPUTE_UNUSED(info);
1683 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
1684 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
1685 ARM_COMPUTE_ERROR_ON(_func == nullptr);
1686 Iterator gx(_gx, window);
1687 Iterator gy(_gy, window);
1688 Iterator magnitude(_magnitude, window);
1689 Iterator phase(_phase, window);
1691 execute_window_loop(window, [&](const Coordinates & id)
1693 (*_func)(gx.ptr(), gy.ptr(), magnitude.ptr(), phase.ptr());
1695 gx, gy, magnitude, phase);
1698 NEEdgeNonMaxSuppressionKernel::NEEdgeNonMaxSuppressionKernel()
1699 : _func(nullptr), _magnitude(nullptr), _phase(nullptr), _output(nullptr), _lower_thr(0), _upper_thr(0)
1703 BorderSize NEEdgeNonMaxSuppressionKernel::border_size() const
1705 return BorderSize(1);
1708 void NEEdgeNonMaxSuppressionKernel::configure(const ITensor *magnitude, const ITensor *phase, ITensor *output,
1709 int32_t upper_thr, int32_t lower_thr, bool border_undefined)
1711 ARM_COMPUTE_ERROR_ON_NULLPTR(magnitude, phase, output);
1713 set_shape_if_empty(*output->info(), magnitude->info()->tensor_shape());
1715 set_format_if_unknown(*phase->info(), Format::U8);
1716 set_format_if_unknown(*output->info(), Format::U8);
1718 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(magnitude, phase, output);
1719 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(magnitude, 1, DataType::U16, DataType::U32);
1720 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(phase, 1, DataType::U8);
1721 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
1722 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(phase, output);
1724 _magnitude = magnitude;
1728 switch(_magnitude->info()->data_type())
1731 _func = &non_max_suppression_U16_U8_U8;
1734 _func = &non_max_suppression_U32_U8_U8;
1737 ARM_COMPUTE_ERROR("Unsupported data type!");
1741 _lower_thr = lower_thr;
1742 _upper_thr = upper_thr;
1744 constexpr unsigned int num_elems_processed_per_iteration = 8;
1745 constexpr unsigned int num_elems_read_per_iteration = 10;
1746 constexpr unsigned int num_rows_read_per_iteration = 3;
1748 // Configure kernel window
1749 Window win = calculate_max_window(*_magnitude->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
1751 AccessWindowRectangle mag_access(_magnitude->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration);
1752 AccessWindowHorizontal phase_access(_phase->info(), 0, num_elems_processed_per_iteration);
1753 AccessWindowHorizontal output_access(_output->info(), 0, num_elems_processed_per_iteration);
1755 update_window_and_padding(win, mag_access, phase_access, output_access);
1757 output_access.set_valid_region(win, _magnitude->info()->valid_region(), border_undefined, border_size());
1759 INEKernel::configure(win);
1762 void NEEdgeNonMaxSuppressionKernel::run(const Window &window, const ThreadInfo &info)
1764 ARM_COMPUTE_UNUSED(info);
1765 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
1766 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
1767 ARM_COMPUTE_ERROR_ON(_func == nullptr);
1768 Iterator magnitude(_magnitude, window);
1769 Iterator phase(_phase, window);
1770 Iterator output(_output, window);
1772 const size_t input1_stride = _magnitude->info()->strides_in_bytes()[1];
1773 const size_t input1_stride_ushort = input1_stride / data_size_from_type(_magnitude->info()->data_type());
1775 execute_window_loop(window, [&](const Coordinates & id)
1777 (*_func)(magnitude.ptr(), phase.ptr(), output.ptr(), input1_stride_ushort, _lower_thr, _upper_thr);
1779 magnitude, phase, output);
1782 NEEdgeTraceKernel::NEEdgeTraceKernel()
1783 : _input(nullptr), _output(nullptr)
1787 BorderSize NEEdgeTraceKernel::border_size() const
1789 return BorderSize(1);
1792 bool NEEdgeTraceKernel::is_parallelisable() const
1797 void NEEdgeTraceKernel::configure(ITensor *input, ITensor *output)
1799 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
1801 set_shape_if_empty(*output->info(), input->info()->tensor_shape());
1803 set_format_if_unknown(*input->info(), Format::U8);
1804 set_format_if_unknown(*output->info(), Format::U8);
1806 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output);
1807 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
1808 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
1809 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
1814 constexpr unsigned int num_elems_processed_per_iteration = 1;
1816 // Configure kernel window
1817 Window win = calculate_max_window(*_input->info(), Steps(num_elems_processed_per_iteration));
1819 const ValidRegion &input_valid_region = input->info()->valid_region();
1820 const ValidRegion &output_valid_region = output->info()->valid_region();
1822 // Reads can occur within the valid region of the input + border
1823 AccessWindowStatic input_access(input->info(),
1824 input_valid_region.anchor[0] - border_size().left,
1825 input_valid_region.anchor[1] - border_size().top,
1826 input_valid_region.anchor[0] + input_valid_region.shape[0] + border_size().right,
1827 input_valid_region.anchor[1] + input_valid_region.shape[1] + border_size().bottom);
1829 // Writes can occur within the valid region of the output + border
1830 AccessWindowStatic output_access(output->info(),
1831 output_valid_region.anchor[0] - border_size().left,
1832 output_valid_region.anchor[1] - border_size().top,
1833 output_valid_region.anchor[0] + output_valid_region.shape[0] + border_size().right,
1834 output_valid_region.anchor[1] + output_valid_region.shape[1] + border_size().bottom);
1836 update_window_and_padding(win, input_access, output_access);
1838 output_access.set_valid_region(win, _input->info()->valid_region());
1840 INEKernel::configure(win);
1843 void NEEdgeTraceKernel::run(const Window &window, const ThreadInfo &info)
1845 ARM_COMPUTE_UNUSED(info);
1846 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
1847 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
1848 Iterator input(_input, window);
1849 Iterator output(_output, window);
1851 const size_t input_stride = _input->info()->strides_in_bytes()[1];
1852 const size_t output_stride = _output->info()->strides_in_bytes()[1];
1854 execute_window_loop(window, [&](const Coordinates & id)
1856 edge_trace_U8_U8(input.ptr(), output.ptr(), input_stride, output_stride);