forked from Vertical-Beach/ByteTrack-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKalmanFilter.cpp
More file actions
89 lines (73 loc) · 3.19 KB
/
Copy pathKalmanFilter.cpp
File metadata and controls
89 lines (73 loc) · 3.19 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
#include "ByteTrack/KalmanFilter.h"
#include <cstddef>
byte_track::KalmanFilter::KalmanFilter(const float& std_weight_position,
const float& std_weight_velocity) :
std_weight_position_(std_weight_position),
std_weight_velocity_(std_weight_velocity)
{
constexpr size_t ndim = 4;
constexpr float dt = 1;
motion_mat_ = Eigen::MatrixXf::Identity(8, 8);
update_mat_ = Eigen::MatrixXf::Identity(4, 8);
for (size_t i = 0; i < ndim; i++)
{
motion_mat_(i, ndim + i) = dt;
}
}
void byte_track::KalmanFilter::initiate(StateMean &mean, StateCov &covariance, const DetectBox &measurement)
{
mean.block<1, 4>(0, 0) = measurement.block<1, 4>(0, 0);
mean.block<1, 4>(0, 4) = Eigen::Vector4f::Zero();
StateMean std;
std(0) = 2 * std_weight_position_ * measurement[3];
std(1) = 2 * std_weight_position_ * measurement[3];
std(2) = 1e-2;
std(3) = 2 * std_weight_position_ * measurement[3];
std(4) = 10 * std_weight_velocity_ * measurement[3];
std(5) = 10 * std_weight_velocity_ * measurement[3];
std(6) = 1e-5;
std(7) = 10 * std_weight_velocity_ * measurement[3];
StateMean tmp = std.array().square();
covariance = tmp.asDiagonal();
}
void byte_track::KalmanFilter::predict(StateMean &mean, StateCov &covariance)
{
StateMean std;
std(0) = std_weight_position_ * mean(3);
std(1) = std_weight_position_ * mean(3);
std(2) = 1e-2;
std(3) = std_weight_position_ * mean(3);
std(4) = std_weight_velocity_ * mean(3);
std(5) = std_weight_velocity_ * mean(3);
std(6) = 1e-5;
std(7) = std_weight_velocity_ * mean(3);
StateMean tmp = std.array().square();
StateCov motion_cov = tmp.asDiagonal();
mean = motion_mat_ * mean.transpose();
covariance = motion_mat_ * covariance * (motion_mat_.transpose()) + motion_cov;
}
void byte_track::KalmanFilter::update(StateMean &mean, StateCov &covariance, const DetectBox &measurement)
{
StateHMean projected_mean;
StateHCov projected_cov;
project(projected_mean, projected_cov, mean, covariance);
Eigen::Matrix<float, 4, 8> B = (covariance * (update_mat_.transpose())).transpose();
Eigen::Matrix<float, 8, 4> kalman_gain = (projected_cov.llt().solve(B)).transpose();
Eigen::Matrix<float, 1, 4> innovation = measurement - projected_mean;
const auto tmp = innovation * (kalman_gain.transpose());
mean = (mean.array() + tmp.array()).matrix();
covariance = covariance - kalman_gain * projected_cov * (kalman_gain.transpose());
}
void byte_track::KalmanFilter::project(StateHMean &projected_mean, StateHCov &projected_covariance,
const StateMean& mean, const StateCov& covariance)
{
DetectBox std;
std << std_weight_position_ * mean(3),
std_weight_position_ * mean(3),
1e-1,
std_weight_position_ * mean(3);
projected_mean = update_mat_ * mean.transpose();
projected_covariance = update_mat_ * covariance * (update_mat_.transpose());
Eigen::Matrix<float, 4, 4> diag = std.asDiagonal();
projected_covariance += diag.array().square().matrix();
}