20 float in = (bb_test & bb_gt).area();
21 float un = bb_test.area() + bb_gt.area() - in;
26 return (
double)(in / un);
31 cv::Rect_<float> bb_test,
32 cv::Rect_<float> bb_gt)
34 float bb_test_centroid_x = (bb_test.x + bb_test.width / 2);
35 float bb_test_centroid_y = (bb_test.y + bb_test.height / 2);
37 float bb_gt_centroid_x = (bb_gt.x + bb_gt.width / 2);
38 float bb_gt_centroid_y = (bb_gt.y + bb_gt.height / 2);
40 double distance = (double)sqrt(pow(bb_gt_centroid_x - bb_test_centroid_x, 2) + pow(bb_gt_centroid_y - bb_test_centroid_y, 2));
45 void SortTracker::update(vector<cv::Rect> detections_cv,
int frame_count,
double image_diagonal, std::vector<float> confidences, std::vector<int> classIds)
47 vector<TrackingBox> detections;
48 if (trackers.size() == 0)
50 alive_tracker =
false;
52 for (
unsigned int i = 0; i < detections_cv.size(); i++)
56 tb.
box = cv::Rect_<float>(detections_cv[i]);
59 detections.push_back(tb);
62 trackers.push_back(trk);
68 for (
unsigned int i = 0; i < detections_cv.size(); i++)
71 tb.
box = cv::Rect_<float>(detections_cv[i]);
74 detections.push_back(tb);
76 for (
auto it = frameTrackingResult.begin(); it != frameTrackingResult.end(); it++)
78 int frame_age = frame_count - it->frame;
79 if (frame_age >= _max_age || frame_age < 0)
81 dead_trackers_id.push_back(it->id);
88 predictedBoxes.clear();
89 for (
unsigned int i = 0; i < trackers.size();)
91 cv::Rect_<float> pBox = trackers[i].predict();
92 if (pBox.x >= 0 && pBox.y >= 0)
94 predictedBoxes.push_back(pBox);
98 trackers.erase(trackers.begin() + i);
101 trkNum = predictedBoxes.size();
102 detNum = detections.size();
104 centroid_dist_matrix.clear();
105 centroid_dist_matrix.resize(trkNum, vector<double>(detNum, 0));
107 for (
unsigned int i = 0; i < trkNum; i++)
109 for (
unsigned int j = 0; j < detNum; j++)
113 centroid_dist_matrix[i][j] = distance;
119 HungAlgo.
Solve(centroid_dist_matrix, assignment);
121 unmatchedTrajectories.clear();
122 unmatchedDetections.clear();
124 matchedItems.clear();
128 for (
unsigned int n = 0; n < detNum; n++)
131 for (
unsigned int i = 0; i < trkNum; ++i)
132 matchedItems.insert(assignment[i]);
134 set_difference(allItems.begin(), allItems.end(),
135 matchedItems.begin(), matchedItems.end(),
136 insert_iterator<set<int>>(unmatchedDetections, unmatchedDetections.begin()));
138 else if (detNum < trkNum)
140 for (
unsigned int i = 0; i < trkNum; ++i)
141 if (assignment[i] == -1)
142 unmatchedTrajectories.insert(i);
148 matchedPairs.clear();
149 for (
unsigned int i = 0; i < trkNum; ++i)
151 if (assignment[i] == -1)
153 if (centroid_dist_matrix[i][assignment[i]] > max_centroid_dist_norm)
155 unmatchedTrajectories.insert(i);
156 unmatchedDetections.insert(assignment[i]);
159 matchedPairs.push_back(cv::Point(i, assignment[i]));
162 for (
unsigned int i = 0; i < matchedPairs.size(); i++)
164 int trkIdx = matchedPairs[i].x;
165 int detIdx = matchedPairs[i].y;
166 trackers[trkIdx].update(detections[detIdx].box);
167 trackers[trkIdx].classId = detections[detIdx].classId;
168 trackers[trkIdx].confidence = detections[detIdx].confidence;
172 for (
auto umd : unmatchedDetections)
175 trackers.push_back(tracker);
178 for (
auto it2 = dead_trackers_id.begin(); it2 != dead_trackers_id.end(); it2++)
180 for (
unsigned int i = 0; i < trackers.size();)
182 if (trackers[i].m_id == (*it2))
184 trackers.erase(trackers.begin() + i);
192 frameTrackingResult.clear();
193 for (
unsigned int i = 0; i < trackers.size();)
195 if ((trackers[i].m_time_since_update < 1 && trackers[i].m_hit_streak >= _min_hits) || frame_count <= _min_hits)
197 alive_tracker =
true;
199 res.
box = trackers[i].get_state();
200 res.
id = trackers[i].m_id;
201 res.
frame = frame_count;
202 res.
classId = trackers[i].classId;
204 frameTrackingResult.push_back(res);
208 if (trackers[i].m_time_since_update >= _max_age)
210 trackers.erase(trackers.begin() + i);