KalmanBoxTracker.cpp
4.05 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
#include "KalmanBoxTracker.h"
float IoU(vector<float> &bb_test, vector<float> &bb_gt)
{
float xx1, yy1, xx2, yy2, w, h, wh, o;
xx1 = max(bb_test[0], bb_gt[0]);
yy1 = max(bb_test[1], bb_gt[1]);
xx2 = min(bb_test[2], bb_gt[2]);
yy2 = min(bb_test[3], bb_gt[3]);
w = max(float(0), (xx2 - xx1));
h = max(float(0), (yy2 - yy1));
wh = w * h;
o = wh / ((bb_test[2] - bb_test[0])*(bb_test[3] - bb_test[1])
+ (bb_gt[2] - bb_gt[0])*(bb_gt[3] - bb_gt[1]) - wh);
return o;
}
void convert_bbox_to_z(vector<float> &bbox, vector<float> &z)
{
float w, h, x, y, s, r;
w = bbox[2] - bbox[0];
h = bbox[3] - bbox[1];
x = bbox[0] + w / 2;
y = bbox[1] + h / 2;
s = w * h; //scale is just area
r = w / h;
z.push_back(x);
z.push_back(y);
z.push_back(s);
z.push_back(r);
}
void convert_x_to_bbox(cv::Mat &x, vector<float> &bbox)
{
float w, h;
w = sqrt(x.at<float>(2) * x.at<float>(3));
h = x.at<float>(2) / w;
//bbox[x1,y1,x2,y2];
bbox.push_back(x.at<float>(0) - w / 2);
bbox.push_back(x.at<float>(1) - h / 2);
bbox.push_back(x.at<float>(0) + w / 2);
bbox.push_back(x.at<float>(1) + h / 2);
}
KalmanBoxTracker::KalmanBoxTracker(vector<float> &bbox, int trackLength)
{
KF.init(7, 4, 0); //初始化卡尔曼滤波器对象KF
state = cv::Mat::zeros(7, 1, CV_32F);
processNoise = cv::Mat::zeros(7, 1, CV_32F);
measurement = cv::Mat::zeros(4, 1, CV_32F); //定义测量值
KF.transitionMatrix = (cv::Mat_<float>(7, 7) << \
1, 0, 0, 0, 1, 0, 0, \
0, 1, 0, 0, 0, 1, 0, \
0, 0, 1, 0, 0, 0, 1, \
0, 0, 0, 1, 0, 0, 0, \
0, 0, 0, 0, 1, 0, 0, \
0, 0, 0, 0, 0, 1, 0, \
0, 0, 0, 0, 0, 0, 1); //状态转移矩阵A
KF.measurementMatrix = (cv::Mat_<float>(4, 7) << \
1, 0, 0, 0, 0, 0, 0, \
0, 1, 0, 0, 0, 0, 0, \
0, 0, 1, 0, 0, 0, 0, \
0, 0, 0, 1, 0, 0, 0); //测量矩阵H
KF.measurementNoiseCov = (cv::Mat_<float>(4, 4) << \
1, 0, 0, 0, \
0, 1, 0, 0, \
0, 0, 10, 0, \
0, 0, 0, 10); //测量噪声方差矩阵R
KF.errorCovPost = (cv::Mat_<float>(7, 7) << \
10, 0, 0, 0, 0, 0, 0, \
0, 10, 0, 0, 0, 0, 0, \
0, 0, 10, 0, 0, 0, 0, \
0, 0, 0, 10, 0, 0, 0, \
0, 0, 0, 0, 10000, 0, 0, \
0, 0, 0, 0, 0, 10000, 0, \
0, 0, 0, 0, 0, 0, 10000); //后验错误估计协方差矩阵P
KF.processNoiseCov = (cv::Mat_<float>(7, 7) << \
1, 0, 0, 0, 0, 0, 0, \
0, 1, 0, 0, 0, 0, 0, \
0, 0, 1, 0, 0, 0, 0, \
0, 0, 0, 1, 0, 0, 0, \
0, 0, 0, 0, 0.01, 0, 0, \
0, 0, 0, 0, 0, 0.01, 0, \
0, 0, 0, 0, 0, 0, 0.0001); //系统噪声方差矩阵Q
vector<float> z;
convert_bbox_to_z(bbox, z);
KF.statePost = (cv::Mat_<float>(7, 1) << z[0], z[1], z[2], z[3], 0, 0, 0); //corrected state
state = (cv::Mat_<float>(7, 1) << z[0], z[1], z[2], z[3], 0, 0, 0);
time_since_update = 0;
cls = bbox[5];
//history
//hits = 0;
hit_streak = 0;
age = 0;
m_trackLength = trackLength;
//history = new cycleQueue<vector<float>>(trackLength);
history.set_param(trackLength);
}
KalmanBoxTracker::~KalmanBoxTracker()
{
/*int trackerSize = history.size();
for (int i = 0; i < trackerSize; i++)
{
history.get(i).clear();
vector<float>().swap(history.get(i));
}*/
/*if (history != NULL)
{
delete history;
history = NULL;
}*/
}
void KalmanBoxTracker::update(vector<float> &bbox)
{
time_since_update = 0;
//history
//hits += 1;
hit_streak += 1;
vector<float> z;
convert_bbox_to_z(bbox, z);
measurement = (cv::Mat_<float>(4, 1) << z[0], z[1], z[2], z[3]);
KF.correct(measurement);
}
vector<float> KalmanBoxTracker::predict()
{
if ((KF.statePost.at<float>(6) + KF.statePost.at<float>(2)) <= 0)
KF.statePost.at<float>(6) *= 0.0;
KF.predict();
age += 1;
if (time_since_update >= FusionInterval)
hit_streak = 0;
time_since_update += 1;
vector<float> bbox;
convert_x_to_bbox(KF.statePost, bbox);
//history.push_back(bbox);
TRACK_POINT tmp_point;
tmp_point.x = bbox[0] + (bbox[2] - bbox[0]) / 2;
tmp_point.y = bbox[1] + (bbox[3] - bbox[1]) / 2;
history.push(tmp_point);
return bbox;
}
vector<float> KalmanBoxTracker::get_state()
{
vector<float> bbox;
convert_x_to_bbox(KF.statePost, bbox);
return bbox;
}