Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 1 | /* |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 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 | |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 34 | #include <cmath> |
Anthony Barbier | d51ea0a | 2018-08-07 17:48:03 +0100 | [diff] [blame] | 35 | #include <stack> |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace test |
| 40 | { |
| 41 | namespace validation |
| 42 | { |
| 43 | namespace reference |
| 44 | { |
| 45 | namespace |
| 46 | { |
| 47 | const auto MARK_ZERO = 0u; |
| 48 | const auto MARK_MAYBE = 127u; |
| 49 | const auto MARK_EDGE = 255u; |
| 50 | |
Michele Di Giorgio | bb71fe5 | 2018-06-20 11:45:35 +0100 | [diff] [blame] | 51 | template <typename T> |
Michele Di Giorgio | 89a2b57 | 2018-06-29 11:17:18 +0100 | [diff] [blame] | 52 | void trace_edge(SimpleTensor<T> &dst, const ValidRegion &valid_region) |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 53 | { |
Michele Di Giorgio | bb71fe5 | 2018-06-20 11:45:35 +0100 | [diff] [blame] | 54 | std::stack<Coordinates> pixels_stack; |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 55 | for(auto i = 0; i < dst.num_elements(); ++i) |
| 56 | { |
Michele Di Giorgio | bb71fe5 | 2018-06-20 11:45:35 +0100 | [diff] [blame] | 57 | if(dst[i] == MARK_EDGE) |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 58 | { |
Michele Di Giorgio | bb71fe5 | 2018-06-20 11:45:35 +0100 | [diff] [blame] | 59 | pixels_stack.push(index2coord(dst.shape(), i)); |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 60 | } |
Michele Di Giorgio | bb71fe5 | 2018-06-20 11:45:35 +0100 | [diff] [blame] | 61 | } |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 62 | |
Michele Di Giorgio | bb71fe5 | 2018-06-20 11:45:35 +0100 | [diff] [blame] | 63 | while(!pixels_stack.empty()) |
| 64 | { |
| 65 | const Coordinates pixel_coord = pixels_stack.top(); |
| 66 | pixels_stack.pop(); |
| 67 | |
| 68 | std::array<Coordinates, 8> neighbours = |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 69 | { |
| 70 | { |
Michele Di Giorgio | bb71fe5 | 2018-06-20 11:45:35 +0100 | [diff] [blame] | 71 | Coordinates(pixel_coord.x() - 1, pixel_coord.y() + 0), |
| 72 | Coordinates(pixel_coord.x() + 1, pixel_coord.y() + 0), |
| 73 | Coordinates(pixel_coord.x() - 1, pixel_coord.y() - 1), |
| 74 | Coordinates(pixel_coord.x() + 1, pixel_coord.y() + 1), |
| 75 | Coordinates(pixel_coord.x() + 0, pixel_coord.y() - 1), |
| 76 | Coordinates(pixel_coord.x() + 0, pixel_coord.y() + 1), |
| 77 | Coordinates(pixel_coord.x() + 1, pixel_coord.y() - 1), |
| 78 | Coordinates(pixel_coord.x() - 1, pixel_coord.y() + 1) |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 79 | } |
| 80 | }; |
| 81 | |
Michele Di Giorgio | bb71fe5 | 2018-06-20 11:45:35 +0100 | [diff] [blame] | 82 | // Mark MAYBE neighbours as edges since they are next to an EDGE |
| 83 | std::for_each(neighbours.begin(), neighbours.end(), [&](Coordinates & coord) |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 84 | { |
Michele Di Giorgio | 89a2b57 | 2018-06-29 11:17:18 +0100 | [diff] [blame] | 85 | if(is_in_valid_region(valid_region, coord)) |
Michele Di Giorgio | bb71fe5 | 2018-06-20 11:45:35 +0100 | [diff] [blame] | 86 | { |
Michele Di Giorgio | 89a2b57 | 2018-06-29 11:17:18 +0100 | [diff] [blame] | 87 | const size_t pixel_index = coord2index(dst.shape(), coord); |
| 88 | const T pixel = dst[pixel_index]; |
| 89 | if(pixel == MARK_MAYBE) |
| 90 | { |
| 91 | dst[pixel_index] = MARK_EDGE; |
| 92 | pixels_stack.push(coord); |
| 93 | } |
Michele Di Giorgio | bb71fe5 | 2018-06-20 11:45:35 +0100 | [diff] [blame] | 94 | } |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 95 | }); |
Michele Di Giorgio | bb71fe5 | 2018-06-20 11:45:35 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // Mark all remaining MAYBE pixels as ZERO (not edges) |
| 99 | for(auto i = 0; i < dst.num_elements(); ++i) |
| 100 | { |
| 101 | if(dst[i] == MARK_MAYBE) |
| 102 | { |
| 103 | dst[i] = MARK_ZERO; |
| 104 | } |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | template <typename U, typename T> |
| 109 | SimpleTensor<T> canny_edge_detector_impl(const SimpleTensor<T> &src, int32_t upper, int32_t lower, int gradient_size, MagnitudeType norm_type, |
| 110 | BorderMode border_mode, T constant_border_value) |
| 111 | { |
| 112 | ARM_COMPUTE_ERROR_ON(gradient_size != 3 && gradient_size != 5 && gradient_size != 7); |
| 113 | ARM_COMPUTE_ERROR_ON(lower < 0 || lower >= upper); |
| 114 | |
| 115 | // Output: T == uint8_t |
| 116 | SimpleTensor<T> dst{ src.shape(), src.data_type() }; |
| 117 | ValidRegion valid_region = shape_to_valid_region(src.shape(), border_mode == BorderMode::UNDEFINED, BorderSize(gradient_size / 2 + 1)); |
| 118 | |
| 119 | // Sobel computation: U == int16_t or int32_t |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 120 | SimpleTensor<U> gx{}; |
| 121 | SimpleTensor<U> gy{}; |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 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 | |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 182 | unsigned_U mag_90; |
| 183 | unsigned_U mag90; |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 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 |
Michele Di Giorgio | 89a2b57 | 2018-06-29 11:17:18 +0100 | [diff] [blame] | 228 | trace_edge<T>(dst, valid_region); |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 229 | return dst; |
| 230 | } |
| 231 | } // namespace |
| 232 | |
| 233 | template <typename T> |
Georgios Pinitas | 09d3451 | 2018-08-30 16:02:11 +0100 | [diff] [blame] | 234 | SimpleTensor<T> canny_edge_detector(const SimpleTensor<T> &src, |
| 235 | int32_t upper_thresh, int32_t lower_thresh, int gradient_size, MagnitudeType norm_type, |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 236 | BorderMode border_mode, T constant_border_value) |
| 237 | { |
| 238 | if(gradient_size < 7) |
| 239 | { |
| 240 | return canny_edge_detector_impl<int16_t>(src, upper_thresh, lower_thresh, gradient_size, norm_type, border_mode, constant_border_value); |
| 241 | } |
| 242 | else |
| 243 | { |
| 244 | return canny_edge_detector_impl<int32_t>(src, upper_thresh, lower_thresh, gradient_size, norm_type, border_mode, constant_border_value); |
| 245 | } |
| 246 | } |
| 247 | |
Georgios Pinitas | 09d3451 | 2018-08-30 16:02:11 +0100 | [diff] [blame] | 248 | template SimpleTensor<uint8_t> canny_edge_detector(const SimpleTensor<uint8_t> &src, |
| 249 | int32_t upper_thresh, int32_t lower_thresh, int gradient_size, MagnitudeType norm_type, |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 250 | BorderMode border_mode, uint8_t constant_border_value); |
| 251 | } // namespace reference |
| 252 | } // namespace validation |
| 253 | } // namespace test |
| 254 | } // namespace arm_compute |