blob: f1ad98cb3b37d85791f4dfa682ea50deea14c8bb [file] [log] [blame]
Abe Mbise1b993382017-12-19 13:51:59 +00001/*
2 * Copyright (c) 2017-2018 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 "CannyEdgeDetector.h"
25
26#include "Utils.h"
27#include "support/ToolchainSupport.h"
28#include "tests/validation/Helpers.h"
29#include "tests/validation/reference/Magnitude.h"
30#include "tests/validation/reference/NonMaximaSuppression.h"
31#include "tests/validation/reference/Phase.h"
32#include "tests/validation/reference/Sobel.h"
33
34#include "tests/SimpleTensorPrinter.h"
35
36#include <cmath>
37
38namespace arm_compute
39{
40namespace test
41{
42namespace validation
43{
44namespace reference
45{
46namespace
47{
48const auto MARK_ZERO = 0u;
49const auto MARK_MAYBE = 127u;
50const auto MARK_EDGE = 255u;
51
Michele Di Giorgiobb71fe52018-06-20 11:45:35 +010052template <typename T>
Michele Di Giorgio89a2b572018-06-29 11:17:18 +010053void trace_edge(SimpleTensor<T> &dst, const ValidRegion &valid_region)
Abe Mbise1b993382017-12-19 13:51:59 +000054{
Michele Di Giorgiobb71fe52018-06-20 11:45:35 +010055 std::stack<Coordinates> pixels_stack;
Abe Mbise1b993382017-12-19 13:51:59 +000056 for(auto i = 0; i < dst.num_elements(); ++i)
57 {
Michele Di Giorgiobb71fe52018-06-20 11:45:35 +010058 if(dst[i] == MARK_EDGE)
Abe Mbise1b993382017-12-19 13:51:59 +000059 {
Michele Di Giorgiobb71fe52018-06-20 11:45:35 +010060 pixels_stack.push(index2coord(dst.shape(), i));
Abe Mbise1b993382017-12-19 13:51:59 +000061 }
Michele Di Giorgiobb71fe52018-06-20 11:45:35 +010062 }
Abe Mbise1b993382017-12-19 13:51:59 +000063
Michele Di Giorgiobb71fe52018-06-20 11:45:35 +010064 while(!pixels_stack.empty())
65 {
66 const Coordinates pixel_coord = pixels_stack.top();
67 pixels_stack.pop();
68
69 std::array<Coordinates, 8> neighbours =
Abe Mbise1b993382017-12-19 13:51:59 +000070 {
71 {
Michele Di Giorgiobb71fe52018-06-20 11:45:35 +010072 Coordinates(pixel_coord.x() - 1, pixel_coord.y() + 0),
73 Coordinates(pixel_coord.x() + 1, pixel_coord.y() + 0),
74 Coordinates(pixel_coord.x() - 1, pixel_coord.y() - 1),
75 Coordinates(pixel_coord.x() + 1, pixel_coord.y() + 1),
76 Coordinates(pixel_coord.x() + 0, pixel_coord.y() - 1),
77 Coordinates(pixel_coord.x() + 0, pixel_coord.y() + 1),
78 Coordinates(pixel_coord.x() + 1, pixel_coord.y() - 1),
79 Coordinates(pixel_coord.x() - 1, pixel_coord.y() + 1)
Abe Mbise1b993382017-12-19 13:51:59 +000080 }
81 };
82
Michele Di Giorgiobb71fe52018-06-20 11:45:35 +010083 // Mark MAYBE neighbours as edges since they are next to an EDGE
84 std::for_each(neighbours.begin(), neighbours.end(), [&](Coordinates & coord)
Abe Mbise1b993382017-12-19 13:51:59 +000085 {
Michele Di Giorgio89a2b572018-06-29 11:17:18 +010086 if(is_in_valid_region(valid_region, coord))
Michele Di Giorgiobb71fe52018-06-20 11:45:35 +010087 {
Michele Di Giorgio89a2b572018-06-29 11:17:18 +010088 const size_t pixel_index = coord2index(dst.shape(), coord);
89 const T pixel = dst[pixel_index];
90 if(pixel == MARK_MAYBE)
91 {
92 dst[pixel_index] = MARK_EDGE;
93 pixels_stack.push(coord);
94 }
Michele Di Giorgiobb71fe52018-06-20 11:45:35 +010095 }
Abe Mbise1b993382017-12-19 13:51:59 +000096 });
Michele Di Giorgiobb71fe52018-06-20 11:45:35 +010097 }
98
99 // Mark all remaining MAYBE pixels as ZERO (not edges)
100 for(auto i = 0; i < dst.num_elements(); ++i)
101 {
102 if(dst[i] == MARK_MAYBE)
103 {
104 dst[i] = MARK_ZERO;
105 }
Abe Mbise1b993382017-12-19 13:51:59 +0000106 }
107}
108
109template <typename U, typename T>
110SimpleTensor<T> canny_edge_detector_impl(const SimpleTensor<T> &src, int32_t upper, int32_t lower, int gradient_size, MagnitudeType norm_type,
111 BorderMode border_mode, T constant_border_value)
112{
113 ARM_COMPUTE_ERROR_ON(gradient_size != 3 && gradient_size != 5 && gradient_size != 7);
114 ARM_COMPUTE_ERROR_ON(lower < 0 || lower >= upper);
115
116 // Output: T == uint8_t
117 SimpleTensor<T> dst{ src.shape(), src.data_type() };
118 ValidRegion valid_region = shape_to_valid_region(src.shape(), border_mode == BorderMode::UNDEFINED, BorderSize(gradient_size / 2 + 1));
119
120 // Sobel computation: U == int16_t or int32_t
121 SimpleTensor<U> gx, gy;
122 std::tie(gx, gy) = sobel<U>(src, gradient_size, border_mode, constant_border_value, GradientDimension::GRAD_XY);
123
124 using unsigned_U = typename traits::make_unsigned_conditional_t<U>::type;
125 using promoted_U = typename common_promoted_signed_type<U>::intermediate_type;
126
127 // Gradient magnitude and phase (edge direction)
128 const DataType mag_data_type = gx.data_type() == DataType::S16 ? DataType::U16 : DataType::U32;
129 SimpleTensor<unsigned_U> grad_mag{ gx.shape(), mag_data_type };
130 SimpleTensor<uint8_t> grad_dir{ gy.shape(), DataType::U8 };
131
132 for(auto i = 0; i < grad_mag.num_elements(); ++i)
133 {
134 double mag = 0.f;
135
136 if(norm_type == MagnitudeType::L2NORM)
137 {
138 mag = support::cpp11::round(std::sqrt(static_cast<promoted_U>(gx[i]) * gx[i] + static_cast<promoted_U>(gy[i]) * gy[i]));
139 }
140 else // MagnitudeType::L1NORM
141 {
142 mag = static_cast<promoted_U>(std::abs(gx[i])) + static_cast<promoted_U>(std::abs(gy[i]));
143 }
144
145 float angle = 180.f * std::atan2(static_cast<float>(gy[i]), static_cast<float>(gx[i])) / M_PI;
146 grad_dir[i] = support::cpp11::round(angle < 0.f ? 180 + angle : angle);
147 grad_mag[i] = saturate_cast<unsigned_U>(mag);
148 }
149
150 /*
151 Quantise the phase into 4 directions
152 0° dir=0 0.0 <= p < 22.5 or 157.5 <= p < 180
153 45° dir=1 22.5 <= p < 67.5
154 90° dir=2 67.5 <= p < 112.5
155 135° dir=3 112.5 <= p < 157.5
156 */
157 for(auto i = 0; i < grad_dir.num_elements(); ++i)
158 {
159 const auto direction = std::fabs(grad_dir[i]);
160 grad_dir[i] = (direction < 22.5 || direction >= 157.5) ? 0 : (direction < 67.5) ? 1 : (direction < 112.5) ? 2 : 3;
161 }
162
163 // Non-maximum suppression
164 std::vector<int> strong_edges;
165 const auto upper_thresh = static_cast<uint32_t>(upper);
166 const auto lower_thresh = static_cast<uint32_t>(lower);
167
168 const auto pixel_at_offset = [&](const SimpleTensor<unsigned_U> &tensor, const Coordinates & coord, int xoffset, int yoffset)
169 {
170 return tensor_elem_at(tensor, Coordinates{ coord.x() + xoffset, coord.y() + yoffset }, border_mode, static_cast<unsigned_U>(constant_border_value));
171 };
172
173 for(auto i = 0; i < dst.num_elements(); ++i)
174 {
175 const auto coord = index2coord(dst.shape(), i);
176 if(!is_in_valid_region(valid_region, coord) || grad_mag[i] <= lower_thresh)
177 {
178 dst[i] = MARK_ZERO;
179 continue;
180 }
181
182 unsigned_U mag_90, mag90;
183 switch(grad_dir[i])
184 {
185 case 0: // North/South edge direction, compare against East/West pixels (left & right)
186 mag_90 = pixel_at_offset(grad_mag, coord, -1, 0);
187 mag90 = pixel_at_offset(grad_mag, coord, 1, 0);
188 break;
189 case 1: // NE/SW edge direction, compare against NW/SE pixels (top-left & bottom-right)
190 mag_90 = pixel_at_offset(grad_mag, coord, -1, -1);
191 mag90 = pixel_at_offset(grad_mag, coord, +1, +1);
192 break;
193 case 2: // East/West edge direction, compare against North/South pixels (top & bottom)
194 mag_90 = pixel_at_offset(grad_mag, coord, 0, -1);
195 mag90 = pixel_at_offset(grad_mag, coord, 0, +1);
196 break;
197 case 3: // NW/SE edge direction, compare against NE/SW pixels (top-right & bottom-left)
198 mag_90 = pixel_at_offset(grad_mag, coord, +1, -1);
199 mag90 = pixel_at_offset(grad_mag, coord, -1, +1);
200 break;
201 default:
202 ARM_COMPUTE_ERROR("Invalid gradient phase provided");
203 break;
204 }
205
206 // Potential edge if greater than both pixels at +/-90° on either side
207 if(grad_mag[i] > mag_90 && grad_mag[i] > mag90)
208 {
209 // Double thresholding and edge tracing
210 if(grad_mag[i] > upper_thresh)
211 {
212 dst[i] = MARK_EDGE; // Definite edge pixel
213 strong_edges.emplace_back(i);
214 }
215 else
216 {
217 dst[i] = MARK_MAYBE;
218 }
219 }
220 else
221 {
222 dst[i] = MARK_ZERO; // Since not greater than neighbours
223 }
224 }
225
226 // Final edge tracing
Michele Di Giorgio89a2b572018-06-29 11:17:18 +0100227 trace_edge<T>(dst, valid_region);
Abe Mbise1b993382017-12-19 13:51:59 +0000228 return dst;
229}
230} // namespace
231
232template <typename T>
233SimpleTensor<T> canny_edge_detector(const SimpleTensor<T> &src, int32_t upper_thresh, int32_t lower_thresh, int gradient_size, MagnitudeType norm_type,
234 BorderMode border_mode, T constant_border_value)
235{
236 if(gradient_size < 7)
237 {
238 return canny_edge_detector_impl<int16_t>(src, upper_thresh, lower_thresh, gradient_size, norm_type, border_mode, constant_border_value);
239 }
240 else
241 {
242 return canny_edge_detector_impl<int32_t>(src, upper_thresh, lower_thresh, gradient_size, norm_type, border_mode, constant_border_value);
243 }
244}
245
246template SimpleTensor<uint8_t> canny_edge_detector(const SimpleTensor<uint8_t> &src, int32_t upper_thresh, int32_t lower_thresh, int gradient_size, MagnitudeType norm_type,
247 BorderMode border_mode, uint8_t constant_border_value);
248} // namespace reference
249} // namespace validation
250} // namespace test
251} // namespace arm_compute