blob: 3cae95f55119f0093266ed80180b96fc49039c2e [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{
Alex Gilday64856912018-01-05 10:10:28 +000068 ARM_COMPUTE_UNUSED(use_fp16); //TODO(COMPMID-772): Add half float support
Anthony Barbier6ff3b192017-09-04 18:44:23 +010069 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input);
70 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
71 ARM_COMPUTE_ERROR_ON(!(block_size == 3 || block_size == 5 || block_size == 7));
72 ARM_COMPUTE_ERROR_ON(nullptr == corners);
73
74 _corners = corners;
75
76 const TensorShape shape = input->info()->tensor_shape();
77 const DataType dt = (gradient_size < 7) ? DataType::S16 : DataType::S32;
78 TensorInfo tensor_info(shape, 1, dt);
Giorgio Arenafc2817d2017-06-27 17:26:37 +010079
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080 _gx.allocator()->init(tensor_info);
81 _gy.allocator()->init(tensor_info);
82
83 TensorInfo info_f32(shape, 1, DataType::F32);
84 _score.allocator()->init(info_f32);
85 _nonmax.allocator()->init(info_f32);
86
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010087 _corners_list.resize(shape.x() * shape.y());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088
Georgios Pinitas5701e2a2017-09-18 17:43:33 +010089 // Manage intermediate buffers
90 _memory_group.manage(&_gx);
91 _memory_group.manage(&_gy);
92
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093 /* Set/init Sobel kernel accordingly with gradient_size */
94 switch(gradient_size)
95 {
96 case 3:
97 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010098 auto k = arm_compute::support::cpp14::make_unique<CLSobel3x3>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
100 _sobel = std::move(k);
101 break;
102 }
103 case 5:
104 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100105 auto k = arm_compute::support::cpp14::make_unique<CLSobel5x5>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
107 _sobel = std::move(k);
108 break;
109 }
110 case 7:
111 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100112 auto k = arm_compute::support::cpp14::make_unique<CLSobel7x7>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
114 _sobel = std::move(k);
115 break;
116 }
117 default:
118 ARM_COMPUTE_ERROR("Gradient size not implemented");
119 }
120
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121 // Normalization factor
122 const float norm_factor = 1.0f / (255.0f * pow(4.0f, gradient_size / 2) * block_size);
123 const float pow4_normalization_factor = pow(norm_factor, 4);
124
Georgios Pinitas5701e2a2017-09-18 17:43:33 +0100125 // Manage intermediate buffers
126 _memory_group.manage(&_score);
127
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128 // Set/init Harris Score kernel accordingly with block_size
129 _harris_score.configure(&_gx, &_gy, &_score, block_size, pow4_normalization_factor, threshold, sensitivity, border_mode == BorderMode::UNDEFINED);
130
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100131 // Configure border filling using harris score kernel's block size
132 _border_gx.configure(&_gx, _harris_score.border_size(), border_mode, PixelValue(constant_border_value));
133 _border_gy.configure(&_gy, _harris_score.border_size(), border_mode, PixelValue(constant_border_value));
134
Georgios Pinitas5701e2a2017-09-18 17:43:33 +0100135 // Allocate intermediate buffers
136 _gx.allocator()->allocate();
137 _gy.allocator()->allocate();
138
139 // Manage intermediate buffers
140 _memory_group.manage(&_nonmax);
141
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100142 // Init non-maxima suppression function
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100143 _non_max_suppr.configure(&_score, &_nonmax, border_mode);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100144
Georgios Pinitas5701e2a2017-09-18 17:43:33 +0100145 // Allocate intermediate buffers
146 _score.allocator()->allocate();
147
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100148 // Init corner candidates kernel
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100149 _candidates.configure(&_nonmax, _corners_list.data(), &_num_corner_candidates);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150
Georgios Pinitas5701e2a2017-09-18 17:43:33 +0100151 // Allocate intermediate buffers
152 _nonmax.allocator()->allocate();
153
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154 // Init euclidean distance
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100155 _sort_euclidean.configure(_corners_list.data(), _corners, &_num_corner_candidates, min_dist);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100156}
157
158void CLHarrisCorners::run()
159{
160 ARM_COMPUTE_ERROR_ON_MSG(_sobel == nullptr, "Unconfigured function");
161
Georgios Pinitasda953f22019-04-02 17:27:03 +0100162 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitas5701e2a2017-09-18 17:43:33 +0100163
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100164 // Init to 0 number of corner candidates
165 _num_corner_candidates = 0;
166
167 // Run Sobel kernel
168 _sobel->run();
169
170 // Fill border before harris score kernel
171 CLScheduler::get().enqueue(_border_gx, false);
172 CLScheduler::get().enqueue(_border_gy, false);
173
174 // Run harris score kernel
175 CLScheduler::get().enqueue(_harris_score, false);
176
177 // Run non-maxima suppression
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100178 _non_max_suppr.run();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100179
180 // Run corner candidate kernel
181 _nonmax.map(true);
182 Scheduler::get().schedule(&_candidates, Window::DimY);
183 _nonmax.unmap();
184
185 _corners->map(CLScheduler::get().queue(), true);
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100186 Scheduler::get().schedule(&_sort_euclidean, Window::DimY);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100187 _corners->unmap(CLScheduler::get().queue());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100188}