forked from Smorodov/Multitarget-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnms.h
147 lines (128 loc) · 3.63 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
#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 = (rect1 & rect2).area();
float unionArea = 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 = (rect1 & rect2).area();
float unionArea = 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);
}
}
}