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