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