blob: 20b8dd81a3f58cfb7ae1074dd0d0b5e9f902451a [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"
29#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/Validate.h"
31#include "arm_compute/runtime/CL/CLScheduler.h"
32#include "arm_compute/runtime/CL/functions/CLSobel3x3.h"
33#include "arm_compute/runtime/CL/functions/CLSobel5x5.h"
34#include "arm_compute/runtime/CL/functions/CLSobel7x7.h"
35
36using namespace arm_compute;
37
38CLCannyEdge::CLCannyEdge()
39 : _sobel(nullptr), _gradient(), _non_max_suppr(), _edge_trace(), _gx(), _gy(), _mag(), _phase(), _nonmax(), _visited(), _recorded(), _l1_list_counter(), _l1_stack()
40{
41}
42
43void 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)
44{
45 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
46 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
47 ARM_COMPUTE_ERROR_ON((1 != norm_type) && (2 != norm_type));
48 ARM_COMPUTE_ERROR_ON(lower_thr > upper_thr);
49
50 const unsigned int L1_hysteresis_stack_size = 8;
51 int32_t num_pixel_to_skip = (border_mode == BorderMode::UNDEFINED) ? gradient_size / 2 : 0;
52 const TensorShape shape = input->info()->tensor_shape();
53
54 TensorInfo gradient_info;
55 TensorInfo info;
56
57 // Initialize images
58 if(gradient_size < 7)
59 {
60 gradient_info.init_auto_padding(shape, 1, arm_compute::DataType::S16);
61 info.init_auto_padding(shape, 1, arm_compute::DataType::U16);
62 }
63 else
64 {
65 gradient_info.init_auto_padding(shape, 1, arm_compute::DataType::S32);
66 info.init_auto_padding(shape, 1, arm_compute::DataType::U32);
67 }
68
69 _gx.allocator()->init(gradient_info);
70 _gx.allocator()->allocate();
71 _gy.allocator()->init(gradient_info);
72 _gy.allocator()->allocate();
73 _mag.allocator()->init(info);
74 _mag.allocator()->allocate();
75 _nonmax.allocator()->init(info);
76 _nonmax.allocator()->allocate();
77
78 TensorInfo info_u8(shape, 1, arm_compute::DataType::U8);
79 info_u8.auto_padding();
80 _phase.allocator()->init(info_u8);
81 _phase.allocator()->allocate();
82 _l1_list_counter.allocator()->init(info_u8);
83 _l1_list_counter.allocator()->allocate();
84
85 TensorInfo info_u32(shape, 1, arm_compute::DataType::U32);
86 info_u32.auto_padding();
87 _visited.allocator()->init(info_u32);
88 _visited.allocator()->allocate();
89 _recorded.allocator()->init(info_u32);
90 _recorded.allocator()->allocate();
91
92 TensorShape shape_l1_stack = input->info()->tensor_shape();
93 shape_l1_stack.set(0, input->info()->dimension(0) * L1_hysteresis_stack_size);
94 TensorInfo info_s32(shape_l1_stack, 1, arm_compute::DataType::S32);
95 info_s32.auto_padding();
96 _l1_stack.allocator()->init(info_s32);
97 _l1_stack.allocator()->allocate();
98
99 // Configure/Init sobelNxN
100 if(gradient_size == 3)
101 {
102 auto k = arm_compute::cpp14::make_unique<CLSobel3x3>();
103 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
104 _sobel = std::move(k);
105 }
106 else if(gradient_size == 5)
107 {
108 auto k = arm_compute::cpp14::make_unique<CLSobel5x5>();
109 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
110 _sobel = std::move(k);
111 }
112 else if(gradient_size == 7)
113 {
114 auto k = arm_compute::cpp14::make_unique<CLSobel7x7>();
115 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, num_pixel_to_skip, border_mode == BorderMode::UNDEFINED);
125
126 // Configure non-maxima suppression
127 _non_max_suppr.configure(&_mag, &_phase, &_nonmax, lower_thr, num_pixel_to_skip, border_mode == BorderMode::UNDEFINED);
128
129 // Configure edge tracing
130 num_pixel_to_skip += 1;
131 _edge_trace.configure(&_nonmax, output, upper_thr, lower_thr,
132 &_visited, &_recorded, &_l1_stack, &_l1_list_counter, num_pixel_to_skip, border_mode == BorderMode::UNDEFINED);
133}
134
135void CLCannyEdge::run()
136{
137 // Run sobel
138 _sobel->run();
139
140 // Run phase and magnitude calculation
141 CLScheduler::get().enqueue(_gradient, false);
142
143 // Run non max suppresion
144 _nonmax.clear(CLScheduler::get().queue());
145 CLScheduler::get().enqueue(_non_max_suppr, false);
146
147 // Clear temporary structures and run edge trace
148 _visited.clear(CLScheduler::get().queue());
149 _recorded.clear(CLScheduler::get().queue());
150 _l1_list_counter.clear(CLScheduler::get().queue());
151 _l1_stack.clear(CLScheduler::get().queue());
152 CLScheduler::get().enqueue(_edge_trace, true);
153}