blob: 87d573a7adc317f752fa2e6a9da84a40504cfd83 [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
45CLHarrisCorners::CLHarrisCorners()
46 : _sobel(), _harris_score(), _non_max_suppr(), _candidates(), _sort_euclidean(), _border_gx(), _border_gy(), _gx(), _gy(), _score(), _nonmax(), _corners_list(), _num_corner_candidates(0),
47 _corners(nullptr)
48{
49}
50
51void CLHarrisCorners::configure(ICLImage *input, float threshold, float min_dist,
52 float sensitivity, int32_t gradient_size, int32_t block_size, ICLKeyPointArray *corners,
53 BorderMode border_mode, uint8_t constant_border_value)
54{
55 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input);
56 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
57 ARM_COMPUTE_ERROR_ON(!(block_size == 3 || block_size == 5 || block_size == 7));
58 ARM_COMPUTE_ERROR_ON(nullptr == corners);
59
60 _corners = corners;
61
62 const TensorShape shape = input->info()->tensor_shape();
63 const DataType dt = (gradient_size < 7) ? DataType::S16 : DataType::S32;
64 TensorInfo tensor_info(shape, 1, dt);
65 _gx.allocator()->init(tensor_info);
66 _gy.allocator()->init(tensor_info);
67
68 TensorInfo info_f32(shape, 1, DataType::F32);
69 _score.allocator()->init(info_f32);
70 _nonmax.allocator()->init(info_f32);
71
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010072 _corners_list = arm_compute::support::cpp14::make_unique<InternalKeypoint[]>(shape.x() * shape.y());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010073
74 /* Set/init Sobel kernel accordingly with gradient_size */
75 switch(gradient_size)
76 {
77 case 3:
78 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010079 auto k = arm_compute::support::cpp14::make_unique<CLSobel3x3>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
81 _sobel = std::move(k);
82 break;
83 }
84 case 5:
85 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010086 auto k = arm_compute::support::cpp14::make_unique<CLSobel5x5>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
88 _sobel = std::move(k);
89 break;
90 }
91 case 7:
92 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010093 auto k = arm_compute::support::cpp14::make_unique<CLSobel7x7>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
95 _sobel = std::move(k);
96 break;
97 }
98 default:
99 ARM_COMPUTE_ERROR("Gradient size not implemented");
100 }
101
102 // Configure border filling before harris score
steniu01960b0842017-06-23 11:44:34 +0100103 _border_gx.configure(&_gx, BorderSize(block_size / 2), border_mode, constant_border_value);
104 _border_gy.configure(&_gy, BorderSize(block_size / 2), border_mode, constant_border_value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105
106 // Normalization factor
107 const float norm_factor = 1.0f / (255.0f * pow(4.0f, gradient_size / 2) * block_size);
108 const float pow4_normalization_factor = pow(norm_factor, 4);
109
110 // Set/init Harris Score kernel accordingly with block_size
111 _harris_score.configure(&_gx, &_gy, &_score, block_size, pow4_normalization_factor, threshold, sensitivity, border_mode == BorderMode::UNDEFINED);
112
113 // Init non-maxima suppression function
114 _non_max_suppr.configure(&_score, &_nonmax, border_mode == BorderMode::UNDEFINED);
115
116 // Init corner candidates kernel
117 _candidates.configure(&_nonmax, _corners_list.get(), &_num_corner_candidates);
118
119 // Init euclidean distance
120 _sort_euclidean.configure(_corners_list.get(), _corners, &_num_corner_candidates, min_dist);
121
122 // Allocate intermediate buffers
123 _gx.allocator()->allocate();
124 _gy.allocator()->allocate();
125 _score.allocator()->allocate();
126 _nonmax.allocator()->allocate();
127}
128
129void CLHarrisCorners::run()
130{
131 ARM_COMPUTE_ERROR_ON_MSG(_sobel == nullptr, "Unconfigured function");
132
133 // Init to 0 number of corner candidates
134 _num_corner_candidates = 0;
135
136 // Run Sobel kernel
137 _sobel->run();
138
139 // Fill border before harris score kernel
140 CLScheduler::get().enqueue(_border_gx, false);
141 CLScheduler::get().enqueue(_border_gy, false);
142
143 // Run harris score kernel
144 CLScheduler::get().enqueue(_harris_score, false);
145
146 // Run non-maxima suppression
147 CLScheduler::get().enqueue(_non_max_suppr);
148
149 // Run corner candidate kernel
150 _nonmax.map(true);
151 Scheduler::get().schedule(&_candidates, Window::DimY);
152 _nonmax.unmap();
153
154 _corners->map(CLScheduler::get().queue(), true);
155 _sort_euclidean.run(_sort_euclidean.window());
156 _corners->unmap(CLScheduler::get().queue());
157}