Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 "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" |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 34 | #include "support/ToolchainSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | |
| 36 | using namespace arm_compute; |
| 37 | |
| 38 | CLCannyEdge::CLCannyEdge() |
| 39 | : _sobel(nullptr), _gradient(), _border_mag_gradient(), _non_max_suppr(), _edge_trace(), _gx(), _gy(), _mag(), _phase(), _nonmax(), _visited(), _recorded(), _l1_list_counter(), _l1_stack() |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | 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, uint8_t constant_border_value) |
| 44 | { |
| 45 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); |
| 46 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8); |
| 47 | ARM_COMPUTE_ERROR_ON((1 != norm_type) && (2 != norm_type)); |
| 48 | ARM_COMPUTE_ERROR_ON(lower_thr > upper_thr); |
| 49 | |
| 50 | const unsigned int L1_hysteresis_stack_size = 8; |
| 51 | const TensorShape shape = input->info()->tensor_shape(); |
| 52 | |
| 53 | TensorInfo gradient_info; |
| 54 | TensorInfo info; |
| 55 | |
| 56 | // Initialize images |
| 57 | if(gradient_size < 7) |
| 58 | { |
| 59 | gradient_info.init(shape, 1, arm_compute::DataType::S16); |
| 60 | info.init(shape, 1, arm_compute::DataType::U16); |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | gradient_info.init(shape, 1, arm_compute::DataType::S32); |
| 65 | info.init(shape, 1, arm_compute::DataType::U32); |
| 66 | } |
| 67 | |
| 68 | _gx.allocator()->init(gradient_info); |
| 69 | _gy.allocator()->init(gradient_info); |
| 70 | _mag.allocator()->init(info); |
| 71 | _nonmax.allocator()->init(info); |
| 72 | |
| 73 | TensorInfo info_u8(shape, 1, arm_compute::DataType::U8); |
| 74 | _phase.allocator()->init(info_u8); |
| 75 | _l1_list_counter.allocator()->init(info_u8); |
| 76 | |
| 77 | TensorInfo info_u32(shape, 1, arm_compute::DataType::U32); |
| 78 | _visited.allocator()->init(info_u32); |
| 79 | _recorded.allocator()->init(info_u32); |
| 80 | |
| 81 | TensorShape shape_l1_stack = input->info()->tensor_shape(); |
| 82 | shape_l1_stack.set(0, input->info()->dimension(0) * L1_hysteresis_stack_size); |
| 83 | TensorInfo info_s32(shape_l1_stack, 1, arm_compute::DataType::S32); |
| 84 | _l1_stack.allocator()->init(info_s32); |
| 85 | |
| 86 | // Configure/Init sobelNxN |
| 87 | if(gradient_size == 3) |
| 88 | { |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 89 | auto k = arm_compute::support::cpp14::make_unique<CLSobel3x3>(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 90 | k->configure(input, &_gx, &_gy, border_mode, constant_border_value); |
| 91 | _sobel = std::move(k); |
| 92 | } |
| 93 | else if(gradient_size == 5) |
| 94 | { |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 95 | auto k = arm_compute::support::cpp14::make_unique<CLSobel5x5>(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 96 | k->configure(input, &_gx, &_gy, border_mode, constant_border_value); |
| 97 | _sobel = std::move(k); |
| 98 | } |
| 99 | else if(gradient_size == 7) |
| 100 | { |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 101 | auto k = arm_compute::support::cpp14::make_unique<CLSobel7x7>(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 102 | k->configure(input, &_gx, &_gy, border_mode, constant_border_value); |
| 103 | _sobel = std::move(k); |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | ARM_COMPUTE_ERROR("Gradient %d size not supported", gradient_size); |
| 108 | } |
| 109 | |
| 110 | // Configure gradient |
| 111 | _gradient.configure(&_gx, &_gy, &_mag, &_phase, norm_type); |
| 112 | |
| 113 | // Configure non-maxima suppression |
| 114 | _non_max_suppr.configure(&_mag, &_phase, &_nonmax, lower_thr, border_mode == BorderMode::UNDEFINED); |
| 115 | |
| 116 | // Fill border around magnitude image as non-maxima suppression will access |
| 117 | // it. If border mode is undefined filling the border is a nop. |
| 118 | _border_mag_gradient.configure(&_mag, _non_max_suppr.border_size(), border_mode, constant_border_value); |
| 119 | |
| 120 | // Configure edge tracing |
| 121 | _edge_trace.configure(&_nonmax, output, upper_thr, lower_thr, &_visited, &_recorded, &_l1_stack, &_l1_list_counter); |
| 122 | |
| 123 | _gx.allocator()->allocate(); |
| 124 | _gy.allocator()->allocate(); |
| 125 | _phase.allocator()->allocate(); |
| 126 | _mag.allocator()->allocate(); |
| 127 | _visited.allocator()->allocate(); |
| 128 | _recorded.allocator()->allocate(); |
| 129 | _l1_stack.allocator()->allocate(); |
| 130 | _l1_list_counter.allocator()->allocate(); |
| 131 | _nonmax.allocator()->allocate(); |
| 132 | } |
| 133 | |
| 134 | void CLCannyEdge::run() |
| 135 | { |
| 136 | // Run sobel |
| 137 | _sobel->run(); |
| 138 | |
| 139 | // Run phase and magnitude calculation |
| 140 | CLScheduler::get().enqueue(_gradient, false); |
| 141 | |
| 142 | // Fill border before non-maxima suppression. Nop for border mode undefined. |
| 143 | CLScheduler::get().enqueue(_border_mag_gradient, false); |
| 144 | |
| 145 | // Run non max suppresion |
| 146 | _nonmax.clear(CLScheduler::get().queue()); |
| 147 | CLScheduler::get().enqueue(_non_max_suppr, false); |
| 148 | |
| 149 | // Clear temporary structures and run edge trace |
| 150 | _visited.clear(CLScheduler::get().queue()); |
| 151 | _recorded.clear(CLScheduler::get().queue()); |
| 152 | _l1_list_counter.clear(CLScheduler::get().queue()); |
| 153 | _l1_stack.clear(CLScheduler::get().queue()); |
| 154 | CLScheduler::get().enqueue(_edge_trace, true); |
| 155 | } |