blob: 448ca9289dd62165862dd06fdb0f145fa8d0ff97 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
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 Barbier6ff3b192017-09-04 18:44:23 +010029#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"
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010034#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035
36using namespace arm_compute;
37
Moritz Pflanzerf4af76e2017-09-06 07:42:43 +010038CLCannyEdge::CLCannyEdge() // NOLINT
39 : _sobel(),
40 _gradient(),
41 _border_mag_gradient(),
42 _non_max_suppr(),
43 _edge_trace(),
44 _gx(),
45 _gy(),
46 _mag(),
47 _phase(),
48 _nonmax(),
49 _visited(),
50 _recorded(),
51 _l1_list_counter(),
52 _l1_stack()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053{
54}
55
56void 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)
57{
58 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
59 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
60 ARM_COMPUTE_ERROR_ON((1 != norm_type) && (2 != norm_type));
61 ARM_COMPUTE_ERROR_ON(lower_thr > upper_thr);
62
63 const unsigned int L1_hysteresis_stack_size = 8;
64 const TensorShape shape = input->info()->tensor_shape();
65
66 TensorInfo gradient_info;
67 TensorInfo info;
68
69 // Initialize images
70 if(gradient_size < 7)
71 {
72 gradient_info.init(shape, 1, arm_compute::DataType::S16);
73 info.init(shape, 1, arm_compute::DataType::U16);
74 }
75 else
76 {
77 gradient_info.init(shape, 1, arm_compute::DataType::S32);
78 info.init(shape, 1, arm_compute::DataType::U32);
79 }
80
81 _gx.allocator()->init(gradient_info);
82 _gy.allocator()->init(gradient_info);
83 _mag.allocator()->init(info);
84 _nonmax.allocator()->init(info);
85
86 TensorInfo info_u8(shape, 1, arm_compute::DataType::U8);
87 _phase.allocator()->init(info_u8);
88 _l1_list_counter.allocator()->init(info_u8);
89
90 TensorInfo info_u32(shape, 1, arm_compute::DataType::U32);
91 _visited.allocator()->init(info_u32);
92 _recorded.allocator()->init(info_u32);
93
94 TensorShape shape_l1_stack = input->info()->tensor_shape();
95 shape_l1_stack.set(0, input->info()->dimension(0) * L1_hysteresis_stack_size);
96 TensorInfo info_s32(shape_l1_stack, 1, arm_compute::DataType::S32);
97 _l1_stack.allocator()->init(info_s32);
98
99 // Configure/Init sobelNxN
100 if(gradient_size == 3)
101 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100102 auto k = arm_compute::support::cpp14::make_unique<CLSobel3x3>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
104 _sobel = std::move(k);
105 }
106 else if(gradient_size == 5)
107 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100108 auto k = arm_compute::support::cpp14::make_unique<CLSobel5x5>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
110 _sobel = std::move(k);
111 }
112 else if(gradient_size == 7)
113 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100114 auto k = arm_compute::support::cpp14::make_unique<CLSobel7x7>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
116 _sobel = std::move(k);
117 }
118 else
119 {
120 ARM_COMPUTE_ERROR("Gradient %d size not supported", gradient_size);
121 }
122
123 // Configure gradient
124 _gradient.configure(&_gx, &_gy, &_mag, &_phase, norm_type);
125
126 // Configure non-maxima suppression
127 _non_max_suppr.configure(&_mag, &_phase, &_nonmax, lower_thr, border_mode == BorderMode::UNDEFINED);
128
129 // Fill border around magnitude image as non-maxima suppression will access
130 // it. If border mode is undefined filling the border is a nop.
131 _border_mag_gradient.configure(&_mag, _non_max_suppr.border_size(), border_mode, constant_border_value);
132
133 // Configure edge tracing
134 _edge_trace.configure(&_nonmax, output, upper_thr, lower_thr, &_visited, &_recorded, &_l1_stack, &_l1_list_counter);
135
136 _gx.allocator()->allocate();
137 _gy.allocator()->allocate();
138 _phase.allocator()->allocate();
139 _mag.allocator()->allocate();
140 _visited.allocator()->allocate();
141 _recorded.allocator()->allocate();
142 _l1_stack.allocator()->allocate();
143 _l1_list_counter.allocator()->allocate();
144 _nonmax.allocator()->allocate();
145}
146
147void CLCannyEdge::run()
148{
149 // Run sobel
150 _sobel->run();
151
152 // Run phase and magnitude calculation
153 CLScheduler::get().enqueue(_gradient, false);
154
155 // Fill border before non-maxima suppression. Nop for border mode undefined.
156 CLScheduler::get().enqueue(_border_mag_gradient, false);
157
158 // Run non max suppresion
159 _nonmax.clear(CLScheduler::get().queue());
160 CLScheduler::get().enqueue(_non_max_suppr, false);
161
162 // Clear temporary structures and run edge trace
163 _visited.clear(CLScheduler::get().queue());
164 _recorded.clear(CLScheduler::get().queue());
165 _l1_list_counter.clear(CLScheduler::get().queue());
166 _l1_stack.clear(CLScheduler::get().queue());
167 CLScheduler::get().enqueue(_edge_trace, true);
168}