forked from Smorodov/Multitarget-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKalman.cpp
736 lines (630 loc) · 25.1 KB
/
Kalman.cpp
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
#include "Kalman.h"
#include <iostream>
#include <vector>
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
TKalmanFilter::TKalmanFilter(
tracking::KalmanType type,
Point_t pt,
track_t deltaTime, // time increment (lower values makes target more "massive")
track_t accelNoiseMag
)
:
m_type(type),
m_initialized(false),
m_deltaTime(deltaTime),
m_accelNoiseMag(accelNoiseMag)
{
m_initialPoints.push_back(pt);
m_lastPointResult = pt;
}
//---------------------------------------------------------------------------
TKalmanFilter::TKalmanFilter(
tracking::KalmanType type,
cv::Rect rect,
track_t deltaTime, // time increment (lower values makes target more "massive")
track_t accelNoiseMag
)
:
m_type(type),
m_initialized(false),
m_deltaTime(deltaTime),
m_accelNoiseMag(accelNoiseMag)
{
m_initialRects.push_back(rect);
m_lastRectResult = rect;
}
//---------------------------------------------------------------------------
TKalmanFilter::~TKalmanFilter()
{
}
//---------------------------------------------------------------------------
void TKalmanFilter::CreateLinear(Point_t xy0, Point_t xyv0)
{
// We don't know acceleration, so, assume it to process noise.
// But we can guess, the range of acceleration values thich can be achieved by tracked object.
// Process noise. (standard deviation of acceleration: m/s^2)
// shows, woh much target can accelerate.
//4 state variables, 2 measurements
m_linearKalman = std::make_unique<cv::KalmanFilter>(4, 2, 0);
// Transition cv::Matrix
m_linearKalman->transitionMatrix = (cv::Mat_<track_t>(4, 4) <<
1, 0, m_deltaTime, 0,
0, 1, 0, m_deltaTime,
0, 0, 1, 0,
0, 0, 0, 1);
// init...
m_lastPointResult = xy0;
m_linearKalman->statePre.at<track_t>(0) = xy0.x; // x
m_linearKalman->statePre.at<track_t>(1) = xy0.y; // y
m_linearKalman->statePre.at<track_t>(2) = xyv0.x;
m_linearKalman->statePre.at<track_t>(3) = xyv0.y;
m_linearKalman->statePost.at<track_t>(0) = xy0.x;
m_linearKalman->statePost.at<track_t>(1) = xy0.y;
cv::setIdentity(m_linearKalman->measurementMatrix);
m_linearKalman->processNoiseCov = (cv::Mat_<track_t>(4, 4) <<
pow(m_deltaTime,4.0)/4.0 ,0 ,pow(m_deltaTime,3.0)/2.0 ,0,
0 ,pow(m_deltaTime,4.0)/4.0 ,0 ,pow(m_deltaTime,3.0)/2.0,
pow(m_deltaTime,3.0)/2.0 ,0 ,pow(m_deltaTime,2.0) ,0,
0 ,pow(m_deltaTime,3.0)/2.0 ,0 ,pow(m_deltaTime,2.0));
m_linearKalman->processNoiseCov *= m_accelNoiseMag;
setIdentity(m_linearKalman->measurementNoiseCov, cv::Scalar::all(0.1));
setIdentity(m_linearKalman->errorCovPost, cv::Scalar::all(.1));
m_initialized = true;
}
//---------------------------------------------------------------------------
void TKalmanFilter::CreateLinear(cv::Rect_<track_t> rect0, Point_t rectv0)
{
// We don't know acceleration, so, assume it to process noise.
// But we can guess, the range of acceleration values thich can be achieved by tracked object.
// Process noise. (standard deviation of acceleration: m/s^2)
// shows, woh much target can accelerate.
//4 state variables (x, y, dx, dy, width, height), 4 measurements (x, y, width, height)
m_linearKalman = std::make_unique<cv::KalmanFilter>(6, 4, 0);
// Transition cv::Matrix
m_linearKalman->transitionMatrix = (cv::Mat_<track_t>(6, 6) <<
1, 0, 0, 0, m_deltaTime, 0,
0, 1, 0, 0, 0, m_deltaTime,
0, 0, 1, 0, 0, 0,
0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 1);
// init...
m_linearKalman->statePre.at<track_t>(0) = rect0.x; // x
m_linearKalman->statePre.at<track_t>(1) = rect0.y; // y
m_linearKalman->statePre.at<track_t>(2) = rect0.width; // width
m_linearKalman->statePre.at<track_t>(3) = rect0.height; // height
m_linearKalman->statePre.at<track_t>(4) = rectv0.x; // dx
m_linearKalman->statePre.at<track_t>(5) = rectv0.y; // dy
m_linearKalman->statePost.at<track_t>(0) = rect0.x;
m_linearKalman->statePost.at<track_t>(1) = rect0.y;
m_linearKalman->statePost.at<track_t>(2) = rect0.width;
m_linearKalman->statePost.at<track_t>(3) = rect0.height;
cv::setIdentity(m_linearKalman->measurementMatrix);
track_t n1 = pow(m_deltaTime, 4.) / 4.;
track_t n2 = pow(m_deltaTime, 3.) / 2.;
track_t n3 = pow(m_deltaTime, 2.);
m_linearKalman->processNoiseCov = (cv::Mat_<track_t>(6, 6) <<
n1, 0, 0, 0, n2, 0,
0, n1, 0, 0, 0, n2,
0, 0, n1, 0, 0, 0,
0, 0, 0, n1, 0, 0,
n2, 0, 0, 0, n3, 0,
0, n2, 0, 0, 0, n3);
m_linearKalman->processNoiseCov *= m_accelNoiseMag;
setIdentity(m_linearKalman->measurementNoiseCov, cv::Scalar::all(0.1));
setIdentity(m_linearKalman->errorCovPost, cv::Scalar::all(.1));
m_initialized = true;
}
//---------------------------------------------------------------------------
template<class T> inline
T sqr(T val)
{
return val * val;
}
//---------------------------------------------------------------------------
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);
}
#ifdef USE_OCV_UKF
//---------------------------------------------------------------------------
class AcceleratedModel: public cv::tracking::UkfSystemModel
{
public:
AcceleratedModel(track_t deltaTime, bool rectModel)
:
cv::tracking::UkfSystemModel(),
m_deltaTime(deltaTime),
m_rectModel(rectModel)
{
}
void stateConversionFunction(const cv::Mat& x_k, const cv::Mat& u_k, const cv::Mat& v_k, cv::Mat& x_kplus1)
{
track_t x0 = x_k.at<track_t>(0, 0);
track_t y0 = x_k.at<track_t>(1, 0);
track_t vx0 = x_k.at<track_t>(2, 0);
track_t vy0 = x_k.at<track_t>(3, 0);
track_t ax0 = x_k.at<track_t>(4, 0);
track_t ay0 = x_k.at<track_t>(5, 0);
x_kplus1.at<track_t>(0, 0) = x0 + vx0 * m_deltaTime + ax0 * sqr(m_deltaTime) / 2;
x_kplus1.at<track_t>(1, 0) = y0 + vy0 * m_deltaTime + ay0 * sqr(m_deltaTime) / 2;
x_kplus1.at<track_t>(2, 0) = vx0 + ax0 * m_deltaTime;
x_kplus1.at<track_t>(3, 0) = vy0 + ay0 * m_deltaTime;
x_kplus1.at<track_t>(4, 0) = ax0;
x_kplus1.at<track_t>(5, 0) = ay0;
if (m_rectModel)
{
x_kplus1.at<track_t>(6, 0) = x_k.at<track_t>(6, 0);
x_kplus1.at<track_t>(7, 0) = x_k.at<track_t>(7, 0);
}
if (v_k.size() == u_k.size())
{
x_kplus1 += v_k + u_k;
}
else
{
x_kplus1 += v_k;
}
}
void measurementFunction(const cv::Mat& x_k, const cv::Mat& n_k, cv::Mat& z_k)
{
track_t x0 = x_k.at<track_t>(0, 0);
track_t y0 = x_k.at<track_t>(1, 0);
track_t vx0 = x_k.at<track_t>(2, 0);
track_t vy0 = x_k.at<track_t>(3, 0);
track_t ax0 = x_k.at<track_t>(4, 0);
track_t ay0 = x_k.at<track_t>(5, 0);
z_k.at<track_t>(0, 0) = x0 + vx0 * m_deltaTime + ax0 * sqr(m_deltaTime) / 2 + n_k.at<track_t>(0, 0);
z_k.at<track_t>(1, 0) = y0 + vy0 * m_deltaTime + ay0 * sqr(m_deltaTime) / 2 + n_k.at<track_t>(1, 0);
if (m_rectModel)
{
z_k.at<track_t>(2, 0) = x_k.at<track_t>(6, 0);
z_k.at<track_t>(3, 0) = x_k.at<track_t>(7, 0);
}
}
private:
track_t m_deltaTime;
bool m_rectModel;
};
//---------------------------------------------------------------------------
void TKalmanFilter::CreateUnscented(Point_t xy0, Point_t xyv0)
{
int MP = 2;
int DP = 6;
int CP = 0;
cv::Mat processNoiseCov = cv::Mat::zeros(DP, DP, Mat_t(1));
processNoiseCov.at<track_t>(0, 0) = 1e-14;
processNoiseCov.at<track_t>(1, 1) = 1e-14;
processNoiseCov.at<track_t>(2, 2) = 1e-6;
processNoiseCov.at<track_t>(3, 3) = 1e-6;
processNoiseCov.at<track_t>(4, 4) = 1e-6;
processNoiseCov.at<track_t>(5, 5) = 1e-6;
cv::Mat measurementNoiseCov = cv::Mat::zeros(MP, MP, Mat_t(1));
measurementNoiseCov.at<track_t>(0, 0) = 1e-6;
measurementNoiseCov.at<track_t>(1, 1) = 1e-6;
cv::Mat initState(DP, 1, Mat_t(1));
initState.at<track_t>(0, 0) = xy0.x;
initState.at<track_t>(1, 0) = xy0.y;
initState.at<track_t>(2, 0) = xyv0.x;
initState.at<track_t>(3, 0) = xyv0.y;
initState.at<track_t>(4, 0) = 0;
initState.at<track_t>(5, 0) = 0;
cv::Mat P = 1e-6 * cv::Mat::eye(DP, DP, Mat_t(1));
cv::Ptr<AcceleratedModel> model(new AcceleratedModel(m_deltaTime, false));
cv::tracking::UnscentedKalmanFilterParams params(DP, MP, CP, 0, 0, model);
params.dataType = Mat_t(1);
params.stateInit = initState.clone();
params.errorCovInit = P.clone();
params.measurementNoiseCov = measurementNoiseCov.clone();
params.processNoiseCov = processNoiseCov.clone();
params.alpha = 1.0;
params.beta = 2.0;
params.k = -2.0;
m_uncsentedKalman = cv::tracking::createUnscentedKalmanFilter(params);
m_initialized = true;
}
//---------------------------------------------------------------------------
void TKalmanFilter::CreateUnscented(cv::Rect_<track_t> rect0, Point_t rectv0)
{
int MP = 4;
int DP = 8;
int CP = 0;
cv::Mat processNoiseCov = cv::Mat::zeros(DP, DP, Mat_t(1));
processNoiseCov.at<track_t>(0, 0) = 1e-3;
processNoiseCov.at<track_t>(1, 1) = 1e-3;
processNoiseCov.at<track_t>(2, 2) = 1e-3;
processNoiseCov.at<track_t>(3, 3) = 1e-3;
processNoiseCov.at<track_t>(4, 4) = 1e-3;
processNoiseCov.at<track_t>(5, 5) = 1e-3;
processNoiseCov.at<track_t>(6, 6) = 1e-3;
processNoiseCov.at<track_t>(7, 7) = 1e-3;
cv::Mat measurementNoiseCov = cv::Mat::zeros(MP, MP, Mat_t(1));
measurementNoiseCov.at<track_t>(0, 0) = 1e-3;
measurementNoiseCov.at<track_t>(1, 1) = 1e-3;
measurementNoiseCov.at<track_t>(2, 2) = 1e-3;
measurementNoiseCov.at<track_t>(3, 3) = 1e-3;
cv::Mat initState(DP, 1, Mat_t(1));
initState.at<track_t>(0, 0) = rect0.x;
initState.at<track_t>(1, 0) = rect0.y;
initState.at<track_t>(2, 0) = rectv0.x;
initState.at<track_t>(3, 0) = rectv0.y;
initState.at<track_t>(4, 0) = 0;
initState.at<track_t>(5, 0) = 0;
initState.at<track_t>(6, 0) = rect0.width;
initState.at<track_t>(7, 0) = rect0.height;
cv::Mat P = 1e-3 * cv::Mat::eye(DP, DP, Mat_t(1));
cv::Ptr<AcceleratedModel> model(new AcceleratedModel(m_deltaTime, true));
cv::tracking::UnscentedKalmanFilterParams params(DP, MP, CP, 0, 0, model);
params.dataType = Mat_t(1);
params.stateInit = initState.clone();
params.errorCovInit = P.clone();
params.measurementNoiseCov = measurementNoiseCov.clone();
params.processNoiseCov = processNoiseCov.clone();
params.alpha = 1;
params.beta = 2.0;
params.k = -2.0;
m_uncsentedKalman = cv::tracking::createUnscentedKalmanFilter(params);
m_initialized = true;
}
//---------------------------------------------------------------------------
void TKalmanFilter::CreateAugmentedUnscented(Point_t xy0, Point_t xyv0)
{
int MP = 2;
int DP = 6;
int CP = 0;
cv::Mat processNoiseCov = cv::Mat::zeros(DP, DP, Mat_t(1));
processNoiseCov.at<track_t>(0, 0) = 1e-14;
processNoiseCov.at<track_t>(1, 1) = 1e-14;
processNoiseCov.at<track_t>(2, 2) = 1e-6;
processNoiseCov.at<track_t>(3, 3) = 1e-6;
processNoiseCov.at<track_t>(4, 4) = 1e-6;
processNoiseCov.at<track_t>(5, 5) = 1e-6;
cv::Mat measurementNoiseCov = cv::Mat::zeros(MP, MP, Mat_t(1));
measurementNoiseCov.at<track_t>(0, 0) = 1e-6;
measurementNoiseCov.at<track_t>(1, 1) = 1e-6;
cv::Mat initState(DP, 1, Mat_t(1));
initState.at<track_t>(0, 0) = xy0.x;
initState.at<track_t>(1, 0) = xy0.y;
initState.at<track_t>(2, 0) = xyv0.x;
initState.at<track_t>(3, 0) = xyv0.y;
initState.at<track_t>(4, 0) = 0;
initState.at<track_t>(5, 0) = 0;
cv::Mat P = 1e-6 * cv::Mat::eye(DP, DP, Mat_t(1));
cv::Ptr<AcceleratedModel> model(new AcceleratedModel(m_deltaTime, false));
cv::tracking::AugmentedUnscentedKalmanFilterParams params(DP, MP, CP, 0, 0, model);
params.dataType = Mat_t(1);
params.stateInit = initState.clone();
params.errorCovInit = P.clone();
params.measurementNoiseCov = measurementNoiseCov.clone();
params.processNoiseCov = processNoiseCov.clone();
params.alpha = 1;
params.beta = 2.0;
params.k = -2.0;
m_uncsentedKalman = cv::tracking::createAugmentedUnscentedKalmanFilter(params);
m_initialized = true;
}
//---------------------------------------------------------------------------
void TKalmanFilter::CreateAugmentedUnscented(cv::Rect_<track_t> rect0, Point_t rectv0)
{
int MP = 4;
int DP = 8;
int CP = 0;
cv::Mat processNoiseCov = cv::Mat::zeros(DP, DP, Mat_t(1));
processNoiseCov.at<track_t>(0, 0) = 1e-3;
processNoiseCov.at<track_t>(1, 1) = 1e-3;
processNoiseCov.at<track_t>(2, 2) = 1e-3;
processNoiseCov.at<track_t>(3, 3) = 1e-3;
processNoiseCov.at<track_t>(4, 4) = 1e-3;
processNoiseCov.at<track_t>(5, 5) = 1e-3;
processNoiseCov.at<track_t>(6, 6) = 1e-3;
processNoiseCov.at<track_t>(7, 7) = 1e-3;
cv::Mat measurementNoiseCov = cv::Mat::zeros(MP, MP, Mat_t(1));
measurementNoiseCov.at<track_t>(0, 0) = 1e-3;
measurementNoiseCov.at<track_t>(1, 1) = 1e-3;
measurementNoiseCov.at<track_t>(2, 2) = 1e-3;
measurementNoiseCov.at<track_t>(3, 3) = 1e-3;
cv::Mat initState(DP, 1, Mat_t(1));
initState.at<track_t>(0, 0) = rect0.x;
initState.at<track_t>(1, 0) = rect0.y;
initState.at<track_t>(2, 0) = rectv0.x;
initState.at<track_t>(3, 0) = rectv0.y;
initState.at<track_t>(4, 0) = 0;
initState.at<track_t>(5, 0) = 0;
initState.at<track_t>(6, 0) = rect0.width;
initState.at<track_t>(7, 0) = rect0.height;
cv::Mat P = 1e-3 * cv::Mat::eye(DP, DP, Mat_t(1));
cv::Ptr<AcceleratedModel> model(new AcceleratedModel(m_deltaTime, true));
cv::tracking::AugmentedUnscentedKalmanFilterParams params(DP, MP, CP, 0, 0, model);
params.dataType = Mat_t(1);
params.stateInit = initState.clone();
params.errorCovInit = P.clone();
params.measurementNoiseCov = measurementNoiseCov.clone();
params.processNoiseCov = processNoiseCov.clone();
params.alpha = 1;
params.beta = 2.0;
params.k = -2.0;
m_uncsentedKalman = cv::tracking::createAugmentedUnscentedKalmanFilter(params);
m_initialized = true;
}
#endif
//---------------------------------------------------------------------------
Point_t TKalmanFilter::GetPointPrediction()
{
if (m_initialized)
{
cv::Mat prediction;
switch (m_type)
{
case tracking::KalmanLinear:
prediction = m_linearKalman->predict();
break;
case tracking::KalmanUnscented:
case tracking::KalmanAugmentedUnscented:
#ifdef USE_OCV_UKF
prediction = m_uncsentedKalman->predict();
#else
prediction = m_linearKalman->predict();
std::cerr << "UnscentedKalmanFilter was disabled in CMAKE! Set KalmanLinear in constructor." << std::endl;
#endif
break;
}
m_lastPointResult = Point_t(prediction.at<track_t>(0), prediction.at<track_t>(1));
}
else
{
}
return m_lastPointResult;
}
//---------------------------------------------------------------------------
Point_t TKalmanFilter::Update(Point_t pt, bool dataCorrect)
{
if (!m_initialized)
{
if (m_initialPoints.size() < MIN_INIT_VALS)
{
if (dataCorrect)
{
m_initialPoints.push_back(pt);
}
}
if (m_initialPoints.size() == MIN_INIT_VALS)
{
track_t kx = 0;
track_t bx = 0;
track_t ky = 0;
track_t by = 0;
get_lin_regress_params(m_initialPoints, 0, MIN_INIT_VALS, kx, bx, ky, by);
Point_t xy0(kx * (MIN_INIT_VALS - 1) + bx, ky * (MIN_INIT_VALS - 1) + by);
Point_t xyv0(kx, ky);
switch (m_type)
{
case tracking::KalmanLinear:
CreateLinear(xy0, xyv0);
break;
case tracking::KalmanUnscented:
#ifdef USE_OCV_UKF
CreateUnscented(xy0, xyv0);
#else
CreateLinear(xy0, xyv0);
std::cerr << "UnscentedKalmanFilter was disabled in CMAKE! Set KalmanLinear in constructor." << std::endl;
#endif
break;
case tracking::KalmanAugmentedUnscented:
#ifdef USE_OCV_UKF
CreateAugmentedUnscented(xy0, xyv0);
#else
CreateLinear(xy0, xyv0);
std::cerr << "AugmentedUnscentedKalmanFilter was disabled in CMAKE! Set KalmanLinear in constructor." << std::endl;
#endif
break;
}
}
}
if (m_initialized)
{
cv::Mat measurement(2, 1, Mat_t(1));
if (!dataCorrect)
{
measurement.at<track_t>(0) = m_lastPointResult.x; //update using prediction
measurement.at<track_t>(1) = m_lastPointResult.y;
}
else
{
measurement.at<track_t>(0) = pt.x; //update using measurements
measurement.at<track_t>(1) = pt.y;
}
// Correction
cv::Mat estimated;
switch (m_type)
{
case tracking::KalmanLinear:
estimated = m_linearKalman->correct(measurement);
break;
case tracking::KalmanUnscented:
case tracking::KalmanAugmentedUnscented:
#ifdef USE_OCV_UKF
estimated = m_uncsentedKalman->correct(measurement);
#else
estimated = m_linearKalman->correct(measurement);
std::cerr << "UnscentedKalmanFilter was disabled in CMAKE! Set KalmanLinear in constructor." << std::endl;
#endif
break;
}
m_lastPointResult.x = estimated.at<track_t>(0); //update using measurements
m_lastPointResult.y = estimated.at<track_t>(1);
}
else
{
if (dataCorrect)
{
m_lastPointResult = pt;
}
}
return m_lastPointResult;
}
//---------------------------------------------------------------------------
cv::Rect TKalmanFilter::GetRectPrediction()
{
if (m_initialized)
{
cv::Mat prediction;
switch (m_type)
{
case tracking::KalmanLinear:
prediction = m_linearKalman->predict();
break;
case tracking::KalmanUnscented:
case tracking::KalmanAugmentedUnscented:
#ifdef USE_OCV_UKF
prediction = m_uncsentedKalman->predict();
#else
prediction = m_linearKalman->predict();
std::cerr << "UnscentedKalmanFilter was disabled in CMAKE! Set KalmanLinear in constructor." << std::endl;
#endif
break;
}
m_lastRectResult = cv::Rect_<track_t>(prediction.at<track_t>(0), prediction.at<track_t>(1), prediction.at<track_t>(2), prediction.at<track_t>(3));
}
else
{
}
return cv::Rect(static_cast<int>(m_lastRectResult.x), static_cast<int>(m_lastRectResult.y), static_cast<int>(m_lastRectResult.width), static_cast<int>(m_lastRectResult.height));
}
//---------------------------------------------------------------------------
cv::Rect TKalmanFilter::Update(cv::Rect rect, bool dataCorrect)
{
if (!m_initialized)
{
if (m_initialRects.size() < MIN_INIT_VALS)
{
if (dataCorrect)
{
m_initialRects.push_back(rect);
}
}
if (m_initialRects.size() == MIN_INIT_VALS)
{
std::vector<Point_t> initialPoints;
Point_t averageSize(0, 0);
for (const auto& r : m_initialRects)
{
initialPoints.push_back(Point_t(r.x, r.y));
averageSize.x += r.width;
averageSize.y += r.height;
}
averageSize.x /= MIN_INIT_VALS;
averageSize.y /= MIN_INIT_VALS;
track_t kx = 0;
track_t bx = 0;
track_t ky = 0;
track_t by = 0;
get_lin_regress_params(initialPoints, 0, MIN_INIT_VALS, kx, bx, ky, by);
cv::Rect_<track_t> rect0(kx * (MIN_INIT_VALS - 1) + bx, ky * (MIN_INIT_VALS - 1) + by, averageSize.x, averageSize.y);
Point_t rectv0(kx, ky);
switch (m_type)
{
case tracking::KalmanLinear:
CreateLinear(rect0, rectv0);
break;
case tracking::KalmanUnscented:
#ifdef USE_OCV_UKF
CreateUnscented(rect0, rectv0);
#else
CreateLinear(rect0, rectv0);
std::cerr << "UnscentedKalmanFilter was disabled in CMAKE! Set KalmanLinear in constructor." << std::endl;
#endif
break;
case tracking::KalmanAugmentedUnscented:
#ifdef USE_OCV_UKF
CreateAugmentedUnscented(rect0, rectv0);
#else
CreateLinear(rect0, rectv0);
std::cerr << "AugmentedUnscentedKalmanFilter was disabled in CMAKE! Set KalmanLinear in constructor." << std::endl;
#endif
break;
}
}
}
if (m_initialized)
{
cv::Mat measurement(4, 1, Mat_t(1));
if (!dataCorrect)
{
measurement.at<track_t>(0) = m_lastRectResult.x; // update using prediction
measurement.at<track_t>(1) = m_lastRectResult.y;
measurement.at<track_t>(2) = m_lastRectResult.width;
measurement.at<track_t>(3) = m_lastRectResult.height;
}
else
{
measurement.at<track_t>(0) = static_cast<track_t>(rect.x); // update using measurements
measurement.at<track_t>(1) = static_cast<track_t>(rect.y);
measurement.at<track_t>(2) = static_cast<track_t>(rect.width);
measurement.at<track_t>(3) = static_cast<track_t>(rect.height);
}
// Correction
cv::Mat estimated;
switch (m_type)
{
case tracking::KalmanLinear:
estimated = m_linearKalman->correct(measurement);
m_lastRectResult.x = estimated.at<track_t>(0); //update using measurements
m_lastRectResult.y = estimated.at<track_t>(1);
m_lastRectResult.width = estimated.at<track_t>(2);
m_lastRectResult.height = estimated.at<track_t>(3);
break;
case tracking::KalmanUnscented:
case tracking::KalmanAugmentedUnscented:
#ifdef USE_OCV_UKF
estimated = m_uncsentedKalman->correct(measurement);
m_lastRectResult.x = estimated.at<track_t>(0); //update using measurements
m_lastRectResult.y = estimated.at<track_t>(1);
m_lastRectResult.width = estimated.at<track_t>(6);
m_lastRectResult.height = estimated.at<track_t>(7);
#else
estimated = m_linearKalman->correct(measurement);
m_lastRectResult.x = estimated.at<track_t>(0); //update using measurements
m_lastRectResult.y = estimated.at<track_t>(1);
m_lastRectResult.width = estimated.at<track_t>(2);
m_lastRectResult.height = estimated.at<track_t>(3);
std::cerr << "UnscentedKalmanFilter was disabled in CMAKE! Set KalmanLinear in constructor." << std::endl;
#endif
break;
}
}
else
{
if (dataCorrect)
{
m_lastRectResult.x = static_cast<track_t>(rect.x);
m_lastRectResult.y = static_cast<track_t>(rect.y);
m_lastRectResult.width = static_cast<track_t>(rect.width);
m_lastRectResult.height = static_cast<track_t>(rect.height);
}
}
return cv::Rect(static_cast<int>(m_lastRectResult.x), static_cast<int>(m_lastRectResult.y), static_cast<int>(m_lastRectResult.width), static_cast<int>(m_lastRectResult.height));
}