blob: 94e5e230f978552cd36bb64576950112edbd571e [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Anthony Barbierb6eb3532018-08-08 13:20:04 +01002 * Copyright (c) 2017-2018 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/CL/kernels/CLCannyEdgeKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/CL/OpenCL.h"
30#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/Validate.h"
32
33using namespace arm_compute;
34
35CLGradientKernel::CLGradientKernel()
36 : _gx(nullptr), _gy(nullptr), _magnitude(nullptr), _phase(nullptr)
37{
38}
39
40void CLGradientKernel::configure(const ICLTensor *gx, const ICLTensor *gy, ICLTensor *magnitude, ICLTensor *phase, int32_t norm_type)
41{
42 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(gx, 1, DataType::S16, DataType::S32);
43 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(gy, 1, DataType::S16, DataType::S32);
44 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(magnitude, 1, DataType::U16, DataType::U32);
45 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(phase, 1, DataType::U8);
46 ARM_COMPUTE_ERROR_ON_MSG(data_size_from_type(gx->info()->data_type()) != data_size_from_type(gy->info()->data_type()),
47 "Gx and Gy must have the same pixel size");
48 ARM_COMPUTE_ERROR_ON_MSG(data_size_from_type(gx->info()->data_type()) != data_size_from_type(magnitude->info()->data_type()),
49 "Mag must have the same pixel size as Gx and Gy");
50
51 _gx = gx;
52 _gy = gy;
53 _magnitude = magnitude;
54 _phase = phase;
55
56 // Create build opts
57 std::set<std::string> built_opts;
58 built_opts.emplace("-DDATA_TYPE_IN=" + get_cl_type_from_data_type(gx->info()->data_type()));
59 built_opts.emplace("-DDATA_TYPE_OUT=" + get_cl_type_from_data_type(gx->info()->data_type()));
60
61 // Create kernel
62 const std::string kernel_name = (norm_type == 1) ? std::string("combine_gradients_L1") : std::string("combine_gradients_L2");
63 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, built_opts));
64
65 // Configure kernel window
66 constexpr unsigned int num_elems_processed_per_iteration = 4;
67
68 Window win = calculate_max_window(*_gx->info(), Steps(num_elems_processed_per_iteration));
69
70 AccessWindowHorizontal gx_access(_gx->info(), 0, num_elems_processed_per_iteration);
71 AccessWindowHorizontal gy_access(_gy->info(), 0, num_elems_processed_per_iteration);
72 AccessWindowHorizontal mag_access(_magnitude->info(), 0, num_elems_processed_per_iteration);
73 AccessWindowHorizontal phase_access(_phase->info(), 0, num_elems_processed_per_iteration);
74
75 update_window_and_padding(win, gx_access, gy_access, mag_access, phase_access);
76
77 mag_access.set_valid_region(win, _gx->info()->valid_region());
78 phase_access.set_valid_region(win, _gx->info()->valid_region());
79
Anthony Barbierb6eb3532018-08-08 13:20:04 +010080 ICLKernel::configure_internal(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010081}
82
83void CLGradientKernel::run(const Window &window, cl::CommandQueue &queue)
84{
85 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
86 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
87
88 Window slice = window.first_slice_window_2D();
89 do
90 {
91 unsigned int idx = 0;
92 add_2D_tensor_argument(idx, _gx, slice);
93 add_2D_tensor_argument(idx, _gy, slice);
94 add_2D_tensor_argument(idx, _magnitude, slice);
95 add_2D_tensor_argument(idx, _phase, slice);
96 enqueue(queue, *this, slice);
97 }
98 while(window.slide_window_slice_2D(slice));
99}
100
101CLEdgeNonMaxSuppressionKernel::CLEdgeNonMaxSuppressionKernel()
102 : _magnitude(nullptr), _phase(nullptr), _output(nullptr)
103{
104}
105
106BorderSize CLEdgeNonMaxSuppressionKernel::border_size() const
107{
108 return BorderSize(1);
109}
110
111void CLEdgeNonMaxSuppressionKernel::configure(const ICLTensor *magnitude, const ICLTensor *phase, ICLTensor *output, int32_t lower_thr, bool border_undefined)
112{
113 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(magnitude, 1, DataType::U16, DataType::U32);
114 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(phase, 1, DataType::U8);
115 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U16, DataType::U32);
116
117 _magnitude = magnitude;
118 _phase = phase;
119 _output = output;
120
121 // Create build opts
122 std::set<std::string> built_opts;
123 built_opts.emplace("-DDATA_TYPE_IN=" + get_cl_type_from_data_type(magnitude->info()->data_type()));
124 built_opts.emplace("-DDATA_TYPE_OUT=" + get_cl_type_from_data_type(output->info()->data_type()));
125
126 // Create kernel
127 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("suppress_non_maximum", built_opts));
128
129 // Set minimum threshold argument
130 unsigned int idx = 3 * num_arguments_per_2D_tensor(); //Skip the input and output parameters
131 _kernel.setArg(idx++, lower_thr);
132
133 // Configure kernel window
134 constexpr unsigned int num_elems_processed_per_iteration = 1;
135 constexpr unsigned int num_elems_read_written_per_iteration = 3;
136
137 Window win = calculate_max_window(*_magnitude->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
138
139 AccessWindowRectangle mag_access(_magnitude->info(), -border_size().left, -border_size().top,
140 num_elems_read_written_per_iteration, num_elems_read_written_per_iteration);
141 AccessWindowHorizontal phase_access(_phase->info(), 0, num_elems_processed_per_iteration);
142 AccessWindowHorizontal output_access(_output->info(), 0, num_elems_processed_per_iteration);
143
144 update_window_and_padding(win, mag_access, phase_access, output_access);
145
146 output_access.set_valid_region(win, _magnitude->info()->valid_region(), border_undefined, border_size());
147
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100148 ICLKernel::configure_internal(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149}
150
151void CLEdgeNonMaxSuppressionKernel::run(const Window &window, cl::CommandQueue &queue)
152{
153 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
154 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
155
156 Window slice = window.first_slice_window_2D();
157 do
158 {
159 unsigned int idx = 0;
160 add_2D_tensor_argument(idx, _magnitude, slice);
161 add_2D_tensor_argument(idx, _phase, slice);
162 add_2D_tensor_argument(idx, _output, slice);
163 enqueue(queue, *this, slice);
164 }
165 while(window.slide_window_slice_2D(slice));
166}
167
168CLEdgeTraceKernel::CLEdgeTraceKernel()
169 : _input(nullptr), _output(nullptr), _lower_thr(0), _upper_thr(0), _visited(nullptr), _recorded(nullptr), _l1_stack(nullptr), _l1_stack_counter(nullptr)
170{
171}
172
173void CLEdgeTraceKernel::configure(const ICLTensor *input, ICLTensor *output, int32_t upper_thr, int32_t lower_thr,
174 ICLTensor *visited, ICLTensor *recorded, ICLTensor *l1_stack, ICLTensor *l1_stack_counter)
175{
176 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U16, DataType::U32);
177 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
178 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(visited, 1, DataType::U32);
179 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(recorded, 1, DataType::U32);
180 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(l1_stack, 1, DataType::S32);
181 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(l1_stack_counter, 1, DataType::U8);
182
183 _input = input;
184 _output = output;
185 _lower_thr = lower_thr;
186 _upper_thr = upper_thr;
187 _visited = visited;
188 _recorded = recorded;
189 _l1_stack = l1_stack;
190 _l1_stack_counter = l1_stack_counter;
191
192 // Create build opts
193 std::set<std::string> built_opts;
194 built_opts.emplace("-DDATA_TYPE_IN=" + get_cl_type_from_data_type(input->info()->data_type()));
195 built_opts.emplace("-DDATA_TYPE_OUT=" + get_cl_type_from_data_type(output->info()->data_type()));
196
197 // Create kernel
198 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("hysteresis", built_opts));
199
200 // Set constant kernel args
201 unsigned int width = _input->info()->dimension(0);
202 unsigned int height = _input->info()->dimension(1);
203 unsigned int idx = 6 * num_arguments_per_2D_tensor(); //Skip the input and output parameters
204 _kernel.setArg(idx++, static_cast<cl_uint>(_lower_thr));
205 _kernel.setArg(idx++, static_cast<cl_uint>(_upper_thr));
206 _kernel.setArg(idx++, static_cast<cl_uint>(width));
207 _kernel.setArg(idx++, static_cast<cl_uint>(height));
208
209 // Configure kernel window
210 constexpr unsigned int num_elems_processed_per_iteration = 1;
211 Window win = calculate_max_window(*_input->info(), Steps(num_elems_processed_per_iteration));
212
213 AccessWindowHorizontal output_access(_output->info(), 0, num_elems_processed_per_iteration);
214 AccessWindowHorizontal visited_access(_visited->info(), 0, num_elems_processed_per_iteration);
215 AccessWindowHorizontal recorded_access(_recorded->info(), 0, num_elems_processed_per_iteration);
216 AccessWindowHorizontal l1_stack_access(_l1_stack->info(), 0, num_elems_processed_per_iteration);
217 AccessWindowHorizontal l1_stack_counter_access(_l1_stack_counter->info(), 0, num_elems_processed_per_iteration);
218
219 update_window_and_padding(win,
220 AccessWindowHorizontal(_input->info(), 0, num_elems_processed_per_iteration),
221 output_access,
222 visited_access,
223 recorded_access,
224 l1_stack_access,
225 l1_stack_counter_access);
226
227 output_access.set_valid_region(win, _input->info()->valid_region());
228 visited_access.set_valid_region(win, _input->info()->valid_region());
229 recorded_access.set_valid_region(win, _input->info()->valid_region());
230 l1_stack_access.set_valid_region(win, _input->info()->valid_region());
231 l1_stack_counter_access.set_valid_region(win, _input->info()->valid_region());
232
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100233 ICLKernel::configure_internal(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100234}
235
236void CLEdgeTraceKernel::run(const Window &window, cl::CommandQueue &queue)
237{
238 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
239 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
240
241 Window slice = window.first_slice_window_2D();
242 do
243 {
244 unsigned int idx = 0;
245 add_2D_tensor_argument(idx, _input, slice);
246 add_2D_tensor_argument(idx, _output, slice);
247 add_2D_tensor_argument(idx, _visited, slice);
248 add_2D_tensor_argument(idx, _recorded, slice);
249 add_2D_tensor_argument(idx, _l1_stack, slice);
250 add_2D_tensor_argument(idx, _l1_stack_counter, slice);
251
252 enqueue(queue, *this, slice);
253 }
254 while(window.slide_window_slice_2D(slice));
255}