-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtracker.cpp
122 lines (107 loc) · 2.08 KB
/
tracker.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
#include "tracker.h"
Tracker::Tracker()
: kf(4,2)
{
KalmanFilter<double>::matrix R(2,2); // measurement noise
KalmanFilter<double>::matrix Q(4,4); // process noise
KalmanFilter<double>::matrix trans(4,4);
KalmanFilter<double>::matrix H(2,4);
/*
* 100 0.00000
* 0.00000 100
*/
R.clear();
R(0,0) = 100;
R(1,1) = 100;
/*
* 0.01 0.00 0.00 0.00
* 0.00 0.01 0.00 0.00
* 0.00 0.00 0.01 0.00
* 0.00 0.00 0.00 0.01
*/
Q.clear();
Q(0,0) = 0.01;
Q(1,1) = 0.01;
Q(2,2) = 0.01;
Q(3,3) = 0.01;
/*
* 1 0 1 0
* 0 1 0 1
* 0 0 1 0
* 0 0 0 1
*/
trans.clear();
trans(0,0) = 1;
trans(0,1) = 0;
trans(0,2) = 1;
trans(0,3) = 0;
trans(1,0) = 0;
trans(1,1) = 1;
trans(1,2) = 0;
trans(1,3) = 1;
trans(2,0) = 0;
trans(2,1) = 0;
trans(2,2) = 1;
trans(2,3) = 0;
trans(3,0) = 0;
trans(3,1) = 0;
trans(3,2) = 0;
trans(3,3) = 1;
/*
* 1 0 0 0
* 0 1 0 0
*/
H.clear();
H(0,0) = 1;
H(0,1) = 0;
H(0,2) = 0;
H(0,3) = 0;
H(1,0) = 0;
H(1,1) = 1;
H(1,2) = 0;
H(1,3) = 0;
kf.setTransitionModel(trans);
kf.setMeasurementModel(H);
kf.setMeasurementNoise(R);
kf.setProcessNoise(Q);
}
QPoint Tracker::initializeStartState(QPoint pos)
{
KalmanFilter<double>::vector state(4);
KalmanFilter<double>::matrix covErr(4,4);
/*
* x
* y
* 0
* 0
*/
state.clear();
state(0) = pos.x(); // X coord
state(1) = pos.y(); // Y coord
state(2) = 0; // Vx
state(3) = 0; // Vy
/*
* 0.1 0.0 0.0 0.0
* 0.0 0.1 0.0 0.0
* 0.0 0.0 0.1 0.0
* 0.0 0.0 0.0 0.1
*/
covErr.clear();
covErr(0,0) = 0.1;
covErr(1,1) = 0.1;
covErr(2,2) = 0.1;
covErr(3,3) = 0.1;
kf.initializeState(state,covErr);
auto pred = kf.predict();
return QPoint(pred.first(0),pred.first(1));
}
std::pair<QPoint,QPoint> Tracker::getTrackPosition(QPoint pos)
{
KalmanFilter<double>::vector z(2);
z(0) = pos.x();
z(1) = pos.y();
auto corr = kf.correct(z);
auto pred = kf.predict();
std::pair<QPoint,QPoint> result(QPoint(pred.first(0),pred.first(1)),QPoint(corr.first(0),corr.first(1)));
return result;
}