blob: 7ec681dce918e3e23391e13c5107b7c2d07556dc [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
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 Barbier6ff3b192017-09-04 18:44:23 +010027#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 Pflanzerd0ae8b82017-06-29 14:51:57 +010037#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038
39#include <cmath>
40#include <utility>
41
42using namespace arm_compute;
43
Moritz Pflanzerf4af76e2017-09-06 07:42:43 +010044NEHarrisCorners::NEHarrisCorners() // NOLINT
45 : _sobel(),
46 _harris_score(),
47 _non_max_suppr(),
48 _candidates(),
49 _sort_euclidean(),
50 _border_gx(),
51 _border_gy(),
52 _gx(),
53 _gy(),
54 _score(),
55 _nonmax(),
56 _corners_list(),
57 _num_corner_candidates(0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010058{
59}
60
61void NEHarrisCorners::configure(IImage *input, float threshold, float min_dist,
62 float sensitivity, int32_t gradient_size, int32_t block_size, KeyPointArray *corners,
63 BorderMode border_mode, uint8_t constant_border_value, bool use_fp16)
64{
65 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input);
66 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
67 ARM_COMPUTE_ERROR_ON(!(block_size == 3 || block_size == 5 || block_size == 7));
68
69 const TensorShape shape = input->info()->tensor_shape();
70 TensorInfo tensor_info_gxgy;
71
72 if(gradient_size < 7)
73 {
74 tensor_info_gxgy.init(shape, Format::S16);
75 }
76 else
77 {
78 tensor_info_gxgy.init(shape, Format::S32);
79 }
80
81 _gx.allocator()->init(tensor_info_gxgy);
82 _gy.allocator()->init(tensor_info_gxgy);
83
84 TensorInfo tensor_info_score(shape, Format::F32);
85 _score.allocator()->init(tensor_info_score);
86 _nonmax.allocator()->init(tensor_info_score);
87
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010088 _corners_list = arm_compute::support::cpp14::make_unique<InternalKeypoint[]>(shape.x() * shape.y());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089
90 // Set/init Sobel kernel accordingly with gradient_size
91 switch(gradient_size)
92 {
93 case 3:
94 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010095 auto k = arm_compute::support::cpp14::make_unique<NESobel3x3>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
97 _sobel = std::move(k);
98 break;
99 }
100 case 5:
101 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100102 auto k = arm_compute::support::cpp14::make_unique<NESobel5x5>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
104 _sobel = std::move(k);
105 break;
106 }
107 case 7:
108 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100109 auto k = arm_compute::support::cpp14::make_unique<NESobel7x7>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
111 _sobel = std::move(k);
112 break;
113 }
114 default:
115 ARM_COMPUTE_ERROR("Gradient size not implemented");
116 }
117
118 // Normalization factor
119 const float norm_factor = 1.0f / (255.0f * pow(4.0f, gradient_size / 2) * block_size);
120
121 if(use_fp16)
122 {
123 switch(block_size)
124 {
125 case 3:
126 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100127 auto k = arm_compute::support::cpp14::make_unique<NEHarrisScoreFP16Kernel<3>>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128 k->configure(&_gx, &_gy, &_score, norm_factor, threshold, sensitivity, border_mode == BorderMode::UNDEFINED);
129 _harris_score = std::move(k);
130 }
131 break;
132 case 5:
133 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100134 auto k = arm_compute::support::cpp14::make_unique<NEHarrisScoreFP16Kernel<5>>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100135 k->configure(&_gx, &_gy, &_score, norm_factor, threshold, sensitivity, border_mode == BorderMode::UNDEFINED);
136 _harris_score = std::move(k);
137 }
138 break;
139 case 7:
140 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100141 auto k = arm_compute::support::cpp14::make_unique<NEHarrisScoreFP16Kernel<7>>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100142 k->configure(&_gx, &_gy, &_score, norm_factor, threshold, sensitivity, border_mode == BorderMode::UNDEFINED);
143 _harris_score = std::move(k);
144 }
145 default:
146 break;
147 }
148 }
149 else
150 {
151 // Set/init Harris Score kernel accordingly with block_size
152 switch(block_size)
153 {
154 case 3:
155 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100156 auto k = arm_compute::support::cpp14::make_unique<NEHarrisScoreKernel<3>>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100157 k->configure(&_gx, &_gy, &_score, norm_factor, threshold, sensitivity, border_mode == BorderMode::UNDEFINED);
158 _harris_score = std::move(k);
159 }
160 break;
161 case 5:
162 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100163 auto k = arm_compute::support::cpp14::make_unique<NEHarrisScoreKernel<5>>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100164 k->configure(&_gx, &_gy, &_score, norm_factor, threshold, sensitivity, border_mode == BorderMode::UNDEFINED);
165 _harris_score = std::move(k);
166 }
167 break;
168 case 7:
169 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100170 auto k = arm_compute::support::cpp14::make_unique<NEHarrisScoreKernel<7>>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100171 k->configure(&_gx, &_gy, &_score, norm_factor, threshold, sensitivity, border_mode == BorderMode::UNDEFINED);
172 _harris_score = std::move(k);
173 }
174 default:
175 break;
176 }
177 }
178
179 // Configure border filling before harris score
180 _border_gx.configure(&_gx, _harris_score->border_size(), border_mode, constant_border_value);
181 _border_gy.configure(&_gy, _harris_score->border_size(), border_mode, constant_border_value);
182
183 // Init non-maxima suppression function
184 _non_max_suppr.configure(&_score, &_nonmax, border_mode);
185
186 // Init corner candidates kernel
187 _candidates.configure(&_nonmax, _corners_list.get(), &_num_corner_candidates);
188
189 // Init euclidean distance
190 _sort_euclidean.configure(_corners_list.get(), corners, &_num_corner_candidates, min_dist);
191
192 // Allocate once all the configure methods have been called
193 _gx.allocator()->allocate();
194 _gy.allocator()->allocate();
195 _score.allocator()->allocate();
196 _nonmax.allocator()->allocate();
197}
198
199void NEHarrisCorners::run()
200{
201 ARM_COMPUTE_ERROR_ON_MSG(_sobel == nullptr, "Unconfigured function");
202
203 // Init to 0 number of corner candidates
204 _num_corner_candidates = 0;
205
206 // Run Sobel kernel
207 _sobel->run();
208
209 // Fill border before harris score kernel
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100210 NEScheduler::get().schedule(&_border_gx, Window::DimZ);
211 NEScheduler::get().schedule(&_border_gy, Window::DimZ);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100212
213 // Run harris score kernel
214 NEScheduler::get().schedule(_harris_score.get(), Window::DimY);
215
216 // Run non-maxima suppression
217 _non_max_suppr.run();
218
219 // Run corner candidate kernel
220 NEScheduler::get().schedule(&_candidates, Window::DimY);
221
222 // Run sort & euclidean distance
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100223 NEScheduler::get().schedule(&_sort_euclidean, Window::DimY);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100224}