2 * Copyright (c) 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/runtime/NEON/functions/NECannyEdge.h"
26 #include "arm_compute/core/Error.h"
27 #include "arm_compute/core/Helpers.h"
28 #include "arm_compute/core/ITensor.h"
29 #include "arm_compute/core/NEON/kernels/NECannyEdgeKernel.h"
30 #include "arm_compute/core/NEON/kernels/NEFillBorderKernel.h"
31 #include "arm_compute/core/TensorInfo.h"
32 #include "arm_compute/core/Validate.h"
33 #include "arm_compute/runtime/NEON/NEScheduler.h"
34 #include "arm_compute/runtime/NEON/functions/NESobel3x3.h"
35 #include "arm_compute/runtime/NEON/functions/NESobel5x5.h"
36 #include "arm_compute/runtime/NEON/functions/NESobel7x7.h"
37 #include "arm_compute/runtime/TensorAllocator.h"
42 using namespace arm_compute;
44 NECannyEdge::NECannyEdge()
45 : _sobel(), _gradient(), _non_max_suppr(), _edge_trace(), _border_mag_gradient(), _border_edge_trace(), _gx(), _gy(), _magnitude(), _phase(), _nonmax(), _output(nullptr)
49 void NECannyEdge::configure(ITensor *input, ITensor *output, int32_t upper_thr, int32_t lower_thr, int32_t gradient_size, int32_t norm_type, BorderMode border_mode, uint8_t constant_border_value,
52 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
53 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
54 ARM_COMPUTE_ERROR_ON(gradient_size < 3);
55 ARM_COMPUTE_ERROR_ON(gradient_size > 7);
56 ARM_COMPUTE_ERROR_ON(lower_thr > upper_thr);
57 ARM_COMPUTE_ERROR_ON((1 != norm_type) && (2 != norm_type));
61 const TensorShape &shape = input->info()->tensor_shape();
62 TensorInfo gradient_info;
63 TensorInfo magnitude_info;
65 /* Initialize images */
68 gradient_info.init(shape, Format::S16);
69 magnitude_info.init(shape, Format::U16);
73 gradient_info.init(shape, Format::S32);
74 magnitude_info.init(shape, Format::U32);
77 _gx.allocator()->init(gradient_info);
78 _gy.allocator()->init(gradient_info);
79 _magnitude.allocator()->init(magnitude_info);
81 TensorInfo info(shape, Format::U8);
82 _phase.allocator()->init(info);
83 _nonmax.allocator()->init(info);
85 /* Configure/Init sobelNxN */
86 if(gradient_size == 3)
88 auto k = arm_compute::cpp14::make_unique<NESobel3x3>();
89 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
90 _sobel = std::move(k);
92 else if(gradient_size == 5)
94 auto k = arm_compute::cpp14::make_unique<NESobel5x5>();
95 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
96 _sobel = std::move(k);
98 else if(gradient_size == 7)
100 auto k = arm_compute::cpp14::make_unique<NESobel7x7>();
101 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
102 _sobel = std::move(k);
106 ARM_COMPUTE_ERROR("Gradient size not supported\n");
109 /* Configure gradient */
112 auto k = arm_compute::cpp14::make_unique<NEGradientFP16Kernel>();
113 k->configure(&_gx, &_gy, &_magnitude, &_phase, norm_type);
114 _gradient = std::move(k);
118 auto k = arm_compute::cpp14::make_unique<NEGradientKernel>();
119 k->configure(&_gx, &_gy, &_magnitude, &_phase, norm_type);
120 _gradient = std::move(k);
123 _gx.allocator()->allocate();
124 _gy.allocator()->allocate();
126 /* Configure non-maxima suppression */
127 _non_max_suppr.configure(&_magnitude, &_phase, &_nonmax, upper_thr, lower_thr, border_mode == BorderMode::UNDEFINED);
129 _phase.allocator()->allocate();
131 if(border_mode != BorderMode::UNDEFINED)
133 /* Configure border filling for magnitude image */
134 _border_mag_gradient.configure(&_magnitude, _non_max_suppr.border_size(), BorderMode::CONSTANT, 0);
137 _magnitude.allocator()->allocate();
139 /* Configure edge tracing */
140 _edge_trace.configure(&_nonmax, output);
142 // Fill border with "No edge" to stop recursion in edge trace
143 _border_edge_trace.configure(&_nonmax, _edge_trace.border_size(), BorderMode::CONSTANT, 0);
145 _nonmax.allocator()->allocate();
148 void NECannyEdge::run()
150 ARM_COMPUTE_ERROR_ON_MSG(_sobel == nullptr, "Unconfigured function");
151 ARM_COMPUTE_ERROR_ON(_output == nullptr);
157 NEScheduler::get().multithread(_gradient.get());
159 /* Fill border before non-maxima suppression */
160 _border_mag_gradient.run(_border_mag_gradient.window());
162 /* Run non-maxima suppression */
163 NEScheduler::get().multithread(&_non_max_suppr);
165 ARM_COMPUTE_ERROR_ON(_output->buffer() == nullptr);
166 memset(_output->buffer(), 0, _output->info()->total_size());
168 // Fill border before edge trace
169 _border_edge_trace.run(_border_edge_trace.window());
171 /* Run edge tracing */
172 _edge_trace.run(_edge_trace.window());