blob: 5acb8e7ddb799dfeb6a328105c3b72e518f238a6 [file] [log] [blame]
Anthony Barbier871448e2017-03-24 14:54:29 +00001/*
2 * Copyright (c) 2017 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/CL/functions/CLCannyEdge.h"
25
26#include "arm_compute/core/CL/ICLTensor.h"
27#include "arm_compute/core/CL/OpenCL.h"
28#include "arm_compute/core/Error.h"
Anthony Barbier871448e2017-03-24 14:54:29 +000029#include "arm_compute/core/Validate.h"
30#include "arm_compute/runtime/CL/CLScheduler.h"
31#include "arm_compute/runtime/CL/functions/CLSobel3x3.h"
32#include "arm_compute/runtime/CL/functions/CLSobel5x5.h"
33#include "arm_compute/runtime/CL/functions/CLSobel7x7.h"
Kaizen8938bd32017-09-28 14:38:23 +010034#include "support/ToolchainSupport.h"
Anthony Barbier871448e2017-03-24 14:54:29 +000035
36using namespace arm_compute;
37
Kaizen8938bd32017-09-28 14:38:23 +010038CLCannyEdge::CLCannyEdge(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT
39 : _memory_group(std::move(memory_manager)),
40 _sobel(),
41 _gradient(),
42 _border_mag_gradient(),
43 _non_max_suppr(),
44 _edge_trace(),
45 _gx(),
46 _gy(),
47 _mag(),
48 _phase(),
49 _nonmax(),
50 _visited(),
51 _recorded(),
52 _l1_list_counter(),
53 _l1_stack()
Anthony Barbier871448e2017-03-24 14:54:29 +000054{
55}
56
57void CLCannyEdge::configure(ICLTensor *input, ICLTensor *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)
58{
59 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
60 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
61 ARM_COMPUTE_ERROR_ON((1 != norm_type) && (2 != norm_type));
62 ARM_COMPUTE_ERROR_ON(lower_thr > upper_thr);
63
64 const unsigned int L1_hysteresis_stack_size = 8;
Anthony Barbier871448e2017-03-24 14:54:29 +000065 const TensorShape shape = input->info()->tensor_shape();
66
67 TensorInfo gradient_info;
68 TensorInfo info;
69
70 // Initialize images
71 if(gradient_size < 7)
72 {
Anthony Barbiera4376382017-04-12 15:12:46 +010073 gradient_info.init(shape, 1, arm_compute::DataType::S16);
74 info.init(shape, 1, arm_compute::DataType::U16);
Anthony Barbier871448e2017-03-24 14:54:29 +000075 }
76 else
77 {
Anthony Barbiera4376382017-04-12 15:12:46 +010078 gradient_info.init(shape, 1, arm_compute::DataType::S32);
79 info.init(shape, 1, arm_compute::DataType::U32);
Anthony Barbier871448e2017-03-24 14:54:29 +000080 }
81
82 _gx.allocator()->init(gradient_info);
Anthony Barbier871448e2017-03-24 14:54:29 +000083 _gy.allocator()->init(gradient_info);
Anthony Barbier871448e2017-03-24 14:54:29 +000084 _mag.allocator()->init(info);
Anthony Barbier871448e2017-03-24 14:54:29 +000085 _nonmax.allocator()->init(info);
Anthony Barbier871448e2017-03-24 14:54:29 +000086
87 TensorInfo info_u8(shape, 1, arm_compute::DataType::U8);
Anthony Barbier871448e2017-03-24 14:54:29 +000088 _phase.allocator()->init(info_u8);
Anthony Barbier871448e2017-03-24 14:54:29 +000089 _l1_list_counter.allocator()->init(info_u8);
Anthony Barbier871448e2017-03-24 14:54:29 +000090
91 TensorInfo info_u32(shape, 1, arm_compute::DataType::U32);
Anthony Barbier871448e2017-03-24 14:54:29 +000092 _visited.allocator()->init(info_u32);
Anthony Barbier871448e2017-03-24 14:54:29 +000093 _recorded.allocator()->init(info_u32);
Anthony Barbier871448e2017-03-24 14:54:29 +000094
95 TensorShape shape_l1_stack = input->info()->tensor_shape();
96 shape_l1_stack.set(0, input->info()->dimension(0) * L1_hysteresis_stack_size);
97 TensorInfo info_s32(shape_l1_stack, 1, arm_compute::DataType::S32);
Anthony Barbier871448e2017-03-24 14:54:29 +000098 _l1_stack.allocator()->init(info_s32);
Anthony Barbier871448e2017-03-24 14:54:29 +000099
Kaizen8938bd32017-09-28 14:38:23 +0100100 // Manage intermediate buffers
101 _memory_group.manage(&_gx);
102 _memory_group.manage(&_gy);
103
Anthony Barbier871448e2017-03-24 14:54:29 +0000104 // Configure/Init sobelNxN
105 if(gradient_size == 3)
106 {
Kaizen8938bd32017-09-28 14:38:23 +0100107 auto k = arm_compute::support::cpp14::make_unique<CLSobel3x3>();
Anthony Barbier871448e2017-03-24 14:54:29 +0000108 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
109 _sobel = std::move(k);
110 }
111 else if(gradient_size == 5)
112 {
Kaizen8938bd32017-09-28 14:38:23 +0100113 auto k = arm_compute::support::cpp14::make_unique<CLSobel5x5>();
Anthony Barbier871448e2017-03-24 14:54:29 +0000114 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
115 _sobel = std::move(k);
116 }
117 else if(gradient_size == 7)
118 {
Kaizen8938bd32017-09-28 14:38:23 +0100119 auto k = arm_compute::support::cpp14::make_unique<CLSobel7x7>();
Anthony Barbier871448e2017-03-24 14:54:29 +0000120 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
121 _sobel = std::move(k);
122 }
123 else
124 {
125 ARM_COMPUTE_ERROR("Gradient %d size not supported", gradient_size);
126 }
127
Kaizen8938bd32017-09-28 14:38:23 +0100128 // Manage intermediate buffers
129 _memory_group.manage(&_mag);
130 _memory_group.manage(&_phase);
131
Anthony Barbier871448e2017-03-24 14:54:29 +0000132 // Configure gradient
Anthony Barbiera4376382017-04-12 15:12:46 +0100133 _gradient.configure(&_gx, &_gy, &_mag, &_phase, norm_type);
Anthony Barbier871448e2017-03-24 14:54:29 +0000134
Kaizen8938bd32017-09-28 14:38:23 +0100135 // Allocate intermediate buffers
136 _gx.allocator()->allocate();
137 _gy.allocator()->allocate();
138
139 // Manage intermediate buffers
140 _memory_group.manage(&_nonmax);
141
Anthony Barbier871448e2017-03-24 14:54:29 +0000142 // Configure non-maxima suppression
Anthony Barbiera4376382017-04-12 15:12:46 +0100143 _non_max_suppr.configure(&_mag, &_phase, &_nonmax, lower_thr, border_mode == BorderMode::UNDEFINED);
144
Kaizen8938bd32017-09-28 14:38:23 +0100145 // Allocate intermediate buffers
146 _phase.allocator()->allocate();
147
Anthony Barbiera4376382017-04-12 15:12:46 +0100148 // Fill border around magnitude image as non-maxima suppression will access
149 // it. If border mode is undefined filling the border is a nop.
150 _border_mag_gradient.configure(&_mag, _non_max_suppr.border_size(), border_mode, constant_border_value);
Anthony Barbier871448e2017-03-24 14:54:29 +0000151
Kaizen8938bd32017-09-28 14:38:23 +0100152 // Allocate intermediate buffers
153 _mag.allocator()->allocate();
154
155 // Manage intermediate buffers
156 _memory_group.manage(&_visited);
157 _memory_group.manage(&_recorded);
158 _memory_group.manage(&_l1_stack);
159 _memory_group.manage(&_l1_list_counter);
160
Anthony Barbier871448e2017-03-24 14:54:29 +0000161 // Configure edge tracing
Anthony Barbiera4376382017-04-12 15:12:46 +0100162 _edge_trace.configure(&_nonmax, output, upper_thr, lower_thr, &_visited, &_recorded, &_l1_stack, &_l1_list_counter);
163
Kaizen8938bd32017-09-28 14:38:23 +0100164 // Allocate intermediate buffers
Anthony Barbiera4376382017-04-12 15:12:46 +0100165 _visited.allocator()->allocate();
166 _recorded.allocator()->allocate();
167 _l1_stack.allocator()->allocate();
168 _l1_list_counter.allocator()->allocate();
169 _nonmax.allocator()->allocate();
Anthony Barbier871448e2017-03-24 14:54:29 +0000170}
171
172void CLCannyEdge::run()
173{
Kaizen8938bd32017-09-28 14:38:23 +0100174 _memory_group.acquire();
175
Anthony Barbier871448e2017-03-24 14:54:29 +0000176 // Run sobel
177 _sobel->run();
178
179 // Run phase and magnitude calculation
180 CLScheduler::get().enqueue(_gradient, false);
181
Anthony Barbiera4376382017-04-12 15:12:46 +0100182 // Fill border before non-maxima suppression. Nop for border mode undefined.
183 CLScheduler::get().enqueue(_border_mag_gradient, false);
184
Anthony Barbier871448e2017-03-24 14:54:29 +0000185 // Run non max suppresion
186 _nonmax.clear(CLScheduler::get().queue());
187 CLScheduler::get().enqueue(_non_max_suppr, false);
188
189 // Clear temporary structures and run edge trace
190 _visited.clear(CLScheduler::get().queue());
191 _recorded.clear(CLScheduler::get().queue());
192 _l1_list_counter.clear(CLScheduler::get().queue());
193 _l1_stack.clear(CLScheduler::get().queue());
194 CLScheduler::get().enqueue(_edge_trace, true);
Kaizen8938bd32017-09-28 14:38:23 +0100195
196 _memory_group.release();
Anthony Barbier871448e2017-03-24 14:54:29 +0000197}