blob: aecec0d3c53972ce477f62ae64559e3d096edc70 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Matthew Bentham92046462020-03-07 22:15:55 +00002 * Copyright (c) 2016-2020 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 Barbier6ff3b192017-09-04 18:44:23 +010030#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 Bentham92046462020-03-07 22:15:55 +000038#include "support/MemorySupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039
40#include <cmath>
41#include <utility>
42
43using namespace arm_compute;
44
Georgios Pinitas5701e2a2017-09-18 17:43:33 +010045CLHarrisCorners::CLHarrisCorners(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT
46 : _memory_group(std::move(memory_manager)),
47 _sobel(nullptr),
Giorgio Arenafc2817d2017-06-27 17:26:37 +010048 _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 Spyroubcfd09a2019-05-01 13:03:59 +010058 _corners_list(),
Giorgio Arenafc2817d2017-06-27 17:26:37 +010059 _num_corner_candidates(0),
Anthony Barbier6ff3b192017-09-04 18:44:23 +010060 _corners(nullptr)
61{
62}
63
64void CLHarrisCorners::configure(ICLImage *input, float threshold, float min_dist,
65 float sensitivity, int32_t gradient_size, int32_t block_size, ICLKeyPointArray *corners,
Alex Gilday64856912018-01-05 10:10:28 +000066 BorderMode border_mode, uint8_t constant_border_value, bool use_fp16)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010067{
Manuel Bottini2b84be52020-04-08 10:15:51 +010068 configure(CLKernelLibrary::get().get_compile_context(), input, threshold, min_dist, sensitivity, gradient_size, block_size, corners, border_mode, constant_border_value, use_fp16);
69}
70
71void CLHarrisCorners::configure(const CLCompileContext &compile_context, ICLImage *input, float threshold, float min_dist,
72 float sensitivity, int32_t gradient_size, int32_t block_size, ICLKeyPointArray *corners,
73 BorderMode border_mode, uint8_t constant_border_value, bool use_fp16)
74{
Alex Gilday64856912018-01-05 10:10:28 +000075 ARM_COMPUTE_UNUSED(use_fp16); //TODO(COMPMID-772): Add half float support
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input);
77 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
78 ARM_COMPUTE_ERROR_ON(!(block_size == 3 || block_size == 5 || block_size == 7));
79 ARM_COMPUTE_ERROR_ON(nullptr == corners);
80
81 _corners = corners;
82
83 const TensorShape shape = input->info()->tensor_shape();
84 const DataType dt = (gradient_size < 7) ? DataType::S16 : DataType::S32;
85 TensorInfo tensor_info(shape, 1, dt);
Giorgio Arenafc2817d2017-06-27 17:26:37 +010086
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087 _gx.allocator()->init(tensor_info);
88 _gy.allocator()->init(tensor_info);
89
90 TensorInfo info_f32(shape, 1, DataType::F32);
91 _score.allocator()->init(info_f32);
92 _nonmax.allocator()->init(info_f32);
93
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010094 _corners_list.resize(shape.x() * shape.y());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095
Georgios Pinitas5701e2a2017-09-18 17:43:33 +010096 // Manage intermediate buffers
97 _memory_group.manage(&_gx);
98 _memory_group.manage(&_gy);
99
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100 /* Set/init Sobel kernel accordingly with gradient_size */
101 switch(gradient_size)
102 {
103 case 3:
104 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100105 auto k = arm_compute::support::cpp14::make_unique<CLSobel3x3>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100106 k->configure(compile_context, input, &_gx, &_gy, border_mode, constant_border_value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107 _sobel = std::move(k);
108 break;
109 }
110 case 5:
111 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100112 auto k = arm_compute::support::cpp14::make_unique<CLSobel5x5>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100113 k->configure(compile_context, input, &_gx, &_gy, border_mode, constant_border_value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114 _sobel = std::move(k);
115 break;
116 }
117 case 7:
118 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100119 auto k = arm_compute::support::cpp14::make_unique<CLSobel7x7>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100120 k->configure(compile_context, input, &_gx, &_gy, border_mode, constant_border_value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121 _sobel = std::move(k);
122 break;
123 }
124 default:
125 ARM_COMPUTE_ERROR("Gradient size not implemented");
126 }
127
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128 // Normalization factor
129 const float norm_factor = 1.0f / (255.0f * pow(4.0f, gradient_size / 2) * block_size);
130 const float pow4_normalization_factor = pow(norm_factor, 4);
131
Georgios Pinitas5701e2a2017-09-18 17:43:33 +0100132 // Manage intermediate buffers
133 _memory_group.manage(&_score);
134
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100135 // Set/init Harris Score kernel accordingly with block_size
Manuel Bottini2b84be52020-04-08 10:15:51 +0100136 _harris_score.configure(compile_context, &_gx, &_gy, &_score, block_size, pow4_normalization_factor, threshold, sensitivity, border_mode == BorderMode::UNDEFINED);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100138 // Configure border filling using harris score kernel's block size
Manuel Bottini2b84be52020-04-08 10:15:51 +0100139 _border_gx.configure(compile_context, &_gx, _harris_score.border_size(), border_mode, PixelValue(constant_border_value));
140 _border_gy.configure(compile_context, &_gy, _harris_score.border_size(), border_mode, PixelValue(constant_border_value));
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100141
Georgios Pinitas5701e2a2017-09-18 17:43:33 +0100142 // Allocate intermediate buffers
143 _gx.allocator()->allocate();
144 _gy.allocator()->allocate();
145
146 // Manage intermediate buffers
147 _memory_group.manage(&_nonmax);
148
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149 // Init non-maxima suppression function
Manuel Bottini2b84be52020-04-08 10:15:51 +0100150 _non_max_suppr.configure(compile_context, &_score, &_nonmax, border_mode);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151
Georgios Pinitas5701e2a2017-09-18 17:43:33 +0100152 // Allocate intermediate buffers
153 _score.allocator()->allocate();
154
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155 // Init corner candidates kernel
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100156 _candidates.configure(&_nonmax, _corners_list.data(), &_num_corner_candidates);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100157
Georgios Pinitas5701e2a2017-09-18 17:43:33 +0100158 // Allocate intermediate buffers
159 _nonmax.allocator()->allocate();
160
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161 // Init euclidean distance
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100162 _sort_euclidean.configure(_corners_list.data(), _corners, &_num_corner_candidates, min_dist);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100163}
164
165void CLHarrisCorners::run()
166{
167 ARM_COMPUTE_ERROR_ON_MSG(_sobel == nullptr, "Unconfigured function");
168
Georgios Pinitasda953f22019-04-02 17:27:03 +0100169 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitas5701e2a2017-09-18 17:43:33 +0100170
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100171 // Init to 0 number of corner candidates
172 _num_corner_candidates = 0;
173
174 // Run Sobel kernel
175 _sobel->run();
176
177 // Fill border before harris score kernel
178 CLScheduler::get().enqueue(_border_gx, false);
179 CLScheduler::get().enqueue(_border_gy, false);
180
181 // Run harris score kernel
182 CLScheduler::get().enqueue(_harris_score, false);
183
184 // Run non-maxima suppression
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100185 _non_max_suppr.run();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100186
187 // Run corner candidate kernel
188 _nonmax.map(true);
189 Scheduler::get().schedule(&_candidates, Window::DimY);
190 _nonmax.unmap();
191
192 _corners->map(CLScheduler::get().queue(), true);
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100193 Scheduler::get().schedule(&_sort_euclidean, Window::DimY);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100194 _corners->unmap(CLScheduler::get().queue());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100195}