forked from Smorodov/Multitarget-tracker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnms.h
234 lines (205 loc) · 6.05 KB
/
nms.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#pragma once
#include <opencv2/opencv.hpp>
#include <assert.h>
/**
* @brief nms
* Non maximum suppression
* @param srcRects
* @param resRects
* @param thresh
* @param neighbors
*/
inline void nms(
const std::vector<cv::Rect>& srcRects,
std::vector<cv::Rect>& resRects,
float thresh,
int neighbors = 0
)
{
resRects.clear();
const size_t size = srcRects.size();
if (!size)
{
return;
}
// Sort the bounding boxes by the bottom - right y - coordinate of the bounding box
std::multimap<int, size_t> idxs;
for (size_t i = 0; i < size; ++i)
{
idxs.insert(std::pair<int, size_t>(srcRects[i].br().y, i));
}
// keep looping while some indexes still remain in the indexes list
while (idxs.size() > 0)
{
// grab the last rectangle
auto lastElem = --std::end(idxs);
const cv::Rect& rect1 = srcRects[lastElem->second];
int neigborsCount = 0;
idxs.erase(lastElem);
for (auto pos = std::begin(idxs); pos != std::end(idxs); )
{
// grab the current rectangle
const cv::Rect& rect2 = srcRects[pos->second];
float intArea = static_cast<float>((rect1 & rect2).area());
float unionArea = static_cast<float>(rect1.area() + rect2.area() - intArea);
float overlap = intArea / unionArea;
// if there is sufficient overlap, suppress the current bounding box
if (overlap > thresh)
{
pos = idxs.erase(pos);
++neigborsCount;
}
else
{
++pos;
}
}
if (neigborsCount >= neighbors)
{
resRects.push_back(rect1);
}
}
}
/**
* @brief nms2
* Non maximum suppression with detection scores
* @param srcRects
* @param scores
* @param resRects
* @param thresh
* @param neighbors
*/
inline void nms2(
const std::vector<cv::Rect>& srcRects,
const std::vector<float>& scores,
std::vector<cv::Rect>& resRects,
float thresh,
int neighbors = 0,
float minScoresSum = 0.f
)
{
resRects.clear();
const size_t size = srcRects.size();
if (!size)
{
return;
}
assert(srcRects.size() == scores.size());
// Sort the bounding boxes by the detection score
std::multimap<float, size_t> idxs;
for (size_t i = 0; i < size; ++i)
{
idxs.insert(std::pair<float, size_t>(scores[i], i));
}
// keep looping while some indexes still remain in the indexes list
while (idxs.size() > 0)
{
// grab the last rectangle
auto lastElem = --std::end(idxs);
const cv::Rect& rect1 = srcRects[lastElem->second];
int neigborsCount = 0;
float scoresSum = lastElem->first;
idxs.erase(lastElem);
for (auto pos = std::begin(idxs); pos != std::end(idxs); )
{
// grab the current rectangle
const cv::Rect& rect2 = srcRects[pos->second];
float intArea = static_cast<float>((rect1 & rect2).area());
float unionArea = static_cast<float>(rect1.area() + rect2.area() - intArea);
float overlap = intArea / unionArea;
// if there is sufficient overlap, suppress the current bounding box
if (overlap > thresh)
{
scoresSum += pos->first;
pos = idxs.erase(pos);
++neigborsCount;
}
else
{
++pos;
}
}
if (neigborsCount >= neighbors &&
scoresSum >= minScoresSum)
{
resRects.push_back(rect1);
}
}
}
/**
* @brief nms3
* Non maximum suppression with detection scores
* @param srcRects
* @param resRects
* @param thresh
* @param neighbors
*/
template<typename OBJ, typename GET_RECT_FUNC, typename GET_SCORE_FUNC, typename GET_TYPE_FUNC>
inline void nms3(
const std::vector<OBJ>& srcRects,
std::vector<OBJ>& resRects,
float thresh,
GET_RECT_FUNC GetRect,
GET_SCORE_FUNC GetScore,
GET_TYPE_FUNC GetType,
int neighbors = 0,
float minScoresSum = 0.f
)
{
resRects.clear();
const size_t size = srcRects.size();
if (!size)
{
return;
}
// Sort the bounding boxes by the detection score
std::multimap<float, size_t> idxs;
for (size_t i = 0; i < size; ++i)
{
idxs.insert(std::pair<float, size_t>(GetScore(srcRects[i]), i));
}
// keep looping while some indexes still remain in the indexes list
while (idxs.size() > 0)
{
// grab the last rectangle
auto lastElem = --std::end(idxs);
size_t lastPos = lastElem->second;
const cv::Rect& rect1 = GetRect(srcRects[lastPos]);
auto type1 = GetType(srcRects[lastPos]);
int neigborsCount = 0;
float scoresSum = lastElem->first;
idxs.erase(lastElem);
for (auto pos = std::begin(idxs); pos != std::end(idxs); )
{
// grab the current rectangle
auto type2 = GetType(srcRects[pos->second]);
if (type1 == type2)
{
const cv::Rect& rect2 = GetRect(srcRects[pos->second]);
float intArea = static_cast<float>((rect1 & rect2).area());
float unionArea = static_cast<float>(rect1.area() + rect2.area() - intArea);
float overlap = intArea / unionArea;
// if there is sufficient overlap, suppress the current bounding box
if (overlap > thresh)
{
scoresSum += pos->first;
pos = idxs.erase(pos);
++neigborsCount;
}
else
{
++pos;
}
}
else
{
++pos;
}
}
if (neigborsCount >= neighbors &&
scoresSum >= minScoresSum)
{
resRects.push_back(srcRects[lastPos]);
}
}
}