1 /*
2 * Copyright (c) 2017-2020 Arm Limited.
3 *
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/runtime/NEON/functions/NECannyEdge.h"
25
26 #include "arm_compute/core/Error.h"
27 #include "arm_compute/core/ITensor.h"
28 #include "arm_compute/core/TensorInfo.h"
29 #include "arm_compute/core/Validate.h"
30 #include "arm_compute/runtime/NEON/NEScheduler.h"
31 #include "arm_compute/runtime/NEON/functions/NESobel3x3.h"
32 #include "arm_compute/runtime/NEON/functions/NESobel5x5.h"
33 #include "arm_compute/runtime/NEON/functions/NESobel7x7.h"
34 #include "arm_compute/runtime/TensorAllocator.h"
35 #include "src/core/NEON/kernels/NECannyEdgeKernel.h"
36 #include "src/core/NEON/kernels/NEFillBorderKernel.h"
37 #include "src/core/NEON/kernels/NESobel5x5Kernel.h"
38 #include "src/core/NEON/kernels/NESobel7x7Kernel.h"
39 #include "support/MemorySupport.h"
40
41 #include <cstring>
42 #include <inttypes.h>
43 #include <utility>
44
45 namespace arm_compute
46 {
47 NECannyEdge::~NECannyEdge() = default;
48
NECannyEdge(std::shared_ptr<IMemoryManager> memory_manager)49 NECannyEdge::NECannyEdge(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT
50 : _memory_group(std::move(memory_manager)),
51 _sobel(),
52 _gradient(),
53 _non_max_suppr(),
54 _edge_trace(),
55 _border_mag_gradient(),
56 _border_edge_trace(),
57 _gx(),
58 _gy(),
59 _magnitude(),
60 _phase(),
61 _nonmax(),
62 _output(nullptr)
63 {
64 }
65
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)66 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)
67 {
68 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
69 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
70 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
71 ARM_COMPUTE_ERROR_ON((1 != norm_type) && (2 != norm_type));
72 ARM_COMPUTE_ERROR_ON((gradient_size != 3) && (gradient_size != 5) && (gradient_size != 7));
73 ARM_COMPUTE_ERROR_ON((lower_thr < 0) || (lower_thr >= upper_thr));
74
75 _output = output;
76
77 const TensorShape &shape = input->info()->tensor_shape();
78 TensorInfo gradient_info;
79 TensorInfo magnitude_info;
80
81 // Initialize images
82 if(gradient_size < 7)
83 {
84 gradient_info.init(shape, Format::S16);
85 magnitude_info.init(shape, Format::U16);
86 }
87 else
88 {
89 gradient_info.init(shape, Format::S32);
90 magnitude_info.init(shape, Format::U32);
91 }
92
93 _gx.allocator()->init(gradient_info);
94 _gy.allocator()->init(gradient_info);
95 _magnitude.allocator()->init(magnitude_info);
96
97 TensorInfo info(shape, Format::U8);
98 _phase.allocator()->init(info);
99 _nonmax.allocator()->init(info);
100
101 // Manage intermediate buffers
102 _memory_group.manage(&_gx);
103 _memory_group.manage(&_gy);
104
105 // Configure/Init sobelNxN
106 if(gradient_size == 3)
107 {
108 auto k = arm_compute::support::cpp14::make_unique<NESobel3x3>();
109 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
110 _sobel = std::move(k);
111 }
112 else if(gradient_size == 5)
113 {
114 auto k = arm_compute::support::cpp14::make_unique<NESobel5x5>();
115 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
116 _sobel = std::move(k);
117 }
118 else if(gradient_size == 7)
119 {
120 auto k = arm_compute::support::cpp14::make_unique<NESobel7x7>();
121 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
122 _sobel = std::move(k);
123 }
124 else
125 {
126 ARM_COMPUTE_ERROR_VAR("Gradient size %+" PRId32 " not supported\n", gradient_size);
127 }
128
129 // Manage intermediate buffers
130 _memory_group.manage(&_magnitude);
131 _memory_group.manage(&_phase);
132
133 // Configure gradient
134 auto k = arm_compute::support::cpp14::make_unique<NEGradientKernel>();
135 k->configure(&_gx, &_gy, &_magnitude, &_phase, norm_type);
136 _gradient = std::move(k);
137
138 // Allocate intermediate tensors
139 _gx.allocator()->allocate();
140 _gy.allocator()->allocate();
141
142 // Manage intermediate buffers
143 _memory_group.manage(&_nonmax);
144
145 // Configure non-maxima suppression
146 _non_max_suppr = arm_compute::support::cpp14::make_unique<NEEdgeNonMaxSuppressionKernel>();
147 _non_max_suppr->configure(&_magnitude, &_phase, &_nonmax, upper_thr, lower_thr, border_mode == BorderMode::UNDEFINED);
148
149 // Fill border around magnitude image as non-maxima suppression will access
150 // it. If border mode is undefined filling the border is a nop.
151 _border_mag_gradient = arm_compute::support::cpp14::make_unique<NEFillBorderKernel>();
152 _border_mag_gradient->configure(&_magnitude, _non_max_suppr->border_size(), border_mode, constant_border_value);
153
154 // Allocate intermediate tensors
155 _phase.allocator()->allocate();
156 _magnitude.allocator()->allocate();
157
158 // Configure edge tracing
159 _edge_trace = arm_compute::support::cpp14::make_unique<NEEdgeTraceKernel>();
160 _edge_trace->configure(&_nonmax, output);
161
162 // Fill border with "No edge" to stop recursion in edge trace
163 _border_edge_trace = arm_compute::support::cpp14::make_unique<NEFillBorderKernel>();
164 _border_edge_trace->configure(&_nonmax, _edge_trace->border_size(), BorderMode::CONSTANT, static_cast<float>(0.f));
165
166 // Allocate intermediate tensors
167 _nonmax.allocator()->allocate();
168 }
169
run()170 void NECannyEdge::run()
171 {
172 ARM_COMPUTE_ERROR_ON_MSG(_sobel == nullptr, "Unconfigured function");
173
174 MemoryGroupResourceScope scope_mg(_memory_group);
175
176 // Run sobelNxN
177 _sobel->run();
178
179 // Run gradient
180 NEScheduler::get().schedule(_gradient.get(), Window::DimY);
181
182 // Fill border before non-maxima suppression. Nop for border mode undefined.
183 NEScheduler::get().schedule(_border_mag_gradient.get(), Window::DimZ);
184
185 // Run non-maxima suppression
186 NEScheduler::get().schedule(_non_max_suppr.get(), Window::DimY);
187
188 ARM_COMPUTE_ERROR_ON(_output->buffer() == nullptr);
189 std::fill_n(_output->buffer(), _output->info()->total_size(), 0);
190
191 // Fill border before edge trace
192 NEScheduler::get().schedule(_border_edge_trace.get(), Window::DimZ);
193
194 // Run edge tracing
195 NEScheduler::get().schedule(_edge_trace.get(), Window::DimY);
196 }
197 } // namespace arm_compute
198