forked from Smorodov/Multitarget-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefines.h
More file actions
325 lines (291 loc) · 6.5 KB
/
defines.h
File metadata and controls
325 lines (291 loc) · 6.5 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#pragma once
#include <vector>
#include <string>
#include <map>
#include <opencv2/opencv.hpp>
#include "object_types.h"
// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
typedef float track_t;
typedef cv::Point_<track_t> Point_t;
#define El_t CV_32F
#define Mat_t CV_32FC
typedef std::vector<int> assignments_t;
typedef std::vector<track_t> distMatrix_t;
///
template<typename T>
class TrackID
{
public:
typedef T value_type;
TrackID() = default;
TrackID(value_type val)
: m_val(val)
{
}
bool operator==(const TrackID& id) const
{
return m_val == id.m_val;
}
std::string ID2Str() const
{
return std::to_string(m_val);
}
static TrackID Str2ID(const std::string& id)
{
return TrackID(std::stoi(id));
}
TrackID NextID() const
{
return TrackID(m_val + 1);
}
size_t ID2Module(size_t module) const
{
return m_val % module;
}
value_type m_val{ 0 };
};
typedef TrackID<size_t> track_id_t;
namespace std
{
template <>
struct hash<track_id_t>
{
std::size_t operator()(const track_id_t& k) const
{
return std::hash<track_id_t::value_type>()(k.m_val);
}
};
}
///
/// \brief config_t
///
typedef std::multimap<std::string, std::string> config_t;
///
/// \brief The CRegion class
///
class CRegion
{
public:
CRegion() = default;
CRegion(const cv::Rect& rect) noexcept
: m_brect(rect)
{
B2RRect();
}
CRegion(const cv::RotatedRect& rrect) noexcept
: m_rrect(rrect)
{
if (m_rrect.size.width < 1)
m_rrect.size.width = 1;
if (m_rrect.size.height < 1)
m_rrect.size.height = 1;
R2BRect();
}
CRegion(const cv::Rect& rect, objtype_t type, float confidence) noexcept
: m_type(type), m_brect(rect), m_confidence(confidence)
{
B2RRect();
}
objtype_t m_type = bad_type;
cv::RotatedRect m_rrect;
cv::Rect m_brect;
float m_confidence = -1;
private:
///
/// \brief R2BRect
/// \return
///
cv::Rect R2BRect() noexcept
{
m_brect = m_rrect.boundingRect();
return m_brect;
}
///
/// \brief B2RRect
/// \return
///
cv::RotatedRect B2RRect() noexcept
{
m_rrect = cv::RotatedRect(m_brect.tl(), cv::Point2f(static_cast<float>(m_brect.x + m_brect.width), static_cast<float>(m_brect.y)), m_brect.br());
if (m_rrect.size.width < 1)
m_rrect.size.width = 1;
if (m_rrect.size.height < 1)
m_rrect.size.height = 1;
return m_rrect;
}
};
typedef std::vector<CRegion> regions_t;
///
/// \brief sqr
/// \param val
/// \return
///
template<class T> inline
T sqr(T val)
{
return val * val;
}
///
/// \brief get_lin_regress_params
/// \param in_data
/// \param start_pos
/// \param in_data_size
/// \param kx
/// \param bx
/// \param ky
/// \param by
///
template<typename T, typename CONT>
void get_lin_regress_params(
const CONT& in_data,
size_t start_pos,
size_t in_data_size,
T& kx, T& bx, T& ky, T& by)
{
T m1(0.), m2(0.);
T m3_x(0.), m4_x(0.);
T m3_y(0.), m4_y(0.);
const T el_count = static_cast<T>(in_data_size - start_pos);
for (size_t i = start_pos; i < in_data_size; ++i)
{
m1 += i;
m2 += sqr(i);
m3_x += in_data[i].x;
m4_x += i * in_data[i].x;
m3_y += in_data[i].y;
m4_y += i * in_data[i].y;
}
T det_1 = 1 / (el_count * m2 - sqr(m1));
m1 *= -1;
kx = det_1 * (m1 * m3_x + el_count * m4_x);
bx = det_1 * (m2 * m3_x + m1 * m4_x);
ky = det_1 * (m1 * m3_y + el_count * m4_y);
by = det_1 * (m2 * m3_y + m1 * m4_y);
}
///
/// \brief sqr: Euclid distance between two points
/// \param val
/// \return
///
template<class T, class POINT_TYPE> inline
T distance(const POINT_TYPE& p1, const POINT_TYPE& p2)
{
return sqrt((T)(sqr(p2.x - p1.x) + sqr(p2.y - p1.y)));
}
///
/// \brief Clamp: Fit rectangle to frame
/// \param rect
/// \param size
/// \return
///
inline cv::Rect Clamp(cv::Rect rect, const cv::Size& size)
{
if (rect.x < 0)
{
rect.width = std::min(rect.width, size.width - 1);
rect.x = 0;
}
else if (rect.x + rect.width >= size.width)
{
rect.x = std::max(0, size.width - rect.width - 1);
rect.width = std::min(rect.width, size.width - 1);
}
if (rect.y < 0)
{
rect.height = std::min(rect.height, size.height - 1);
rect.y = 0;
}
else if (rect.y + rect.height >= size.height)
{
rect.y = std::max(0, size.height - rect.height - 1);
rect.height = std::min(rect.height, size.height - 1);
}
return rect;
}
///
///
///
namespace tracking
{
///
/// \brief The Detectors enum
///
enum Detectors
{
Motion_VIBE = 0,
Motion_MOG = 1,
Motion_GMG = 2,
Motion_CNT = 3,
Motion_SuBSENSE = 4,
Motion_LOBSTER = 5,
Motion_MOG2 = 6,
Face_HAAR = 7,
Pedestrian_HOG = 8,
Pedestrian_C4 = 9,
Yolo_Darknet = 10,
Yolo_TensorRT = 11,
DNN_OCV = 12,
DetectorsCount
};
///
/// \brief The DistType enum
///
enum DistType
{
DistCenters, // Euclidean distance between centers, [0, 1]
DistRects, // Euclidean distance between bounding rectangles, [0, 1]
DistJaccard, // Intersection over Union, IoU, [0, 1]
DistHist, // Bhatacharia distance between histograms, [0, 1]
DistFeatureCos, // Cosine distance between embeddings, [0, 1]
DistMahalanobis, // Mahalanobis: https://ww2.mathworks.cn/help/vision/ug/motion-based-multiple-object-tracking.html
DistsCount
};
///
/// \brief The FilterGoal enum
///
enum FilterGoal
{
FilterCenter,
FilterRect,
FiltersCount
};
///
/// \brief The KalmanType enum
///
enum KalmanType
{
KalmanLinear,
KalmanUnscented,
KalmanAugmentedUnscented,
KalmanCount
};
///
/// \brief The MatchType enum
///
enum MatchType
{
MatchHungrian,
MatchBipart,
MatchCount
};
///
/// \brief The LostTrackType enum
///
enum LostTrackType
{
TrackNone,
TrackKCF,
TrackMIL,
TrackMedianFlow,
TrackGOTURN,
TrackMOSSE,
TrackCSRT,
TrackDAT,
TrackSTAPLE,
TrackLDES,
TrackDaSiamRPN,
SingleTracksCount
};
}