37595b4e83968d45d1ccebc3cdc38483ddb99b2d
[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, const Detection& detection, int id) 
10 : id(id)
11 {
12     KF.transitionMatrix = (Mat_<double>(4, 4) << 
13                                                 1, 0, 1, 0,
14                                                 0, 1, 0, 1,
15                                                 0, 0, 1, 0,
16                                                 0, 0, 0, 1);
17
18     KF.measurementMatrix = (Mat_<double>(2, 2) << 
19                                                 1, 0, 0, 0,
20                                                 0, 1, 0, 0);
21
22     KF.processNoiseCov = 1e-5 * Mat_<double>::eye(4, 4);
23     KF.measurementNoiseCov = 1e-1 * Mat_<double>::ones(2, 2);
24     KF.errorCovPost = 1. * Mat_<double>::ones(4, 4);
25     randn(KF.statePre, Scalar::all(0), Scalar::all(0.1));
26     KF.statePost = (Mat_<double>(4, 1) << detection.center_x, detection.center_y, 0, 0);
27 }
28
29 Tracker::~Tracker()
30 {
31     patches.clear();
32 }
33
34 void Tracker::updateState(const Mat& image)
35 {
36     preStatus = this->status;
37     int lost_age = this->age - this->last_active;
38     int active_age = this->last_active;
39
40     if (lost_age >= MaxLost){
41         status = TrackerStatus::Delete;
42     } else if (lost_age >= 1 && active_age == 1){
43         status = TrackerStatus::Delete;
44     } else if (lost_age >= 1) {
45         status = TrackerStatus::Lost;
46     }
47 }
48
49 void Tracker::addPatch(PatchPtr p)
50 {
51     patches.insert(patches.begin(), p);
52     if (patches.size() > MaxPatch){
53         patches.erase(patches.end());
54     }
55 }
56
57 void Tracker::correct(const cv::Mat& image, const Detection& detection)
58 {
59     // detection.center_x,   detection.center_y, 
60     // KF.correct(detect.center_x, detect.center_y);
61     preStatus = status;
62     status = TrackerStatus::Active;
63     last_active = age;
64 }
65
66 void Tracker::predict()
67 {
68     age++;
69     // TODO
70     Mat temp = KF.predict();
71 }