Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | 9204646 | 2020-03-07 22:15:55 +0000 | [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/CL/functions/CLCannyEdge.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/ICLTensor.h" |
| 27 | #include "arm_compute/core/CL/OpenCL.h" |
| 28 | #include "arm_compute/core/Error.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Validate.h" |
| 30 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 31 | #include "arm_compute/runtime/CL/functions/CLSobel3x3.h" |
| 32 | #include "arm_compute/runtime/CL/functions/CLSobel5x5.h" |
| 33 | #include "arm_compute/runtime/CL/functions/CLSobel7x7.h" |
Matthew Bentham | 9204646 | 2020-03-07 22:15:55 +0000 | [diff] [blame] | 34 | #include "support/MemorySupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | |
| 36 | using namespace arm_compute; |
| 37 | |
Georgios Pinitas | 5701e2a | 2017-09-18 17:43:33 +0100 | [diff] [blame] | 38 | CLCannyEdge::CLCannyEdge(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT |
| 39 | : _memory_group(std::move(memory_manager)), |
| 40 | _sobel(), |
Moritz Pflanzer | f4af76e | 2017-09-06 07:42:43 +0100 | [diff] [blame] | 41 | _gradient(), |
| 42 | _border_mag_gradient(), |
| 43 | _non_max_suppr(), |
| 44 | _edge_trace(), |
| 45 | _gx(), |
| 46 | _gy(), |
| 47 | _mag(), |
| 48 | _phase(), |
| 49 | _nonmax(), |
| 50 | _visited(), |
| 51 | _recorded(), |
| 52 | _l1_list_counter(), |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 53 | _l1_stack(), |
| 54 | _output(nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 55 | { |
| 56 | } |
| 57 | |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 58 | void CLCannyEdge::configure(ICLTensor *input, ICLTensor *output, int32_t upper_thr, int32_t lower_thr, int32_t gradient_size, int32_t norm_type, BorderMode border_mode, |
| 59 | uint8_t constant_border_value) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 60 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 61 | configure(CLKernelLibrary::get().get_compile_context(), input, output, upper_thr, lower_thr, gradient_size, norm_type, border_mode, constant_border_value); |
| 62 | } |
| 63 | |
| 64 | void CLCannyEdge::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *output, int32_t upper_thr, int32_t lower_thr, int32_t gradient_size, int32_t norm_type, |
| 65 | BorderMode border_mode, |
| 66 | uint8_t constant_border_value) |
| 67 | { |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 68 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 69 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); |
| 70 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8); |
| 71 | ARM_COMPUTE_ERROR_ON((1 != norm_type) && (2 != norm_type)); |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 72 | ARM_COMPUTE_ERROR_ON((gradient_size != 3) && (gradient_size != 5) && (gradient_size != 7)); |
Michele Di Giorgio | ef91516 | 2018-07-30 12:01:44 +0100 | [diff] [blame] | 73 | ARM_COMPUTE_ERROR_ON((lower_thr < 0) || (lower_thr >= upper_thr)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 74 | |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 75 | _output = output; |
| 76 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 77 | const unsigned int L1_hysteresis_stack_size = 8; |
| 78 | const TensorShape shape = input->info()->tensor_shape(); |
| 79 | |
| 80 | TensorInfo gradient_info; |
| 81 | TensorInfo info; |
| 82 | |
| 83 | // Initialize images |
| 84 | if(gradient_size < 7) |
| 85 | { |
| 86 | gradient_info.init(shape, 1, arm_compute::DataType::S16); |
| 87 | info.init(shape, 1, arm_compute::DataType::U16); |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | gradient_info.init(shape, 1, arm_compute::DataType::S32); |
| 92 | info.init(shape, 1, arm_compute::DataType::U32); |
| 93 | } |
| 94 | |
| 95 | _gx.allocator()->init(gradient_info); |
| 96 | _gy.allocator()->init(gradient_info); |
| 97 | _mag.allocator()->init(info); |
| 98 | _nonmax.allocator()->init(info); |
| 99 | |
| 100 | TensorInfo info_u8(shape, 1, arm_compute::DataType::U8); |
| 101 | _phase.allocator()->init(info_u8); |
| 102 | _l1_list_counter.allocator()->init(info_u8); |
| 103 | |
| 104 | TensorInfo info_u32(shape, 1, arm_compute::DataType::U32); |
| 105 | _visited.allocator()->init(info_u32); |
| 106 | _recorded.allocator()->init(info_u32); |
| 107 | |
| 108 | TensorShape shape_l1_stack = input->info()->tensor_shape(); |
| 109 | shape_l1_stack.set(0, input->info()->dimension(0) * L1_hysteresis_stack_size); |
| 110 | TensorInfo info_s32(shape_l1_stack, 1, arm_compute::DataType::S32); |
| 111 | _l1_stack.allocator()->init(info_s32); |
| 112 | |
Georgios Pinitas | 5701e2a | 2017-09-18 17:43:33 +0100 | [diff] [blame] | 113 | // Manage intermediate buffers |
| 114 | _memory_group.manage(&_gx); |
| 115 | _memory_group.manage(&_gy); |
| 116 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 117 | // Configure/Init sobelNxN |
| 118 | if(gradient_size == 3) |
| 119 | { |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 120 | auto k = arm_compute::support::cpp14::make_unique<CLSobel3x3>(); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 121 | k->configure(compile_context, input, &_gx, &_gy, border_mode, constant_border_value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 122 | _sobel = std::move(k); |
| 123 | } |
| 124 | else if(gradient_size == 5) |
| 125 | { |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 126 | auto k = arm_compute::support::cpp14::make_unique<CLSobel5x5>(); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 127 | k->configure(compile_context, input, &_gx, &_gy, border_mode, constant_border_value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 128 | _sobel = std::move(k); |
| 129 | } |
| 130 | else if(gradient_size == 7) |
| 131 | { |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 132 | auto k = arm_compute::support::cpp14::make_unique<CLSobel7x7>(); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 133 | k->configure(compile_context, input, &_gx, &_gy, border_mode, constant_border_value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 134 | _sobel = std::move(k); |
| 135 | } |
| 136 | else |
| 137 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 138 | ARM_COMPUTE_ERROR_VAR("Gradient size %d not supported", gradient_size); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Georgios Pinitas | 5701e2a | 2017-09-18 17:43:33 +0100 | [diff] [blame] | 141 | // Manage intermediate buffers |
| 142 | _memory_group.manage(&_mag); |
| 143 | _memory_group.manage(&_phase); |
| 144 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 145 | // Configure gradient |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 146 | _gradient.configure(compile_context, &_gx, &_gy, &_mag, &_phase, norm_type); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 147 | |
Georgios Pinitas | 5701e2a | 2017-09-18 17:43:33 +0100 | [diff] [blame] | 148 | // Allocate intermediate buffers |
| 149 | _gx.allocator()->allocate(); |
| 150 | _gy.allocator()->allocate(); |
| 151 | |
| 152 | // Manage intermediate buffers |
| 153 | _memory_group.manage(&_nonmax); |
| 154 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 155 | // Configure non-maxima suppression |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 156 | _non_max_suppr.configure(compile_context, &_mag, &_phase, &_nonmax, lower_thr, border_mode == BorderMode::UNDEFINED); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 157 | |
Georgios Pinitas | 5701e2a | 2017-09-18 17:43:33 +0100 | [diff] [blame] | 158 | // Allocate intermediate buffers |
| 159 | _phase.allocator()->allocate(); |
| 160 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 161 | // Fill border around magnitude image as non-maxima suppression will access |
| 162 | // it. If border mode is undefined filling the border is a nop. |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 163 | _border_mag_gradient.configure(compile_context, &_mag, _non_max_suppr.border_size(), border_mode, constant_border_value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 164 | |
Georgios Pinitas | 5701e2a | 2017-09-18 17:43:33 +0100 | [diff] [blame] | 165 | // Allocate intermediate buffers |
| 166 | _mag.allocator()->allocate(); |
| 167 | |
| 168 | // Manage intermediate buffers |
| 169 | _memory_group.manage(&_visited); |
| 170 | _memory_group.manage(&_recorded); |
| 171 | _memory_group.manage(&_l1_stack); |
| 172 | _memory_group.manage(&_l1_list_counter); |
| 173 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 174 | // Configure edge tracing |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 175 | _edge_trace.configure(compile_context, &_nonmax, output, upper_thr, lower_thr, &_visited, &_recorded, &_l1_stack, &_l1_list_counter); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 176 | |
Georgios Pinitas | 5701e2a | 2017-09-18 17:43:33 +0100 | [diff] [blame] | 177 | // Allocate intermediate buffers |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 178 | _visited.allocator()->allocate(); |
| 179 | _recorded.allocator()->allocate(); |
| 180 | _l1_stack.allocator()->allocate(); |
| 181 | _l1_list_counter.allocator()->allocate(); |
| 182 | _nonmax.allocator()->allocate(); |
| 183 | } |
| 184 | |
| 185 | void CLCannyEdge::run() |
| 186 | { |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 187 | MemoryGroupResourceScope scope_mg(_memory_group); |
Georgios Pinitas | 5701e2a | 2017-09-18 17:43:33 +0100 | [diff] [blame] | 188 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 189 | // Run sobel |
| 190 | _sobel->run(); |
| 191 | |
| 192 | // Run phase and magnitude calculation |
| 193 | CLScheduler::get().enqueue(_gradient, false); |
| 194 | |
| 195 | // Fill border before non-maxima suppression. Nop for border mode undefined. |
| 196 | CLScheduler::get().enqueue(_border_mag_gradient, false); |
| 197 | |
| 198 | // Run non max suppresion |
| 199 | _nonmax.clear(CLScheduler::get().queue()); |
| 200 | CLScheduler::get().enqueue(_non_max_suppr, false); |
| 201 | |
| 202 | // Clear temporary structures and run edge trace |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 203 | _output->clear(CLScheduler::get().queue()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 204 | _visited.clear(CLScheduler::get().queue()); |
| 205 | _recorded.clear(CLScheduler::get().queue()); |
| 206 | _l1_list_counter.clear(CLScheduler::get().queue()); |
| 207 | _l1_stack.clear(CLScheduler::get().queue()); |
| 208 | CLScheduler::get().enqueue(_edge_trace, true); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 209 | } |