8ab241491303389b8952792c5b8938ad04b0b6f1
[trackerpp.git] / src / Tracker.cpp
1 #include "Tracker.h"
2
3 using namespace suanzi;
4 using namespace cv;
5 using namespace std;
6
7 static const int MaxLost = 5;
8
9 Tracker::Tracker(const cv::Mat& image,int id) : id(id)
10 {
11     status = TrackerStatus::Fire;
12     preStatus = TrackerStatus::Fire;
13
14     // TODO: Kalman filter
15     this->kf.transitionMatrix = (Mat_<float>(4, 4) << 
16                                                 1, 0, 1, 0,
17                                                 0, 1, 0, 1,
18                                                 0, 0, 1, 0,
19                                                 0, 0, 0, 1);
20
21     this->kf.measurementMatrix = (Mat_<float>(2, 2) << 
22                                                 1, 0, 0, 0,
23                                                 0, 1, 0, 0);
24
25     this->kf.processNoiseCov = 1e-5 * Mat_<float>::eye(4, 4);
26     this->kf.measurementNoiseCov = 1e-1 * Mat_<float>::ones(2, 2);
27     this->kf.errorCovPost = 1. * Mat_<float>::ones(4, 4);
28 }
29
30 Tracker::~Tracker()
31 {
32     patches.clear();
33 }
34
35 void Tracker::updateState(const Mat& image)
36 {
37     preStatus = this->status;
38     int lost_age = this->age - this->last_active;
39     int active_age = this->last_active;
40
41     if (lost_age >= MaxLost){
42         status = TrackerStatus::Delete;
43     } else if (lost_age >= 1 && active_age == 1){
44         status = TrackerStatus::Delete;
45     } else if (lost_age >= 1) {
46         status = TrackerStatus::Lost;
47     }
48 }
49
50 void Tracker::addPatch(PatchPtr p)
51 {
52     this->patches.push_back(p);
53 }