Compute Library
 20.08
NEHarrisCorners.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2020 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  */
25 
26 #include "arm_compute/core/Error.h"
37 #include "support/MemorySupport.h"
38 
39 #include <cmath>
40 #include <utility>
41 
42 using namespace arm_compute;
43 
44 NEHarrisCorners::NEHarrisCorners(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT
45  : _memory_group(std::move(memory_manager)),
46  _sobel(),
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(),
58  _num_corner_candidates(0)
59 {
60 }
61 
62 void NEHarrisCorners::configure(IImage *input, float threshold, float min_dist,
63  float sensitivity, int32_t gradient_size, int32_t block_size, KeyPointArray *corners,
64  BorderMode border_mode, uint8_t constant_border_value)
65 {
68  ARM_COMPUTE_ERROR_ON(!(block_size == 3 || block_size == 5 || block_size == 7));
69 
70  const TensorShape shape = input->info()->tensor_shape();
71  TensorInfo tensor_info_gxgy;
72 
73  if(gradient_size < 7)
74  {
75  tensor_info_gxgy.init(shape, Format::S16);
76  }
77  else
78  {
79  tensor_info_gxgy.init(shape, Format::S32);
80  }
81 
82  _gx.allocator()->init(tensor_info_gxgy);
83  _gy.allocator()->init(tensor_info_gxgy);
84 
85  // Manage intermediate buffers
86  _memory_group.manage(&_gx);
87  _memory_group.manage(&_gy);
88 
89  TensorInfo tensor_info_score(shape, Format::F32);
90  _score.allocator()->init(tensor_info_score);
91  _nonmax.allocator()->init(tensor_info_score);
92 
93  _corners_list.resize(shape.x() * shape.y());
94 
95  // Set/init Sobel kernel accordingly with gradient_size
96  switch(gradient_size)
97  {
98  case 3:
99  {
100  auto k = arm_compute::support::cpp14::make_unique<NESobel3x3>();
101  k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
102  _sobel = std::move(k);
103  break;
104  }
105  case 5:
106  {
107  auto k = arm_compute::support::cpp14::make_unique<NESobel5x5>();
108  k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
109  _sobel = std::move(k);
110  break;
111  }
112  case 7:
113  {
114  auto k = arm_compute::support::cpp14::make_unique<NESobel7x7>();
115  k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
116  _sobel = std::move(k);
117  break;
118  }
119  default:
120  ARM_COMPUTE_ERROR("Gradient size not implemented");
121  }
122 
123  // Normalization factor
124  const float norm_factor = 1.0f / (255.0f * pow(4.0f, gradient_size / 2) * block_size);
125 
126  // Manage intermediate buffers
127  _memory_group.manage(&_score);
128 
129  // Set/init Harris Score kernel accordingly with block_size
130  switch(block_size)
131  {
132  case 3:
133  {
134  auto k = arm_compute::support::cpp14::make_unique<NEHarrisScoreKernel<3>>();
135  k->configure(&_gx, &_gy, &_score, norm_factor, threshold, sensitivity, border_mode == BorderMode::UNDEFINED);
136  _harris_score = std::move(k);
137  }
138  break;
139  case 5:
140  {
141  auto k = arm_compute::support::cpp14::make_unique<NEHarrisScoreKernel<5>>();
142  k->configure(&_gx, &_gy, &_score, norm_factor, threshold, sensitivity, border_mode == BorderMode::UNDEFINED);
143  _harris_score = std::move(k);
144  }
145  break;
146  case 7:
147  {
148  auto k = arm_compute::support::cpp14::make_unique<NEHarrisScoreKernel<7>>();
149  k->configure(&_gx, &_gy, &_score, norm_factor, threshold, sensitivity, border_mode == BorderMode::UNDEFINED);
150  _harris_score = std::move(k);
151  }
152  default:
153  break;
154  }
155 
156  // Configure border filling before harris score
157  _border_gx.configure(&_gx, _harris_score->border_size(), border_mode, constant_border_value);
158  _border_gy.configure(&_gy, _harris_score->border_size(), border_mode, constant_border_value);
159 
160  // Allocate once all the configure methods have been called
161  _gx.allocator()->allocate();
162  _gy.allocator()->allocate();
163 
164  // Manage intermediate buffers
165  _memory_group.manage(&_nonmax);
166 
167  // Init non-maxima suppression function
168  _non_max_suppr.configure(&_score, &_nonmax, border_mode);
169 
170  // Allocate once all the configure methods have been called
171  _score.allocator()->allocate();
172 
173  // Init corner candidates kernel
174  _candidates.configure(&_nonmax, _corners_list.data(), &_num_corner_candidates);
175 
176  // Allocate once all the configure methods have been called
177  _nonmax.allocator()->allocate();
178 
179  // Init euclidean distance
180  _sort_euclidean.configure(_corners_list.data(), corners, &_num_corner_candidates, min_dist);
181 }
182 
184 {
185  ARM_COMPUTE_ERROR_ON_MSG(_sobel == nullptr, "Unconfigured function");
186 
187  MemoryGroupResourceScope scope_mg(_memory_group);
188 
189  // Init to 0 number of corner candidates
190  _num_corner_candidates = 0;
191 
192  // Run Sobel kernel
193  _sobel->run();
194 
195  // Fill border before harris score kernel
196  NEScheduler::get().schedule(&_border_gx, Window::DimZ);
197  NEScheduler::get().schedule(&_border_gy, Window::DimZ);
198 
199  // Run harris score kernel
200  NEScheduler::get().schedule(_harris_score.get(), Window::DimY);
201 
202  // Run non-maxima suppression
203  _non_max_suppr.run();
204 
205  // Run corner candidate kernel
206  NEScheduler::get().schedule(&_candidates, Window::DimY);
207 
208  // Run sort & euclidean distance
209  NEScheduler::get().schedule(&_sort_euclidean, Window::DimY);
210 }
BorderMode
Methods available to handle borders.
Definition: Types.h:264
Shape of a tensor.
Definition: TensorShape.h:39
void init(const TensorAllocator &allocator, const Coordinates &coords, TensorInfo &sub_info)
Shares the same backing memory with another tensor allocator, while the tensor info might be differen...
void run() override final
Run the kernels contained in the function.
NEHarrisCorners(std::shared_ptr< IMemoryManager > memory_manager=nullptr)
Constructor.
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
1 channel, 1 U8 per channel
1 channel, 1 F32 per channel
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
void configure(ITensor *tensor, BorderSize border_size, BorderMode border_mode, const PixelValue &constant_border_value=PixelValue())
Initialise the function.
Interface for NEON tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2020 Arm Limited.
TensorAllocator * allocator()
Return a pointer to the tensor's allocator.
Definition: Tensor.cpp:48
Basic implementation of the IArray interface which allocates a static number of T values.
Definition: Array.h:37
1 channel, 1 S32 per channel
void manage(IMemoryManageable *obj) override
Sets a object to be managed by the given memory group.
Definition: MemoryGroup.h:79
void run() override
Run the kernels contained in the function.
#define ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(t)
Definition: Validate.h:856
void configure(InternalKeypoint *in_out, IKeyPointArray *output, const int32_t *num_corner_candidates, float min_distance)
Initialise the kernel's source, destination and border mode.
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
void allocate() override
Allocate size specified by TensorInfo of CPU memory.
1 channel, 1 S16 per channel
void configure(ITensor *input, ITensor *output, BorderMode border_mode)
Initialise the function's source, destinations and border mode.
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:790
void init(Format format)
Initialize the tensor info with just a format.
Definition: TensorInfo.cpp:107
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
Memory group resources scope handling class.
Definition: IMemoryGroup.h:82
virtual void schedule(ICPPKernel *kernel, const Hints &hints)=0
Runs the kernel in the same thread as the caller synchronously.
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
Borders are left undefined.
void configure(IImage *input, float threshold, float min_dist, float sensitivity, int32_t gradient_size, int32_t block_size, KeyPointArray *corners, BorderMode border_mode, uint8_t constant_border_value=0)
Initialize the function's source, destination, conv and border_mode.
Store the tensor's metadata.
Definition: TensorInfo.h:45
SimpleTensor< T > threshold(const SimpleTensor< T > &src, T threshold, T false_value, T true_value, ThresholdType type, T upper)
Definition: Threshold.cpp:35
void configure(const IImage *input, InternalKeypoint *output, int32_t *num_corner_candidates)
Setup the kernel parameters.
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:95