blob: 2140240753de0fa6e1cb73f3cbd369b559e5472f [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/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"
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010038#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039
40#include <cmath>
41#include <utility>
42
43using namespace arm_compute;
44
Giorgio Arenafc2817d2017-06-27 17:26:37 +010045CLHarrisCorners::CLHarrisCorners() // NOLINT
46 : _sobel(nullptr),
47 _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(nullptr),
58 _num_corner_candidates(0),
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059 _corners(nullptr)
60{
61}
62
63void CLHarrisCorners::configure(ICLImage *input, float threshold, float min_dist,
64 float sensitivity, int32_t gradient_size, int32_t block_size, ICLKeyPointArray *corners,
65 BorderMode border_mode, uint8_t constant_border_value)
66{
67 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input);
68 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
69 ARM_COMPUTE_ERROR_ON(!(block_size == 3 || block_size == 5 || block_size == 7));
70 ARM_COMPUTE_ERROR_ON(nullptr == corners);
71
72 _corners = corners;
73
74 const TensorShape shape = input->info()->tensor_shape();
75 const DataType dt = (gradient_size < 7) ? DataType::S16 : DataType::S32;
76 TensorInfo tensor_info(shape, 1, dt);
Giorgio Arenafc2817d2017-06-27 17:26:37 +010077
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078 _gx.allocator()->init(tensor_info);
79 _gy.allocator()->init(tensor_info);
80
81 TensorInfo info_f32(shape, 1, DataType::F32);
82 _score.allocator()->init(info_f32);
83 _nonmax.allocator()->init(info_f32);
84
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010085 _corners_list = arm_compute::support::cpp14::make_unique<InternalKeypoint[]>(shape.x() * shape.y());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086
87 /* Set/init Sobel kernel accordingly with gradient_size */
88 switch(gradient_size)
89 {
90 case 3:
91 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010092 auto k = arm_compute::support::cpp14::make_unique<CLSobel3x3>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
94 _sobel = std::move(k);
95 break;
96 }
97 case 5:
98 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010099 auto k = arm_compute::support::cpp14::make_unique<CLSobel5x5>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
101 _sobel = std::move(k);
102 break;
103 }
104 case 7:
105 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100106 auto k = arm_compute::support::cpp14::make_unique<CLSobel7x7>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
108 _sobel = std::move(k);
109 break;
110 }
111 default:
112 ARM_COMPUTE_ERROR("Gradient size not implemented");
113 }
114
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115 // Normalization factor
116 const float norm_factor = 1.0f / (255.0f * pow(4.0f, gradient_size / 2) * block_size);
117 const float pow4_normalization_factor = pow(norm_factor, 4);
118
119 // Set/init Harris Score kernel accordingly with block_size
120 _harris_score.configure(&_gx, &_gy, &_score, block_size, pow4_normalization_factor, threshold, sensitivity, border_mode == BorderMode::UNDEFINED);
121
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100122 // Configure border filling using harris score kernel's block size
123 _border_gx.configure(&_gx, _harris_score.border_size(), border_mode, PixelValue(constant_border_value));
124 _border_gy.configure(&_gy, _harris_score.border_size(), border_mode, PixelValue(constant_border_value));
125
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100126 // Init non-maxima suppression function
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100127 _non_max_suppr.configure(&_score, &_nonmax, border_mode);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128
129 // Init corner candidates kernel
130 _candidates.configure(&_nonmax, _corners_list.get(), &_num_corner_candidates);
131
132 // Init euclidean distance
133 _sort_euclidean.configure(_corners_list.get(), _corners, &_num_corner_candidates, min_dist);
134
135 // Allocate intermediate buffers
136 _gx.allocator()->allocate();
137 _gy.allocator()->allocate();
138 _score.allocator()->allocate();
139 _nonmax.allocator()->allocate();
140}
141
142void CLHarrisCorners::run()
143{
144 ARM_COMPUTE_ERROR_ON_MSG(_sobel == nullptr, "Unconfigured function");
145
146 // Init to 0 number of corner candidates
147 _num_corner_candidates = 0;
148
149 // Run Sobel kernel
150 _sobel->run();
151
152 // Fill border before harris score kernel
153 CLScheduler::get().enqueue(_border_gx, false);
154 CLScheduler::get().enqueue(_border_gy, false);
155
156 // Run harris score kernel
157 CLScheduler::get().enqueue(_harris_score, false);
158
159 // Run non-maxima suppression
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100160 _non_max_suppr.run();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161
162 // Run corner candidate kernel
163 _nonmax.map(true);
164 Scheduler::get().schedule(&_candidates, Window::DimY);
165 _nonmax.unmap();
166
167 _corners->map(CLScheduler::get().queue(), true);
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100168 Scheduler::get().schedule(&_sort_euclidean, Window::DimY);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100169 _corners->unmap(CLScheduler::get().queue());
170}