blob: 37f428c677c6fc60a6cccd463bd8fe182109ee47 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * 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"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/Error.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/TensorInfo.h"
29#include "arm_compute/core/Validate.h"
30#include "arm_compute/runtime/CL/CLScheduler.h"
31#include "arm_compute/runtime/CL/functions/CLSobel3x3.h"
32#include "arm_compute/runtime/CL/functions/CLSobel5x5.h"
33#include "arm_compute/runtime/CL/functions/CLSobel7x7.h"
34#include "arm_compute/runtime/ITensorAllocator.h"
35#include "arm_compute/runtime/Scheduler.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010036#include "src/core/CL/kernels/CLFillBorderKernel.h"
37#include "src/core/CL/kernels/CLHarrisCornersKernel.h"
38#include "src/core/CL/kernels/CLSobel5x5Kernel.h"
39#include "src/core/CL/kernels/CLSobel7x7Kernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040
41#include <cmath>
42#include <utility>
43
44using namespace arm_compute;
45
Georgios Pinitas5701e2a2017-09-18 17:43:33 +010046CLHarrisCorners::CLHarrisCorners(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT
47 : _memory_group(std::move(memory_manager)),
48 _sobel(nullptr),
Georgios Pinitas40f51a62020-11-21 03:04:18 +000049 _harris_score(std::make_unique<CLHarrisScoreKernel>()),
Giorgio Arenafc2817d2017-06-27 17:26:37 +010050 _non_max_suppr(),
51 _candidates(),
52 _sort_euclidean(),
Georgios Pinitas40f51a62020-11-21 03:04:18 +000053 _border_gx(std::make_unique<CLFillBorderKernel>()),
54 _border_gy(std::make_unique<CLFillBorderKernel>()),
Giorgio Arenafc2817d2017-06-27 17:26:37 +010055 _gx(),
56 _gy(),
57 _score(),
58 _nonmax(),
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010059 _corners_list(),
Giorgio Arenafc2817d2017-06-27 17:26:37 +010060 _num_corner_candidates(0),
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061 _corners(nullptr)
62{
63}
64
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010065CLHarrisCorners::~CLHarrisCorners() = default;
66
Anthony Barbier6ff3b192017-09-04 18:44:23 +010067void CLHarrisCorners::configure(ICLImage *input, float threshold, float min_dist,
68 float sensitivity, int32_t gradient_size, int32_t block_size, ICLKeyPointArray *corners,
Alex Gilday64856912018-01-05 10:10:28 +000069 BorderMode border_mode, uint8_t constant_border_value, bool use_fp16)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070{
Manuel Bottini2b84be52020-04-08 10:15:51 +010071 configure(CLKernelLibrary::get().get_compile_context(), input, threshold, min_dist, sensitivity, gradient_size, block_size, corners, border_mode, constant_border_value, use_fp16);
72}
73
74void CLHarrisCorners::configure(const CLCompileContext &compile_context, ICLImage *input, float threshold, float min_dist,
75 float sensitivity, int32_t gradient_size, int32_t block_size, ICLKeyPointArray *corners,
76 BorderMode border_mode, uint8_t constant_border_value, bool use_fp16)
77{
Alex Gilday64856912018-01-05 10:10:28 +000078 ARM_COMPUTE_UNUSED(use_fp16); //TODO(COMPMID-772): Add half float support
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input);
80 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
81 ARM_COMPUTE_ERROR_ON(!(block_size == 3 || block_size == 5 || block_size == 7));
82 ARM_COMPUTE_ERROR_ON(nullptr == corners);
83
84 _corners = corners;
85
86 const TensorShape shape = input->info()->tensor_shape();
87 const DataType dt = (gradient_size < 7) ? DataType::S16 : DataType::S32;
88 TensorInfo tensor_info(shape, 1, dt);
Giorgio Arenafc2817d2017-06-27 17:26:37 +010089
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090 _gx.allocator()->init(tensor_info);
91 _gy.allocator()->init(tensor_info);
92
93 TensorInfo info_f32(shape, 1, DataType::F32);
94 _score.allocator()->init(info_f32);
95 _nonmax.allocator()->init(info_f32);
96
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010097 _corners_list.resize(shape.x() * shape.y());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098
Georgios Pinitas5701e2a2017-09-18 17:43:33 +010099 // Manage intermediate buffers
100 _memory_group.manage(&_gx);
101 _memory_group.manage(&_gy);
102
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103 /* Set/init Sobel kernel accordingly with gradient_size */
104 switch(gradient_size)
105 {
106 case 3:
107 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000108 auto k = std::make_unique<CLSobel3x3>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100109 k->configure(compile_context, input, &_gx, &_gy, border_mode, constant_border_value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110 _sobel = std::move(k);
111 break;
112 }
113 case 5:
114 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000115 auto k = std::make_unique<CLSobel5x5>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100116 k->configure(compile_context, input, &_gx, &_gy, border_mode, constant_border_value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100117 _sobel = std::move(k);
118 break;
119 }
120 case 7:
121 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000122 auto k = std::make_unique<CLSobel7x7>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100123 k->configure(compile_context, input, &_gx, &_gy, border_mode, constant_border_value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124 _sobel = std::move(k);
125 break;
126 }
127 default:
128 ARM_COMPUTE_ERROR("Gradient size not implemented");
129 }
130
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100131 // Normalization factor
132 const float norm_factor = 1.0f / (255.0f * pow(4.0f, gradient_size / 2) * block_size);
133 const float pow4_normalization_factor = pow(norm_factor, 4);
134
Georgios Pinitas5701e2a2017-09-18 17:43:33 +0100135 // Manage intermediate buffers
136 _memory_group.manage(&_score);
137
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100138 // Set/init Harris Score kernel accordingly with block_size
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100139 _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 +0100140
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100141 // Configure border filling using harris score kernel's block size
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100142 _border_gx->configure(compile_context, &_gx, _harris_score->border_size(), border_mode, PixelValue(constant_border_value));
143 _border_gy->configure(compile_context, &_gy, _harris_score->border_size(), border_mode, PixelValue(constant_border_value));
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100144
Georgios Pinitas5701e2a2017-09-18 17:43:33 +0100145 // Allocate intermediate buffers
146 _gx.allocator()->allocate();
147 _gy.allocator()->allocate();
148
149 // Manage intermediate buffers
150 _memory_group.manage(&_nonmax);
151
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152 // Init non-maxima suppression function
Manuel Bottini2b84be52020-04-08 10:15:51 +0100153 _non_max_suppr.configure(compile_context, &_score, &_nonmax, border_mode);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154
Georgios Pinitas5701e2a2017-09-18 17:43:33 +0100155 // Allocate intermediate buffers
156 _score.allocator()->allocate();
157
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158 // Init corner candidates kernel
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100159 _candidates.configure(&_nonmax, _corners_list.data(), &_num_corner_candidates);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160
Georgios Pinitas5701e2a2017-09-18 17:43:33 +0100161 // Allocate intermediate buffers
162 _nonmax.allocator()->allocate();
163
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100164 // Init euclidean distance
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100165 _sort_euclidean.configure(_corners_list.data(), _corners, &_num_corner_candidates, min_dist);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100166}
167
168void CLHarrisCorners::run()
169{
170 ARM_COMPUTE_ERROR_ON_MSG(_sobel == nullptr, "Unconfigured function");
171
Georgios Pinitasda953f22019-04-02 17:27:03 +0100172 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitas5701e2a2017-09-18 17:43:33 +0100173
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100174 // Init to 0 number of corner candidates
175 _num_corner_candidates = 0;
176
177 // Run Sobel kernel
178 _sobel->run();
179
180 // Fill border before harris score kernel
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100181 CLScheduler::get().enqueue(*_border_gx, false);
182 CLScheduler::get().enqueue(*_border_gy, false);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100183
184 // Run harris score kernel
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100185 CLScheduler::get().enqueue(*_harris_score, false);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100186
187 // Run non-maxima suppression
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100188 _non_max_suppr.run();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100189
190 // Run corner candidate kernel
191 _nonmax.map(true);
192 Scheduler::get().schedule(&_candidates, Window::DimY);
193 _nonmax.unmap();
194
195 _corners->map(CLScheduler::get().queue(), true);
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100196 Scheduler::get().schedule(&_sort_euclidean, Window::DimY);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100197 _corners->unmap(CLScheduler::get().queue());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100198}