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) 2016-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/CLHarrisCorners.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/OpenCL.h" |
| 27 | #include "arm_compute/core/CL/kernels/CLFillBorderKernel.h" |
| 28 | #include "arm_compute/core/CL/kernels/CLHarrisCornersKernel.h" |
| 29 | #include "arm_compute/core/Error.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/TensorInfo.h" |
| 31 | #include "arm_compute/core/Validate.h" |
| 32 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 33 | #include "arm_compute/runtime/CL/functions/CLSobel3x3.h" |
| 34 | #include "arm_compute/runtime/CL/functions/CLSobel5x5.h" |
| 35 | #include "arm_compute/runtime/CL/functions/CLSobel7x7.h" |
| 36 | #include "arm_compute/runtime/ITensorAllocator.h" |
| 37 | #include "arm_compute/runtime/Scheduler.h" |
Matthew Bentham | 9204646 | 2020-03-07 22:15:55 +0000 | [diff] [blame] | 38 | #include "support/MemorySupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | |
| 40 | #include <cmath> |
| 41 | #include <utility> |
| 42 | |
| 43 | using namespace arm_compute; |
| 44 | |
Georgios Pinitas | 5701e2a | 2017-09-18 17:43:33 +0100 | [diff] [blame] | 45 | CLHarrisCorners::CLHarrisCorners(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT |
| 46 | : _memory_group(std::move(memory_manager)), |
| 47 | _sobel(nullptr), |
Giorgio Arena | fc2817d | 2017-06-27 17:26:37 +0100 | [diff] [blame] | 48 | _harris_score(), |
| 49 | _non_max_suppr(), |
| 50 | _candidates(), |
| 51 | _sort_euclidean(), |
| 52 | _border_gx(), |
| 53 | _border_gy(), |
| 54 | _gx(), |
| 55 | _gy(), |
| 56 | _score(), |
| 57 | _nonmax(), |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 58 | _corners_list(), |
Giorgio Arena | fc2817d | 2017-06-27 17:26:37 +0100 | [diff] [blame] | 59 | _num_corner_candidates(0), |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 60 | _corners(nullptr) |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | void CLHarrisCorners::configure(ICLImage *input, float threshold, float min_dist, |
| 65 | float sensitivity, int32_t gradient_size, int32_t block_size, ICLKeyPointArray *corners, |
Alex Gilday | 6485691 | 2018-01-05 10:10:28 +0000 | [diff] [blame] | 66 | BorderMode border_mode, uint8_t constant_border_value, bool use_fp16) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 67 | { |
Alex Gilday | 6485691 | 2018-01-05 10:10:28 +0000 | [diff] [blame] | 68 | ARM_COMPUTE_UNUSED(use_fp16); //TODO(COMPMID-772): Add half float support |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 69 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input); |
| 70 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); |
| 71 | ARM_COMPUTE_ERROR_ON(!(block_size == 3 || block_size == 5 || block_size == 7)); |
| 72 | ARM_COMPUTE_ERROR_ON(nullptr == corners); |
| 73 | |
| 74 | _corners = corners; |
| 75 | |
| 76 | const TensorShape shape = input->info()->tensor_shape(); |
| 77 | const DataType dt = (gradient_size < 7) ? DataType::S16 : DataType::S32; |
| 78 | TensorInfo tensor_info(shape, 1, dt); |
Giorgio Arena | fc2817d | 2017-06-27 17:26:37 +0100 | [diff] [blame] | 79 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 80 | _gx.allocator()->init(tensor_info); |
| 81 | _gy.allocator()->init(tensor_info); |
| 82 | |
| 83 | TensorInfo info_f32(shape, 1, DataType::F32); |
| 84 | _score.allocator()->init(info_f32); |
| 85 | _nonmax.allocator()->init(info_f32); |
| 86 | |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 87 | _corners_list.resize(shape.x() * shape.y()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 88 | |
Georgios Pinitas | 5701e2a | 2017-09-18 17:43:33 +0100 | [diff] [blame] | 89 | // Manage intermediate buffers |
| 90 | _memory_group.manage(&_gx); |
| 91 | _memory_group.manage(&_gy); |
| 92 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 93 | /* Set/init Sobel kernel accordingly with gradient_size */ |
| 94 | switch(gradient_size) |
| 95 | { |
| 96 | case 3: |
| 97 | { |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 98 | auto k = arm_compute::support::cpp14::make_unique<CLSobel3x3>(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 99 | k->configure(input, &_gx, &_gy, border_mode, constant_border_value); |
| 100 | _sobel = std::move(k); |
| 101 | break; |
| 102 | } |
| 103 | case 5: |
| 104 | { |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 105 | auto k = arm_compute::support::cpp14::make_unique<CLSobel5x5>(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 106 | k->configure(input, &_gx, &_gy, border_mode, constant_border_value); |
| 107 | _sobel = std::move(k); |
| 108 | break; |
| 109 | } |
| 110 | case 7: |
| 111 | { |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 112 | auto k = arm_compute::support::cpp14::make_unique<CLSobel7x7>(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 113 | k->configure(input, &_gx, &_gy, border_mode, constant_border_value); |
| 114 | _sobel = std::move(k); |
| 115 | break; |
| 116 | } |
| 117 | default: |
| 118 | ARM_COMPUTE_ERROR("Gradient size not implemented"); |
| 119 | } |
| 120 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 121 | // Normalization factor |
| 122 | const float norm_factor = 1.0f / (255.0f * pow(4.0f, gradient_size / 2) * block_size); |
| 123 | const float pow4_normalization_factor = pow(norm_factor, 4); |
| 124 | |
Georgios Pinitas | 5701e2a | 2017-09-18 17:43:33 +0100 | [diff] [blame] | 125 | // Manage intermediate buffers |
| 126 | _memory_group.manage(&_score); |
| 127 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 128 | // Set/init Harris Score kernel accordingly with block_size |
| 129 | _harris_score.configure(&_gx, &_gy, &_score, block_size, pow4_normalization_factor, threshold, sensitivity, border_mode == BorderMode::UNDEFINED); |
| 130 | |
Giorgio Arena | fc2817d | 2017-06-27 17:26:37 +0100 | [diff] [blame] | 131 | // Configure border filling using harris score kernel's block size |
| 132 | _border_gx.configure(&_gx, _harris_score.border_size(), border_mode, PixelValue(constant_border_value)); |
| 133 | _border_gy.configure(&_gy, _harris_score.border_size(), border_mode, PixelValue(constant_border_value)); |
| 134 | |
Georgios Pinitas | 5701e2a | 2017-09-18 17:43:33 +0100 | [diff] [blame] | 135 | // Allocate intermediate buffers |
| 136 | _gx.allocator()->allocate(); |
| 137 | _gy.allocator()->allocate(); |
| 138 | |
| 139 | // Manage intermediate buffers |
| 140 | _memory_group.manage(&_nonmax); |
| 141 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 142 | // Init non-maxima suppression function |
Giorgio Arena | fc2817d | 2017-06-27 17:26:37 +0100 | [diff] [blame] | 143 | _non_max_suppr.configure(&_score, &_nonmax, border_mode); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 144 | |
Georgios Pinitas | 5701e2a | 2017-09-18 17:43:33 +0100 | [diff] [blame] | 145 | // Allocate intermediate buffers |
| 146 | _score.allocator()->allocate(); |
| 147 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 148 | // Init corner candidates kernel |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 149 | _candidates.configure(&_nonmax, _corners_list.data(), &_num_corner_candidates); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 150 | |
Georgios Pinitas | 5701e2a | 2017-09-18 17:43:33 +0100 | [diff] [blame] | 151 | // Allocate intermediate buffers |
| 152 | _nonmax.allocator()->allocate(); |
| 153 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 154 | // Init euclidean distance |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 155 | _sort_euclidean.configure(_corners_list.data(), _corners, &_num_corner_candidates, min_dist); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | void CLHarrisCorners::run() |
| 159 | { |
| 160 | ARM_COMPUTE_ERROR_ON_MSG(_sobel == nullptr, "Unconfigured function"); |
| 161 | |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 162 | MemoryGroupResourceScope scope_mg(_memory_group); |
Georgios Pinitas | 5701e2a | 2017-09-18 17:43:33 +0100 | [diff] [blame] | 163 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 164 | // Init to 0 number of corner candidates |
| 165 | _num_corner_candidates = 0; |
| 166 | |
| 167 | // Run Sobel kernel |
| 168 | _sobel->run(); |
| 169 | |
| 170 | // Fill border before harris score kernel |
| 171 | CLScheduler::get().enqueue(_border_gx, false); |
| 172 | CLScheduler::get().enqueue(_border_gy, false); |
| 173 | |
| 174 | // Run harris score kernel |
| 175 | CLScheduler::get().enqueue(_harris_score, false); |
| 176 | |
| 177 | // Run non-maxima suppression |
Giorgio Arena | fc2817d | 2017-06-27 17:26:37 +0100 | [diff] [blame] | 178 | _non_max_suppr.run(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 179 | |
| 180 | // Run corner candidate kernel |
| 181 | _nonmax.map(true); |
| 182 | Scheduler::get().schedule(&_candidates, Window::DimY); |
| 183 | _nonmax.unmap(); |
| 184 | |
| 185 | _corners->map(CLScheduler::get().queue(), true); |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 186 | Scheduler::get().schedule(&_sort_euclidean, Window::DimY); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 187 | _corners->unmap(CLScheduler::get().queue()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 188 | } |