Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [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 "arm_compute/runtime/NEON/functions/NECannyEdge.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/ITensor.h" |
| 28 | #include "arm_compute/core/NEON/kernels/NECannyEdgeKernel.h" |
| 29 | #include "arm_compute/core/NEON/kernels/NEFillBorderKernel.h" |
| 30 | #include "arm_compute/core/TensorInfo.h" |
| 31 | #include "arm_compute/core/Validate.h" |
| 32 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
| 33 | #include "arm_compute/runtime/NEON/functions/NESobel3x3.h" |
| 34 | #include "arm_compute/runtime/NEON/functions/NESobel5x5.h" |
| 35 | #include "arm_compute/runtime/NEON/functions/NESobel7x7.h" |
| 36 | #include "arm_compute/runtime/TensorAllocator.h" |
Matthew Bentham | 9204646 | 2020-03-07 22:15:55 +0000 | [diff] [blame] | 37 | #include "support/MemorySupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 38 | |
| 39 | #include <cstring> |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 40 | #include <inttypes.h> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 41 | #include <utility> |
| 42 | |
| 43 | using namespace arm_compute; |
| 44 | |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 45 | NECannyEdge::NECannyEdge(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT |
| 46 | : _memory_group(std::move(memory_manager)), |
| 47 | _sobel(), |
Moritz Pflanzer | f4af76e | 2017-09-06 07:42:43 +0100 | [diff] [blame] | 48 | _gradient(), |
| 49 | _non_max_suppr(), |
| 50 | _edge_trace(), |
| 51 | _border_mag_gradient(), |
| 52 | _border_edge_trace(), |
| 53 | _gx(), |
| 54 | _gy(), |
| 55 | _magnitude(), |
| 56 | _phase(), |
| 57 | _nonmax(), |
| 58 | _output(nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 59 | { |
| 60 | } |
| 61 | |
Georgios Pinitas | 09d3451 | 2018-08-30 16:02:11 +0100 | [diff] [blame] | 62 | void NECannyEdge::configure(ITensor *input, ITensor *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) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 63 | { |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 64 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 65 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); |
| 66 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 67 | ARM_COMPUTE_ERROR_ON((1 != norm_type) && (2 != norm_type)); |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 68 | ARM_COMPUTE_ERROR_ON((gradient_size != 3) && (gradient_size != 5) && (gradient_size != 7)); |
Michele Di Giorgio | bb71fe5 | 2018-06-20 11:45:35 +0100 | [diff] [blame] | 69 | ARM_COMPUTE_ERROR_ON((lower_thr < 0) || (lower_thr >= upper_thr)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 70 | |
| 71 | _output = output; |
| 72 | |
| 73 | const TensorShape &shape = input->info()->tensor_shape(); |
| 74 | TensorInfo gradient_info; |
| 75 | TensorInfo magnitude_info; |
| 76 | |
| 77 | // Initialize images |
| 78 | if(gradient_size < 7) |
| 79 | { |
| 80 | gradient_info.init(shape, Format::S16); |
| 81 | magnitude_info.init(shape, Format::U16); |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | gradient_info.init(shape, Format::S32); |
| 86 | magnitude_info.init(shape, Format::U32); |
| 87 | } |
| 88 | |
| 89 | _gx.allocator()->init(gradient_info); |
| 90 | _gy.allocator()->init(gradient_info); |
| 91 | _magnitude.allocator()->init(magnitude_info); |
| 92 | |
| 93 | TensorInfo info(shape, Format::U8); |
| 94 | _phase.allocator()->init(info); |
| 95 | _nonmax.allocator()->init(info); |
| 96 | |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 97 | // Manage intermediate buffers |
| 98 | _memory_group.manage(&_gx); |
| 99 | _memory_group.manage(&_gy); |
| 100 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 101 | // Configure/Init sobelNxN |
| 102 | if(gradient_size == 3) |
| 103 | { |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 104 | auto k = arm_compute::support::cpp14::make_unique<NESobel3x3>(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 105 | k->configure(input, &_gx, &_gy, border_mode, constant_border_value); |
| 106 | _sobel = std::move(k); |
| 107 | } |
| 108 | else if(gradient_size == 5) |
| 109 | { |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 110 | auto k = arm_compute::support::cpp14::make_unique<NESobel5x5>(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 111 | k->configure(input, &_gx, &_gy, border_mode, constant_border_value); |
| 112 | _sobel = std::move(k); |
| 113 | } |
| 114 | else if(gradient_size == 7) |
| 115 | { |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 116 | auto k = arm_compute::support::cpp14::make_unique<NESobel7x7>(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 117 | k->configure(input, &_gx, &_gy, border_mode, constant_border_value); |
| 118 | _sobel = std::move(k); |
| 119 | } |
| 120 | else |
| 121 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 122 | ARM_COMPUTE_ERROR_VAR("Gradient size %+" PRId32 " not supported\n", gradient_size); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 123 | } |
| 124 | |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 125 | // Manage intermediate buffers |
| 126 | _memory_group.manage(&_magnitude); |
| 127 | _memory_group.manage(&_phase); |
| 128 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 129 | // Configure gradient |
Georgios Pinitas | 09d3451 | 2018-08-30 16:02:11 +0100 | [diff] [blame] | 130 | auto k = arm_compute::support::cpp14::make_unique<NEGradientKernel>(); |
| 131 | k->configure(&_gx, &_gy, &_magnitude, &_phase, norm_type); |
| 132 | _gradient = std::move(k); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 133 | |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 134 | // Allocate intermediate tensors |
| 135 | _gx.allocator()->allocate(); |
| 136 | _gy.allocator()->allocate(); |
| 137 | |
| 138 | // Manage intermediate buffers |
| 139 | _memory_group.manage(&_nonmax); |
| 140 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 141 | // Configure non-maxima suppression |
| 142 | _non_max_suppr.configure(&_magnitude, &_phase, &_nonmax, upper_thr, lower_thr, border_mode == BorderMode::UNDEFINED); |
| 143 | |
| 144 | // Fill border around magnitude image as non-maxima suppression will access |
| 145 | // it. If border mode is undefined filling the border is a nop. |
| 146 | _border_mag_gradient.configure(&_magnitude, _non_max_suppr.border_size(), border_mode, constant_border_value); |
| 147 | |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 148 | // Allocate intermediate tensors |
| 149 | _phase.allocator()->allocate(); |
| 150 | _magnitude.allocator()->allocate(); |
| 151 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 152 | // Configure edge tracing |
| 153 | _edge_trace.configure(&_nonmax, output); |
| 154 | |
| 155 | // Fill border with "No edge" to stop recursion in edge trace |
Michalis Spyrou | 490bf2e | 2017-09-29 11:24:55 +0100 | [diff] [blame] | 156 | _border_edge_trace.configure(&_nonmax, _edge_trace.border_size(), BorderMode::CONSTANT, static_cast<float>(0.f)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 157 | |
| 158 | // Allocate intermediate tensors |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 159 | _nonmax.allocator()->allocate(); |
| 160 | } |
| 161 | |
| 162 | void NECannyEdge::run() |
| 163 | { |
| 164 | ARM_COMPUTE_ERROR_ON_MSG(_sobel == nullptr, "Unconfigured function"); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 165 | |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 166 | MemoryGroupResourceScope scope_mg(_memory_group); |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 167 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 168 | // Run sobelNxN |
| 169 | _sobel->run(); |
| 170 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 171 | // Run gradient |
| 172 | NEScheduler::get().schedule(_gradient.get(), Window::DimY); |
| 173 | |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 174 | // Fill border before non-maxima suppression. Nop for border mode undefined. |
| 175 | NEScheduler::get().schedule(&_border_mag_gradient, Window::DimZ); |
| 176 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 177 | // Run non-maxima suppression |
| 178 | NEScheduler::get().schedule(&_non_max_suppr, Window::DimY); |
| 179 | |
| 180 | ARM_COMPUTE_ERROR_ON(_output->buffer() == nullptr); |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 181 | std::fill_n(_output->buffer(), _output->info()->total_size(), 0); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 182 | |
| 183 | // Fill border before edge trace |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 184 | NEScheduler::get().schedule(&_border_edge_trace, Window::DimZ); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 185 | |
| 186 | // Run edge tracing |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 187 | NEScheduler::get().schedule(&_edge_trace, Window::DimY); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 188 | } |