Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, 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/NEON/functions/NEHarrisCorners.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/NEON/kernels/NEFillBorderKernel.h" |
| 28 | #include "arm_compute/core/NEON/kernels/NEHarrisCornersKernel.h" |
| 29 | #include "arm_compute/core/TensorInfo.h" |
| 30 | #include "arm_compute/core/Validate.h" |
| 31 | #include "arm_compute/runtime/Array.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" |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame^] | 37 | #include "support/ToolchainSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 38 | |
| 39 | #include <cmath> |
| 40 | #include <utility> |
| 41 | |
| 42 | using namespace arm_compute; |
| 43 | |
| 44 | NEHarrisCorners::NEHarrisCorners() |
| 45 | : _sobel(), _harris_score(), _non_max_suppr(), _candidates(), _sort_euclidean(), _border_gx(), _border_gy(), _gx(), _gy(), _score(), _nonmax(), _corners_list(), _num_corner_candidates(0) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | void NEHarrisCorners::configure(IImage *input, float threshold, float min_dist, |
| 50 | float sensitivity, int32_t gradient_size, int32_t block_size, KeyPointArray *corners, |
| 51 | BorderMode border_mode, uint8_t constant_border_value, bool use_fp16) |
| 52 | { |
| 53 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input); |
| 54 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); |
| 55 | ARM_COMPUTE_ERROR_ON(!(block_size == 3 || block_size == 5 || block_size == 7)); |
| 56 | |
| 57 | const TensorShape shape = input->info()->tensor_shape(); |
| 58 | TensorInfo tensor_info_gxgy; |
| 59 | |
| 60 | if(gradient_size < 7) |
| 61 | { |
| 62 | tensor_info_gxgy.init(shape, Format::S16); |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | tensor_info_gxgy.init(shape, Format::S32); |
| 67 | } |
| 68 | |
| 69 | _gx.allocator()->init(tensor_info_gxgy); |
| 70 | _gy.allocator()->init(tensor_info_gxgy); |
| 71 | |
| 72 | TensorInfo tensor_info_score(shape, Format::F32); |
| 73 | _score.allocator()->init(tensor_info_score); |
| 74 | _nonmax.allocator()->init(tensor_info_score); |
| 75 | |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame^] | 76 | _corners_list = arm_compute::support::cpp14::make_unique<InternalKeypoint[]>(shape.x() * shape.y()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 77 | |
| 78 | // Set/init Sobel kernel accordingly with gradient_size |
| 79 | switch(gradient_size) |
| 80 | { |
| 81 | case 3: |
| 82 | { |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame^] | 83 | auto k = arm_compute::support::cpp14::make_unique<NESobel3x3>(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 84 | k->configure(input, &_gx, &_gy, border_mode, constant_border_value); |
| 85 | _sobel = std::move(k); |
| 86 | break; |
| 87 | } |
| 88 | case 5: |
| 89 | { |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame^] | 90 | auto k = arm_compute::support::cpp14::make_unique<NESobel5x5>(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 91 | k->configure(input, &_gx, &_gy, border_mode, constant_border_value); |
| 92 | _sobel = std::move(k); |
| 93 | break; |
| 94 | } |
| 95 | case 7: |
| 96 | { |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame^] | 97 | auto k = arm_compute::support::cpp14::make_unique<NESobel7x7>(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 98 | k->configure(input, &_gx, &_gy, border_mode, constant_border_value); |
| 99 | _sobel = std::move(k); |
| 100 | break; |
| 101 | } |
| 102 | default: |
| 103 | ARM_COMPUTE_ERROR("Gradient size not implemented"); |
| 104 | } |
| 105 | |
| 106 | // Normalization factor |
| 107 | const float norm_factor = 1.0f / (255.0f * pow(4.0f, gradient_size / 2) * block_size); |
| 108 | |
| 109 | if(use_fp16) |
| 110 | { |
| 111 | switch(block_size) |
| 112 | { |
| 113 | case 3: |
| 114 | { |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame^] | 115 | auto k = arm_compute::support::cpp14::make_unique<NEHarrisScoreFP16Kernel<3>>(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 116 | k->configure(&_gx, &_gy, &_score, norm_factor, threshold, sensitivity, border_mode == BorderMode::UNDEFINED); |
| 117 | _harris_score = std::move(k); |
| 118 | } |
| 119 | break; |
| 120 | case 5: |
| 121 | { |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame^] | 122 | auto k = arm_compute::support::cpp14::make_unique<NEHarrisScoreFP16Kernel<5>>(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 123 | k->configure(&_gx, &_gy, &_score, norm_factor, threshold, sensitivity, border_mode == BorderMode::UNDEFINED); |
| 124 | _harris_score = std::move(k); |
| 125 | } |
| 126 | break; |
| 127 | case 7: |
| 128 | { |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame^] | 129 | auto k = arm_compute::support::cpp14::make_unique<NEHarrisScoreFP16Kernel<7>>(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 130 | k->configure(&_gx, &_gy, &_score, norm_factor, threshold, sensitivity, border_mode == BorderMode::UNDEFINED); |
| 131 | _harris_score = std::move(k); |
| 132 | } |
| 133 | default: |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | // Set/init Harris Score kernel accordingly with block_size |
| 140 | switch(block_size) |
| 141 | { |
| 142 | case 3: |
| 143 | { |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame^] | 144 | auto k = arm_compute::support::cpp14::make_unique<NEHarrisScoreKernel<3>>(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 145 | k->configure(&_gx, &_gy, &_score, norm_factor, threshold, sensitivity, border_mode == BorderMode::UNDEFINED); |
| 146 | _harris_score = std::move(k); |
| 147 | } |
| 148 | break; |
| 149 | case 5: |
| 150 | { |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame^] | 151 | auto k = arm_compute::support::cpp14::make_unique<NEHarrisScoreKernel<5>>(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 152 | k->configure(&_gx, &_gy, &_score, norm_factor, threshold, sensitivity, border_mode == BorderMode::UNDEFINED); |
| 153 | _harris_score = std::move(k); |
| 154 | } |
| 155 | break; |
| 156 | case 7: |
| 157 | { |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame^] | 158 | auto k = arm_compute::support::cpp14::make_unique<NEHarrisScoreKernel<7>>(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 159 | k->configure(&_gx, &_gy, &_score, norm_factor, threshold, sensitivity, border_mode == BorderMode::UNDEFINED); |
| 160 | _harris_score = std::move(k); |
| 161 | } |
| 162 | default: |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // Configure border filling before harris score |
| 168 | _border_gx.configure(&_gx, _harris_score->border_size(), border_mode, constant_border_value); |
| 169 | _border_gy.configure(&_gy, _harris_score->border_size(), border_mode, constant_border_value); |
| 170 | |
| 171 | // Init non-maxima suppression function |
| 172 | _non_max_suppr.configure(&_score, &_nonmax, border_mode); |
| 173 | |
| 174 | // Init corner candidates kernel |
| 175 | _candidates.configure(&_nonmax, _corners_list.get(), &_num_corner_candidates); |
| 176 | |
| 177 | // Init euclidean distance |
| 178 | _sort_euclidean.configure(_corners_list.get(), corners, &_num_corner_candidates, min_dist); |
| 179 | |
| 180 | // Allocate once all the configure methods have been called |
| 181 | _gx.allocator()->allocate(); |
| 182 | _gy.allocator()->allocate(); |
| 183 | _score.allocator()->allocate(); |
| 184 | _nonmax.allocator()->allocate(); |
| 185 | } |
| 186 | |
| 187 | void NEHarrisCorners::run() |
| 188 | { |
| 189 | ARM_COMPUTE_ERROR_ON_MSG(_sobel == nullptr, "Unconfigured function"); |
| 190 | |
| 191 | // Init to 0 number of corner candidates |
| 192 | _num_corner_candidates = 0; |
| 193 | |
| 194 | // Run Sobel kernel |
| 195 | _sobel->run(); |
| 196 | |
| 197 | // Fill border before harris score kernel |
| 198 | _border_gx.run(_border_gx.window()); |
| 199 | _border_gy.run(_border_gy.window()); |
| 200 | |
| 201 | // Run harris score kernel |
| 202 | NEScheduler::get().schedule(_harris_score.get(), Window::DimY); |
| 203 | |
| 204 | // Run non-maxima suppression |
| 205 | _non_max_suppr.run(); |
| 206 | |
| 207 | // Run corner candidate kernel |
| 208 | NEScheduler::get().schedule(&_candidates, Window::DimY); |
| 209 | |
| 210 | // Run sort & euclidean distance |
| 211 | _sort_euclidean.run(_sort_euclidean.window()); |
| 212 | } |