16 void KalmanTracker::init_kf(
21 kf = KalmanFilter(stateNum, measureNum, 0);
23 measurement = Mat::zeros(measureNum, 1, CV_32F);
25 kf.transitionMatrix = (Mat_<float>(8, 8) << 1, 0, 0, 0, 1, 0, 0, 0,
27 0, 1, 0, 0, 0, 1, 0, 0,
28 0, 0, 1, 0, 0, 0, 1, 0,
29 0, 0, 0, 1, 0, 0, 0, 1,
30 0, 0, 0, 0, 1, 0, 0, 0,
31 0, 0, 0, 0, 0, 1, 0, 0,
32 0, 0, 0, 0, 0, 0, 1, 0,
33 0, 0, 0, 0, 0, 0, 0, 1);
35 setIdentity(kf.measurementMatrix);
36 setIdentity(kf.processNoiseCov, Scalar::all(1e-1));
37 kf.processNoiseCov.at<
float>(2, 2) = 1e0;
38 kf.processNoiseCov.at<
float>(3, 3) = 1e0;
39 setIdentity(kf.measurementNoiseCov, Scalar::all(1e-4));
40 setIdentity(kf.errorCovPost, Scalar::all(1e-2));
43 kf.statePost.at<
float>(0, 0) = stateMat.x + stateMat.width / 2;
44 kf.statePost.at<
float>(1, 0) = stateMat.y + stateMat.height / 2;
45 kf.statePost.at<
float>(2, 0) = stateMat.area();
46 kf.statePost.at<
float>(3, 0) = stateMat.width / stateMat.height;
47 kf.statePost.at<
float>(4, 0) = 0.0f;
48 kf.statePost.at<
float>(5, 0) = 0.0f;
49 kf.statePost.at<
float>(6, 0) = 0.0f;
50 kf.statePost.at<
float>(7, 0) = 0.0f;
60 if (m_time_since_update > 0)
62 m_time_since_update += 1;
64 StateType predictBox = get_rect_xysr(p.at<
float>(0, 0), p.at<
float>(1, 0), p.at<
float>(2, 0), p.at<
float>(3, 0));
66 m_history.push_back(predictBox);
67 return m_history.back();
75 StateType predictBox = get_rect_xysr(p.at<
float>(0, 0), p.at<
float>(1, 0), p.at<
float>(2, 0), p.at<
float>(3, 0));
84 m_time_since_update = 0;
90 measurement.at<
float>(0, 0) = stateMat.x + stateMat.width / 2;
91 measurement.at<
float>(1, 0) = stateMat.y + stateMat.height / 2;
92 measurement.at<
float>(2, 0) = stateMat.area();
93 measurement.at<
float>(3, 0) = stateMat.width / stateMat.height;
96 kf.correct(measurement);
105 const double decay = 0.82;
106 const double update_weight = 1.0 - decay;
107 const bool had_history = !classScoreHistory.empty();
109 for (
auto it = classScoreHistory.begin(); it != classScoreHistory.end();) {
111 if (it->second < 0.0001)
112 it = classScoreHistory.erase(it);
117 const double candidate_weight = had_history ? update_weight : 1.0;
118 if (classScores.empty()) {
119 classScoreHistory[fallbackClassId] += fallbackConfidence * candidate_weight;
121 for (
const auto& candidate : classScores) {
122 if (candidate.classId < 0 || candidate.score <= 0.0f)
124 classScoreHistory[candidate.classId] += candidate.score * candidate_weight;
128 if (classScoreHistory.empty()) {
129 classId = fallbackClassId;
133 auto best = std::max_element(
134 classScoreHistory.begin(), classScoreHistory.end(),
135 [](
const auto& a,
const auto& b) { return a.second < b.second; });
136 classId = best->first;
142 Mat s = kf.statePost;
143 return get_rect_xysr(s.at<
float>(0, 0), s.at<
float>(1, 0), s.at<
float>(2, 0), s.at<
float>(3, 0));
153 float w = sqrt(s * r);
155 float x = (cx - w / 2);
156 float y = (cy - h / 2);