blob: 25e28d2213ea7025a013a7af77e47155a8543c3a [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
Georgios Pinitasd910ffa2017-09-18 16:04:42 +010044NEHarrisCorners::NEHarrisCorners(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT
45 : _memory_group(std::move(memory_manager)),
46 _sobel(),
Moritz Pflanzerf4af76e2017-09-06 07:42:43 +010047 _harris_score(),
48 _non_max_suppr(),
49 _candidates(),
50 _sort_euclidean(),
51 _border_gx(),
52 _border_gy(),
53 _gx(),
54 _gy(),
55 _score(),
56 _nonmax(),
57 _corners_list(),
58 _num_corner_candidates(0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059{
60}
61
62void NEHarrisCorners::configure(IImage *input, float threshold, float min_dist,
63 float sensitivity, int32_t gradient_size, int32_t block_size, KeyPointArray *corners,
64 BorderMode border_mode, uint8_t constant_border_value, bool use_fp16)
65{
66 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input);
67 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
68 ARM_COMPUTE_ERROR_ON(!(block_size == 3 || block_size == 5 || block_size == 7));
69
70 const TensorShape shape = input->info()->tensor_shape();
71 TensorInfo tensor_info_gxgy;
72
73 if(gradient_size < 7)
74 {
75 tensor_info_gxgy.init(shape, Format::S16);
76 }
77 else
78 {
79 tensor_info_gxgy.init(shape, Format::S32);
80 }
81
82 _gx.allocator()->init(tensor_info_gxgy);
83 _gy.allocator()->init(tensor_info_gxgy);
84
Georgios Pinitasd910ffa2017-09-18 16:04:42 +010085 // Manage intermediate buffers
86 _memory_group.manage(&_gx);
87 _memory_group.manage(&_gy);
88
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089 TensorInfo tensor_info_score(shape, Format::F32);
90 _score.allocator()->init(tensor_info_score);
91 _nonmax.allocator()->init(tensor_info_score);
92
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010093 _corners_list = arm_compute::support::cpp14::make_unique<InternalKeypoint[]>(shape.x() * shape.y());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094
95 // Set/init Sobel kernel accordingly with gradient_size
96 switch(gradient_size)
97 {
98 case 3:
99 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100100 auto k = arm_compute::support::cpp14::make_unique<NESobel3x3>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
102 _sobel = std::move(k);
103 break;
104 }
105 case 5:
106 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100107 auto k = arm_compute::support::cpp14::make_unique<NESobel5x5>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
109 _sobel = std::move(k);
110 break;
111 }
112 case 7:
113 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100114 auto k = arm_compute::support::cpp14::make_unique<NESobel7x7>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
116 _sobel = std::move(k);
117 break;
118 }
119 default:
120 ARM_COMPUTE_ERROR("Gradient size not implemented");
121 }
122
123 // Normalization factor
124 const float norm_factor = 1.0f / (255.0f * pow(4.0f, gradient_size / 2) * block_size);
125
Georgios Pinitasd910ffa2017-09-18 16:04:42 +0100126 // Manage intermediate buffers
127 _memory_group.manage(&_score);
128
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129 if(use_fp16)
130 {
131 switch(block_size)
132 {
133 case 3:
134 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100135 auto k = arm_compute::support::cpp14::make_unique<NEHarrisScoreFP16Kernel<3>>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136 k->configure(&_gx, &_gy, &_score, norm_factor, threshold, sensitivity, border_mode == BorderMode::UNDEFINED);
137 _harris_score = std::move(k);
138 }
139 break;
140 case 5:
141 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100142 auto k = arm_compute::support::cpp14::make_unique<NEHarrisScoreFP16Kernel<5>>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143 k->configure(&_gx, &_gy, &_score, norm_factor, threshold, sensitivity, border_mode == BorderMode::UNDEFINED);
144 _harris_score = std::move(k);
145 }
146 break;
147 case 7:
148 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100149 auto k = arm_compute::support::cpp14::make_unique<NEHarrisScoreFP16Kernel<7>>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150 k->configure(&_gx, &_gy, &_score, norm_factor, threshold, sensitivity, border_mode == BorderMode::UNDEFINED);
151 _harris_score = std::move(k);
152 }
153 default:
154 break;
155 }
156 }
157 else
158 {
159 // Set/init Harris Score kernel accordingly with block_size
160 switch(block_size)
161 {
162 case 3:
163 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100164 auto k = arm_compute::support::cpp14::make_unique<NEHarrisScoreKernel<3>>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100165 k->configure(&_gx, &_gy, &_score, norm_factor, threshold, sensitivity, border_mode == BorderMode::UNDEFINED);
166 _harris_score = std::move(k);
167 }
168 break;
169 case 5:
170 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100171 auto k = arm_compute::support::cpp14::make_unique<NEHarrisScoreKernel<5>>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100172 k->configure(&_gx, &_gy, &_score, norm_factor, threshold, sensitivity, border_mode == BorderMode::UNDEFINED);
173 _harris_score = std::move(k);
174 }
175 break;
176 case 7:
177 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100178 auto k = arm_compute::support::cpp14::make_unique<NEHarrisScoreKernel<7>>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100179 k->configure(&_gx, &_gy, &_score, norm_factor, threshold, sensitivity, border_mode == BorderMode::UNDEFINED);
180 _harris_score = std::move(k);
181 }
182 default:
183 break;
184 }
185 }
186
187 // Configure border filling before harris score
188 _border_gx.configure(&_gx, _harris_score->border_size(), border_mode, constant_border_value);
189 _border_gy.configure(&_gy, _harris_score->border_size(), border_mode, constant_border_value);
190
Georgios Pinitasd910ffa2017-09-18 16:04:42 +0100191 // Allocate once all the configure methods have been called
192 _gx.allocator()->allocate();
193 _gy.allocator()->allocate();
194
195 // Manage intermediate buffers
196 _memory_group.manage(&_nonmax);
197
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100198 // Init non-maxima suppression function
199 _non_max_suppr.configure(&_score, &_nonmax, border_mode);
200
Georgios Pinitasd910ffa2017-09-18 16:04:42 +0100201 // Allocate once all the configure methods have been called
202 _score.allocator()->allocate();
203
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100204 // Init corner candidates kernel
205 _candidates.configure(&_nonmax, _corners_list.get(), &_num_corner_candidates);
206
Georgios Pinitasd910ffa2017-09-18 16:04:42 +0100207 // Allocate once all the configure methods have been called
208 _nonmax.allocator()->allocate();
209
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100210 // Init euclidean distance
211 _sort_euclidean.configure(_corners_list.get(), corners, &_num_corner_candidates, min_dist);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100212}
213
214void NEHarrisCorners::run()
215{
216 ARM_COMPUTE_ERROR_ON_MSG(_sobel == nullptr, "Unconfigured function");
217
Georgios Pinitasd910ffa2017-09-18 16:04:42 +0100218 _memory_group.acquire();
219
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100220 // Init to 0 number of corner candidates
221 _num_corner_candidates = 0;
222
223 // Run Sobel kernel
224 _sobel->run();
225
226 // Fill border before harris score kernel
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100227 NEScheduler::get().schedule(&_border_gx, Window::DimZ);
228 NEScheduler::get().schedule(&_border_gy, Window::DimZ);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100229
230 // Run harris score kernel
231 NEScheduler::get().schedule(_harris_score.get(), Window::DimY);
232
233 // Run non-maxima suppression
234 _non_max_suppr.run();
235
236 // Run corner candidate kernel
237 NEScheduler::get().schedule(&_candidates, Window::DimY);
238
239 // Run sort & euclidean distance
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100240 NEScheduler::get().schedule(&_sort_euclidean, Window::DimY);
Georgios Pinitasd910ffa2017-09-18 16:04:42 +0100241
242 _memory_group.release();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100243}