upgrade opencv to 3.3.1, complete patch similarity
[trackerpp.git] / src / MultiTracker.cpp
1 #include "MultiTracker.h"
2 #include "Metrics.h"
3 #include <algorithm>
4 #include "hungarian.h"
5 #include "Logger.h"
6
7 using namespace suanzi;
8 using namespace cv;
9 using namespace std;
10
11 static const std::string TAG = "MultiTracker";
12 static const cv::Size PREFERRED_SIZE = Size(64, 128);
13
14 #define MaxCost  100000
15
16 MultiTracker::MultiTracker()
17 {
18     LOG_DEBUG(TAG, "init - loading model.pkl");
19     predictor = PredictorWrapper::create("./python/model.pkl");
20     predictor->dump();
21     this->descriptor = {Size(64, 128), Size(16, 16), Size(8, 8), Size(8, 8), 9};
22
23     std::vector<double> ff (40, 1);
24     double prob = predictor->predict(4, ff);
25 }
26
27 MultiTracker::~MultiTracker()
28 {
29     predictor.reset();
30     trackers.clear();
31 }
32
33
34 static std::vector<double> similarity(const PatchPtr p1, const PatchPtr p2)
35 {
36     std::vector<double> feature;
37     cv::Mat im1(PREFERRED_SIZE, p1->image_crop.type());
38     cv::Mat im2(PREFERRED_SIZE, p2->image_crop.type());
39     cv::resize(p1->image_crop, im1, im1.size());
40     cv::resize(p2->image_crop, im2, im2.size());
41     cv::Mat result;
42     cv::matchTemplate(im1, im2, result, CV_TM_CCOEFF_NORMED);
43     feature.push_back(result.at<double>(0, 0));
44     cv::matchTemplate(im1, im2, result, CV_TM_CCORR_NORMED);
45     feature.push_back(result.at<double>(0, 0));
46
47
48     vector<double>& f1_hog = p1->features.first; Mat f1_hue = p1->features.second;
49     vector<double>& f2_hog = p1->features.first; Mat f2_hue = p1->features.second;
50     feature.push_back(distance_cosine(Eigen::Map<Eigen::VectorXd>(f1_hog.data(), f1_hog.size()), 
51                                     Eigen::Map<Eigen::VectorXd>(f2_hog.data(), f2_hog.size())));
52     feature.push_back(distance_euclidean(Eigen::Map<Eigen::VectorXd>(f1_hog.data(), f1_hog.size()), 
53                                     Eigen::Map<Eigen::VectorXd>(f2_hog.data(), f2_hog.size())));
54     feature.push_back(compareHist(f1_hue, f2_hue, HISTCMP_CORREL));
55     feature.push_back(compareHist(f1_hue, f2_hue, HISTCMP_HELLINGER));
56
57     Detection& d1 = p1->detection;
58     Detection& d2 = p2->detection;
59
60     double center_distance = sqrt(pow((d1.center_x - d2.center_x), 2) + pow((d1.center_y - d2.center_y), 2));
61     feature.push_back(center_distance / (d1.width + d1.height + d2.width + d2.height) * 4);
62
63     //TODO
64     double iou_ratio = 0.03;
65     feature.push_back(iou_ratio);
66
67     return feature;
68 }
69
70
71 double MultiTracker::distance(TrackerPtr tracker, const cv::Mat& image, const Detection& d)
72 {
73     PatchPtr patch = createPatch(image, d);
74     std::vector<double> features;
75
76     std::vector<double> ss;
77     for (auto i : tracker->patches){
78         ss = similarity(i, patch);
79         features.insert(features.end(),  ss.begin(), ss.end());
80     }
81     double prob = predictor->predict(4, features);
82     return prob; 
83 }
84
85 void MultiTracker::update(unsigned int total, const Detection* detections, const Mat& image)
86 {
87     int row = trackers.size();
88     int col = total;
89     Eigen::MatrixXi cost_matrix = Eigen::MatrixXi::Zero(row, col);
90     for (int i = 0; i < row; i++){
91         for (int j = 0; j < col; j++){
92             // TODO
93             cost_matrix(i, j) = distance(trackers[i], image, detections[j]);
94         }
95     }
96
97     // assignment
98     Eigen::VectorXi tracker_inds, bb_inds;
99     linear_sum_assignment(cost_matrix, tracker_inds, bb_inds);
100
101     // handle unmatched trackers
102     vector<TrackerPtr> unmatched_trackers;
103     for (int i = 0; i < row; i++){
104         if (!(tracker_inds.array() == i).any()){
105             unmatched_trackers.push_back(trackers[i]);
106         }
107     }
108     for (auto t : unmatched_trackers){
109         t->updateState(image);
110     }
111
112     // handle unmatched detections
113     vector<int> unmatched_detection;
114     for(int j = 0; j < col; j++){
115         if (!(bb_inds.array() == j).any()){
116             unmatched_detection.push_back(j);
117         }
118     }
119     // create new trackers for new detections
120     for (auto i : unmatched_detection){
121         TrackerPtr t (new Tracker(image));
122         this->trackers.push_back(t);
123     }
124
125     Detection dd;
126
127     PatchPtr pp = createPatch(image, dd);
128 }
129
130 // Get image crop from input image within given bounding box - Detecinon
131 static cv::Mat image_crop(const cv::Mat& image, const Detection& bb)
132 {
133     // RECT
134     // TODO;
135     return image.clone();
136 }
137
138 PatchPtr MultiTracker::createPatch(const Mat& image, const Detection& detect)
139 {
140     PatchPtr patch(new Patch());
141
142     // calculate hog descriptors, size is 3780
143     Mat im, im2;
144     im = image_crop(image, detect);
145     resize(im, im2, PREFERRED_SIZE);
146     vector<float> feature_hog;
147     this->descriptor.compute(im2, feature_hog);
148
149     // calculate histogram, size is (64 x 45)
150     Mat hsv, hist;
151     cvtColor(im, hsv, COLOR_BGR2HSV);
152     int channels[] = {0, 1};
153     int histSize[] = {45, 64};
154     float hranges[] = {0, 180};
155     float sranges[] = {0, 256};
156     const float* ranges[] = {hranges, sranges};
157     calcHist(&hsv, 1, channels, Mat(), hist, 2, histSize, ranges, true, false);
158     Size sm = hist.size();
159
160     patch->image_crop = im.clone();
161     patch->detection = detect;
162     std::vector<double> feature_hog_double (feature_hog.begin(), feature_hog.end()); // convert to double
163     patch->features = std::make_pair(feature_hog_double, hist);
164     return patch;
165 }