Compute Library
 20.02.1
CLCannyEdge.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2019 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  */
25 
28 #include "arm_compute/core/Error.h"
35 
36 using namespace arm_compute;
37 
38 CLCannyEdge::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(),
54  _output(nullptr)
55 {
56 }
57 
58 void 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,
59  uint8_t constant_border_value)
60 {
64  ARM_COMPUTE_ERROR_ON((1 != norm_type) && (2 != norm_type));
65  ARM_COMPUTE_ERROR_ON((gradient_size != 3) && (gradient_size != 5) && (gradient_size != 7));
66  ARM_COMPUTE_ERROR_ON((lower_thr < 0) || (lower_thr >= upper_thr));
67 
68  _output = output;
69 
70  const unsigned int L1_hysteresis_stack_size = 8;
71  const TensorShape shape = input->info()->tensor_shape();
72 
73  TensorInfo gradient_info;
75 
76  // Initialize images
77  if(gradient_size < 7)
78  {
79  gradient_info.init(shape, 1, arm_compute::DataType::S16);
81  }
82  else
83  {
84  gradient_info.init(shape, 1, arm_compute::DataType::S32);
86  }
87 
88  _gx.allocator()->init(gradient_info);
89  _gy.allocator()->init(gradient_info);
90  _mag.allocator()->init(info);
91  _nonmax.allocator()->init(info);
92 
94  _phase.allocator()->init(info_u8);
95  _l1_list_counter.allocator()->init(info_u8);
96 
98  _visited.allocator()->init(info_u32);
99  _recorded.allocator()->init(info_u32);
100 
101  TensorShape shape_l1_stack = input->info()->tensor_shape();
102  shape_l1_stack.set(0, input->info()->dimension(0) * L1_hysteresis_stack_size);
103  TensorInfo info_s32(shape_l1_stack, 1, arm_compute::DataType::S32);
104  _l1_stack.allocator()->init(info_s32);
105 
106  // Manage intermediate buffers
107  _memory_group.manage(&_gx);
108  _memory_group.manage(&_gy);
109 
110  // Configure/Init sobelNxN
111  if(gradient_size == 3)
112  {
113  auto k = arm_compute::support::cpp14::make_unique<CLSobel3x3>();
114  k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
115  _sobel = std::move(k);
116  }
117  else if(gradient_size == 5)
118  {
119  auto k = arm_compute::support::cpp14::make_unique<CLSobel5x5>();
120  k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
121  _sobel = std::move(k);
122  }
123  else if(gradient_size == 7)
124  {
125  auto k = arm_compute::support::cpp14::make_unique<CLSobel7x7>();
126  k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
127  _sobel = std::move(k);
128  }
129  else
130  {
131  ARM_COMPUTE_ERROR_VAR("Gradient size %d not supported", gradient_size);
132  }
133 
134  // Manage intermediate buffers
135  _memory_group.manage(&_mag);
136  _memory_group.manage(&_phase);
137 
138  // Configure gradient
139  _gradient.configure(&_gx, &_gy, &_mag, &_phase, norm_type);
140 
141  // Allocate intermediate buffers
142  _gx.allocator()->allocate();
143  _gy.allocator()->allocate();
144 
145  // Manage intermediate buffers
146  _memory_group.manage(&_nonmax);
147 
148  // Configure non-maxima suppression
149  _non_max_suppr.configure(&_mag, &_phase, &_nonmax, lower_thr, border_mode == BorderMode::UNDEFINED);
150 
151  // Allocate intermediate buffers
152  _phase.allocator()->allocate();
153 
154  // Fill border around magnitude image as non-maxima suppression will access
155  // it. If border mode is undefined filling the border is a nop.
156  _border_mag_gradient.configure(&_mag, _non_max_suppr.border_size(), border_mode, constant_border_value);
157 
158  // Allocate intermediate buffers
159  _mag.allocator()->allocate();
160 
161  // Manage intermediate buffers
162  _memory_group.manage(&_visited);
163  _memory_group.manage(&_recorded);
164  _memory_group.manage(&_l1_stack);
165  _memory_group.manage(&_l1_list_counter);
166 
167  // Configure edge tracing
168  _edge_trace.configure(&_nonmax, output, upper_thr, lower_thr, &_visited, &_recorded, &_l1_stack, &_l1_list_counter);
169 
170  // Allocate intermediate buffers
171  _visited.allocator()->allocate();
172  _recorded.allocator()->allocate();
173  _l1_stack.allocator()->allocate();
174  _l1_list_counter.allocator()->allocate();
175  _nonmax.allocator()->allocate();
176 }
177 
179 {
180  MemoryGroupResourceScope scope_mg(_memory_group);
181 
182  // Run sobel
183  _sobel->run();
184 
185  // Run phase and magnitude calculation
186  CLScheduler::get().enqueue(_gradient, false);
187 
188  // Fill border before non-maxima suppression. Nop for border mode undefined.
189  CLScheduler::get().enqueue(_border_mag_gradient, false);
190 
191  // Run non max suppresion
192  _nonmax.clear(CLScheduler::get().queue());
193  CLScheduler::get().enqueue(_non_max_suppr, false);
194 
195  // Clear temporary structures and run edge trace
196  _output->clear(CLScheduler::get().queue());
197  _visited.clear(CLScheduler::get().queue());
198  _recorded.clear(CLScheduler::get().queue());
199  _l1_list_counter.clear(CLScheduler::get().queue());
200  _l1_stack.clear(CLScheduler::get().queue());
201  CLScheduler::get().enqueue(_edge_trace, true);
202 }
BorderMode
Methods available to handle borders.
Definition: Types.h:261
void configure(const ICLTensor *magnitude, const ICLTensor *phase, ICLTensor *output, int32_t lower_thr, bool border_undefined)
Initialise the kernel's sources, destination and border mode.
Shape of a tensor.
Definition: TensorShape.h:39
static CLScheduler & get()
Access the scheduler singleton.
Definition: CLScheduler.cpp:99
1 channel, 1 U8 per channel
#define ARM_COMPUTE_ERROR_VAR(msg,...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:346
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
CLTensorAllocator * allocator()
Return a pointer to the tensor's allocator.
Definition: CLTensor.cpp:61
1 channel, 1 U16 per channel
void configure(ICLTensor *tensor, BorderSize border_size, BorderMode border_mode, const PixelValue &constant_border_value=PixelValue())
Initialise the kernel's input, output and border mode.
void init(const TensorInfo &input, size_t alignment=0)
Initialize a tensor based on the passed TensorInfo.
Copyright (c) 2017-2020 ARM Limited.
1 channel, 1 S32 per channel
void manage(IMemoryManageable *obj) override
Sets a object to be managed by the given memory group.
Definition: MemoryGroup.h:79
virtual void run() override
Run the kernels contained in the function.
CLCannyEdge(std::shared_ptr< IMemoryManager > memory_manager=nullptr)
Constructor.
Definition: CLCannyEdge.cpp:38
void clear(cl::CommandQueue &q)
Clear the contents of the tensor synchronously.
Definition: ICLTensor.cpp:46
1 channel, 1 U32 per channel
1 channel, 1 S16 per channel
void enqueue(ICLKernel &kernel, bool flush=true)
Schedule the execution of the passed kernel if possible.
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:790
void init(Format format)
Initialize the tensor info with just a format.
Definition: TensorInfo.cpp:107
void allocate() override
Allocate size specified by TensorInfo of OpenCL memory.
void configure(const ICLTensor *gx, const ICLTensor *gy, ICLTensor *magnitude, ICLTensor *phase, int32_t norm_type)
Initialise the kernel's sources, destinations and border mode.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
void 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=0)
Initialise the function's source, destination, thresholds, gradient size, normalization type and bord...
Definition: CLCannyEdge.cpp:58
void configure(const ICLTensor *input, ICLTensor *output, int32_t upper_thr, int32_t lower_thr, ICLTensor *visited, ICLTensor *recorded, ICLTensor *l1_stack, ICLTensor *l1_stack_counter)
Initialise the kernel's source, destination and border mode.
Memory group resources scope handling class.
Definition: IMemoryGroup.h:82
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
Borders are left undefined.
TensorShape & set(size_t dimension, size_t value, bool apply_dim_correction=true)
Accessor to set the value of one of the dimensions.
Definition: TensorShape.h:78
Store the tensor's metadata.
Definition: TensorInfo.h:45
BorderSize border_size() const override
The size of the border for that kernel.