forked from Smorodov/Multitarget-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrajectory.h
More file actions
390 lines (349 loc) · 10.3 KB
/
trajectory.h
File metadata and controls
390 lines (349 loc) · 10.3 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#pragma once
#include <vector>
#include "defines.h"
///
/// \brief The TrajectoryPoint struct
///
struct TrajectoryPoint
{
///
/// \brief TrajectoryPoint
///
TrajectoryPoint() = default;
///
/// \brief TrajectoryPoint
/// \param prediction
///
TrajectoryPoint(const Point_t& prediction)
: m_prediction(prediction)
{
}
///
/// \brief TrajectoryPoint
/// \param prediction
/// \param raw
///
TrajectoryPoint(const Point_t& prediction, const Point_t& raw)
:
m_prediction(prediction),
m_raw(raw),
m_hasRaw(true)
{
}
///
TrajectoryPoint(const TrajectoryPoint& tp) noexcept
: m_prediction(tp.m_prediction), m_raw(tp.m_raw), m_hasRaw(tp.m_hasRaw)
{
}
///
TrajectoryPoint& operator=(const TrajectoryPoint& tp) noexcept
{
m_prediction = tp.m_prediction;
m_raw = tp.m_raw;
m_hasRaw = tp.m_hasRaw;
return *this;
}
///
TrajectoryPoint(TrajectoryPoint&&) = default;
Point_t m_prediction;
Point_t m_raw;
bool m_hasRaw = false;
};
///
/// \brief The Trace class
///
class Trace
{
public:
///
Trace() = default;
///
Trace(const Trace&) = default;
///
Trace(Trace&&) = default;
///
Trace& operator=(const Trace& trace)
{
m_trace = trace.m_trace;
return *this;
}
///
/// \brief operator []
/// \param i
/// \return
///
const Point_t& operator[](size_t i) const
{
return m_trace[i].m_prediction;
}
///
/// \brief operator []
/// \param i
/// \return
///
Point_t& operator[](size_t i)
{
return m_trace[i].m_prediction;
}
///
/// \brief at
/// \param i
/// \return
///
const TrajectoryPoint& at(size_t i) const
{
return m_trace[i];
}
///
/// \brief at
/// \param i
/// \return
///
TrajectoryPoint& at(size_t i)
{
return m_trace[i];
}
///
/// \brief size
/// \return
///
size_t size() const
{
return m_trace.size();
}
///
/// \brief push_back
/// \param prediction
///
void push_back(const Point_t& prediction)
{
m_trace.emplace_back(prediction);
}
void push_back(const Point_t& prediction, const Point_t& raw)
{
m_trace.emplace_back(prediction, raw);
}
///
/// \brief pop_front
/// \param count
///
void pop_front(size_t count)
{
if (count < size())
m_trace.erase(m_trace.begin(), m_trace.begin() + count);
else
m_trace.clear();
}
///
/// \brief GetRawCount
/// \param lastPeriod
/// \return
///
size_t GetRawCount(size_t lastPeriod) const
{
size_t res = 0;
size_t i = 0;
if (lastPeriod < m_trace.size())
i = m_trace.size() - lastPeriod;
for (; i < m_trace.size(); ++i)
{
if (m_trace[i].m_hasRaw)
++res;
}
return res;
}
///
/// \brief Reserve
/// \param capacity
/// \return
///
void Reserve(size_t capacity)
{
m_trace.reserve(capacity);
}
private:
std::vector<TrajectoryPoint> m_trace;
};
///
/// \brief The TrackingObject class
///
struct TrackingObject
{
Trace m_trace; // Trajectory
track_id_t m_ID; // Objects ID
cv::RotatedRect m_rrect; // Coordinates
cv::Vec<track_t, 2> m_velocity; // pixels/sec
objtype_t m_type = bad_type; // Objects type name or empty value
float m_confidence = -1; // From Detector with score (YOLO or SSD)
bool m_isStatic = false; // Object is abandoned
int m_staticTime = 0; // Object is abandoned, frames
bool m_outOfTheFrame = false; // Is object out of the frame
mutable bool m_lastRobust = false; // saved latest robust value
///
TrackingObject(const cv::RotatedRect& rrect, track_id_t ID, const Trace& trace,
bool isStatic, int staticTime, bool outOfTheFrame, objtype_t type, float confidence, cv::Vec<track_t, 2> velocity)
:
m_trace(trace), m_ID(ID), m_rrect(rrect), m_velocity(velocity), m_type(type), m_confidence(confidence),
m_isStatic(isStatic), m_staticTime(staticTime),
m_outOfTheFrame(outOfTheFrame)
{
//std::cout << "TrackingObject.m_rrect: " << m_rrect.center << ", " << m_rrect.angle << ", " << m_rrect.size << std::endl;
}
///
TrackingObject() = default;
///
TrackingObject(const TrackingObject&) = default;
///
TrackingObject(TrackingObject&&) = default;
///
TrackingObject & operator=(const TrackingObject& track)
{
m_trace = track.m_trace;
m_ID = track.m_ID;
m_rrect = track.m_rrect;
m_velocity = track.m_velocity;
m_type = track.m_type;
m_confidence = track.m_confidence;
m_isStatic = track.m_isStatic;
m_staticTime = track.m_staticTime;
m_outOfTheFrame = track.m_outOfTheFrame;
m_lastRobust = track.m_lastRobust;
return *this;
}
///
~TrackingObject() = default;
///
/// \brief IsRobust
/// \param minTraceSize
/// \param minRawRatio
/// \param sizeRatio
/// \return
///
bool IsRobust(int minTraceSize, float minRawRatio, cv::Size2f sizeRatio, size_t lastDetectsCount = 0) const
{
m_lastRobust = m_trace.size() > static_cast<size_t>(minTraceSize);
if (lastDetectsCount)
{
size_t raws = m_trace.GetRawCount(lastDetectsCount);
m_lastRobust = (raws > 0);
}
m_lastRobust &= m_trace.GetRawCount(m_trace.size() - 1) / static_cast<float>(m_trace.size()) > minRawRatio;
if (sizeRatio.width + sizeRatio.height > 0)
{
float sr = m_rrect.size.width / m_rrect.size.height;
if (sizeRatio.width > 0)
m_lastRobust &= (sr > sizeRatio.width);
if (sizeRatio.height > 0)
m_lastRobust &= (sr < sizeRatio.height);
}
if (m_outOfTheFrame)
m_lastRobust = false;
return m_lastRobust;
}
///
/// \brief GetTrajectory
/// \return
///
std::vector<cv::Point> GetTrajectory() const
{
std::vector<cv::Point> trajectory(m_trace.size());
for (size_t i = 0; i < m_trace.size(); ++i)
{
trajectory[i] = m_trace.at(i).m_prediction;
}
return trajectory;
}
///
/// \brief LeastSquarespoly2
/// \return
///
void LeastSquarespoly2(size_t posFrom, size_t count, track_t& ax, track_t& v0x, track_t& x0, track_t& ay, track_t& v0y, track_t& y0) const
{
double b1_x(0), b2_x(0), b3_x(0);
double b1_y(0), b2_y(0), b3_y(0);
double t_0(0.), t_1(0.), t_2(0.), t_3(0.), t_4(0.);
double j = static_cast<double>(posFrom);
for (size_t i = posFrom; i < count; ++i, j += 1.)
{
double sqr_j = sqr(j);
t_0 += 1.;
t_1 += j;
t_2 += sqr_j;
t_3 += j * sqr_j;
t_4 += sqr(sqr_j);
const auto& pt = m_trace.at(i).m_prediction;
b1_x += pt.x;
b2_x += j * pt.x;
b3_x += sqr_j * pt.x;
b1_y += pt.y;
b2_y += j * pt.y;
b3_y += sqr_j * pt.y;
}
// Cramers rule for system of linear equations 3x3
double a11(t_0), a12(t_1), a13(t_2), a21(t_1), a22(t_2), a23(t_3), a31(t_2), a32(t_3), a33(t_4);
double det_1 = 1. / (a11 * a22 * a33 + a21 * a32 * a13 + a12 * a23 * a31 - a31 * a22 * a13 - a11 * a23 * a32 - a12 * a21 * a33);
x0 = static_cast<track_t>(det_1 * (b1_x * a22 * a33 + b2_x * a32 * a13 + a12 * a23 * b3_x - b3_x * a22 * a13 - b1_x * a23 * a32 - a12 * b2_x * a33));
v0x = static_cast<track_t>(det_1 * (a11 * b2_x * a33 + a21 * b3_x * a13 + b1_x * a23 * a31 - a31 * b2_x * a13 - a11 * a23 * b3_x - b1_x * a21 * a33));
ax = static_cast<track_t>(det_1 * (a11 * a22 * b3_x + a21 * a32 * b1_x + a12 * b2_x * a31 - a31 * a22 * b1_x - a11 * b2_x * a32 - a12 * a21 * b3_x));
y0 = static_cast<track_t>(det_1 * (b1_y * a22 * a33 + b2_y * a32 * a13 + a12 * a23 * b3_y - b3_y * a22 * a13 - b1_y * a23 * a32 - a12 * b2_y * a33));
v0y = static_cast<track_t>(det_1 * (a11 * b2_y * a33 + a21 * b3_y * a13 + b1_y * a23 * a31 - a31 * b2_y * a13 - a11 * a23 * b3_y - b1_y * a21 * a33));
ay = static_cast<track_t>(det_1 * (a11 * a22 * b3_y + a21 * a32 * b1_y + a12 * b2_y * a31 - a31 * a22 * b1_y - a11 * b2_y * a32 - a12 * a21 * b3_y));
}
///
struct LSParams
{
track_t m_ax = 0;
track_t m_v0x = 0;
track_t m_x0 = 0;
track_t m_ay = 0;
track_t m_v0y = 0;
track_t m_y0 = 0;
friend std::ostream& operator<<(std::ostream& os, const LSParams& lsParaml)
{
os << "(" << lsParaml.m_ax << ", " << lsParaml.m_v0x << ", " << lsParaml.m_x0 << "), (" << lsParaml.m_ay << ", " << lsParaml.m_v0y << ", " << lsParaml.m_y0 << ")";
return os;
}
};
///
/// \brief LeastSquares2
/// \return
///
bool LeastSquares2(size_t framesCount, track_t& mean, track_t& stddev, LSParams& lsParams) const
{
bool res = m_trace.size() > 3;
if (res)
{
size_t startPos = 0;
#if 0
if (framesCount < m_trace.size())
startPos = m_trace.size() - framesCount;
else
framesCount = m_trace.size();
#else
framesCount = m_trace.size();
#endif
LeastSquarespoly2(startPos, framesCount, lsParams.m_ax, lsParams.m_v0x, lsParams.m_x0, lsParams.m_ay, lsParams.m_v0y, lsParams.m_y0);
track_t sum = 0;
track_t sum2 = 0;
for (size_t i = startPos; i < framesCount; ++i)
{
track_t t = static_cast<track_t>(i);
track_t dist = distance<track_t>(m_trace[i], Point_t(lsParams.m_ax * sqr(t) + lsParams.m_v0x * t + lsParams.m_x0,
lsParams.m_ay * sqr(t) + lsParams.m_v0y * t + lsParams.m_y0));
sum += dist;
sum2 += sqr(dist);
}
mean = sum / static_cast<track_t>(framesCount);
stddev = sqrt(sum2 / static_cast<track_t>(framesCount) - sqr(mean));
}
return res;
}
///
/// \brief GetTrajectory
/// \return
///
cv::Rect GetBoundingRect() const
{
return m_rrect.boundingRect();
}
};