blob: 62bfdd60ba362614b0fd0992ad22d04fb58450df [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 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/core/CPP/kernels/CPPDetectionWindowNonMaximaSuppressionKernel.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28
29#include <algorithm>
30#include <cmath>
31
32using namespace arm_compute;
33
34namespace
35{
36bool compare_detection_window(const DetectionWindow &lhs, const DetectionWindow &rhs)
37{
38 return lhs.score > rhs.score;
39}
40} // namespace
41
42CPPDetectionWindowNonMaximaSuppressionKernel::CPPDetectionWindowNonMaximaSuppressionKernel()
43 : _input_output(nullptr), _min_distance(0.0f)
44{
45}
46
47bool CPPDetectionWindowNonMaximaSuppressionKernel::is_parallelisable() const
48{
49 return false;
50}
51
52void CPPDetectionWindowNonMaximaSuppressionKernel::configure(IDetectionWindowArray *input_output, float min_distance)
53{
54 ARM_COMPUTE_ERROR_ON(nullptr == input_output);
55
56 _input_output = input_output;
57 _min_distance = min_distance;
58
59 IKernel::configure(Window()); // Default 1 iteration window
60}
61
62void CPPDetectionWindowNonMaximaSuppressionKernel::run(const Window &window)
63{
64 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
65 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(IKernel::window(), window);
66 ARM_COMPUTE_ERROR_ON(_input_output->buffer() == nullptr);
67
68 const size_t num_candidates = _input_output->num_values();
69 size_t num_detections = 0;
70
71 // Sort list of candidates
72 std::sort(_input_output->buffer(), _input_output->buffer() + num_candidates, compare_detection_window);
73
74 const float min_distance_pow2 = _min_distance * _min_distance;
75
76 // Euclidean distance
77 for(size_t i = 0; i < num_candidates; ++i)
78 {
79 if(0.0f != _input_output->at(i).score)
80 {
81 DetectionWindow cur;
82 cur.x = _input_output->at(i).x;
83 cur.y = _input_output->at(i).y;
84 cur.width = _input_output->at(i).width;
85 cur.height = _input_output->at(i).height;
86 cur.idx_class = _input_output->at(i).idx_class;
87 cur.score = _input_output->at(i).score;
88
89 // Store window
90 _input_output->at(num_detections) = cur;
91
92 ++num_detections;
93
94 const float xc = cur.x + cur.width * 0.5f;
95 const float yc = cur.y + cur.height * 0.5f;
96
97 for(size_t k = i + 1; k < num_candidates; ++k)
98 {
99 const float xn = _input_output->at(k).x + _input_output->at(k).width * 0.5f;
100 const float yn = _input_output->at(k).y + _input_output->at(k).height * 0.5f;
101
102 const float dx = std::fabs(xn - xc);
103 const float dy = std::fabs(yn - yc);
104
105 if(dx < _min_distance && dy < _min_distance)
106 {
107 const float d = dx * dx + dy * dy;
108
109 if(d < min_distance_pow2)
110 {
111 // Invalidate keypoint
112 _input_output->at(k).score = 0.0f;
113 }
114 }
115 }
116 }
117 }
118
119 _input_output->resize(num_detections);
120}