1 #include "MultiTracker.h"
7 using namespace suanzi;
11 static const std::string TAG = "MultiTracker";
12 static const cv::Size PREFERRED_SIZE = Size(64, 128);
14 #define MaxCost 100000
16 MultiTracker::MultiTracker()
18 LOG_DEBUG(TAG, "init - loading model.pkl");
19 predictor = PredictorWrapper::create("./python/model.pkl");
21 this->descriptor = {Size(64, 128), Size(16, 16), Size(8, 8), Size(8, 8), 9};
23 std::vector<double> ff (40, 1);
24 double prob = predictor->predict(4, ff);
27 MultiTracker::~MultiTracker()
34 static std::vector<double> similarity(const PatchPtr p1, const PatchPtr p2)
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());
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));
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));
57 Detection& d1 = p1->detection;
58 Detection& d2 = p2->detection;
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);
64 double iou_ratio = 0.03;
65 feature.push_back(iou_ratio);
71 double MultiTracker::distance(TrackerPtr tracker, const cv::Mat& image, const Detection& d)
73 PatchPtr patch = createPatch(image, d);
74 std::vector<double> features;
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());
81 double prob = predictor->predict(4, features);
85 void MultiTracker::update(unsigned int total, const Detection* detections, const Mat& image)
87 int row = trackers.size();
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++){
93 cost_matrix(i, j) = distance(trackers[i], image, detections[j]);
98 Eigen::VectorXi tracker_inds, bb_inds;
99 linear_sum_assignment(cost_matrix, tracker_inds, bb_inds);
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]);
108 for (auto t : unmatched_trackers){
109 t->updateState(image);
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);
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);
127 PatchPtr pp = createPatch(image, dd);
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)
135 return image.clone();
138 PatchPtr MultiTracker::createPatch(const Mat& image, const Detection& detect)
140 PatchPtr patch(new Patch());
142 // calculate hog descriptors, size is 3780
144 im = image_crop(image, detect);
145 resize(im, im2, PREFERRED_SIZE);
146 vector<float> feature_hog;
147 this->descriptor.compute(im2, feature_hog);
149 // calculate histogram, size is (64 x 45)
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();
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);