14 _max_missed = max_missed;
16 _nms_iou_thresh = nms_iou_thresh;
25 float in = (bb_test & bb_gt).area();
26 float un = bb_test.area() + bb_gt.area() - in;
31 return (
double)(in / un);
36 cv::Rect_<float> bb_test,
37 cv::Rect_<float> bb_gt)
39 float bb_test_centroid_x = (bb_test.x + bb_test.width / 2);
40 float bb_test_centroid_y = (bb_test.y + bb_test.height / 2);
42 float bb_gt_centroid_x = (bb_gt.x + bb_gt.width / 2);
43 float bb_gt_centroid_y = (bb_gt.y + bb_gt.height / 2);
45 double distance = (double)sqrt(pow(bb_gt_centroid_x - bb_test_centroid_x, 2) + pow(bb_gt_centroid_y - bb_test_centroid_y, 2));
51 void apply_nms(vector<TrackingBox>& detections,
double nms_iou_thresh) {
52 if (detections.empty())
return;
56 return a.confidence > b.confidence;
59 vector<bool> suppressed(detections.size(),
false);
61 for (
size_t i = 0; i < detections.size(); ++i) {
62 if (suppressed[i])
continue;
64 for (
size_t j = i + 1; j < detections.size(); ++j) {
65 if (suppressed[j])
continue;
67 if (detections[i].classId == detections[j].classId &&
75 vector<TrackingBox> filtered;
76 for (
size_t i = 0; i < detections.size(); ++i) {
78 filtered.push_back(detections[i]);
81 detections = filtered;
84 void SortTracker::update(vector<cv::Rect> detections_cv,
int frame_count,
double image_diagonal, std::vector<float> confidences, std::vector<int> classIds)
86 vector<TrackingBox> detections;
87 if (trackers.size() == 0)
89 alive_tracker =
false;
91 for (
unsigned int i = 0; i < detections_cv.size(); i++)
93 if (confidences[i] < _min_conf)
continue;
97 tb.
box = cv::Rect_<float>(detections_cv[i]);
100 detections.push_back(tb);
102 KalmanTracker trk =
KalmanTracker(detections.back().box, detections.back().confidence, detections.back().classId, _next_id++);
103 trackers.push_back(trk);
109 for (
unsigned int i = 0; i < detections_cv.size(); i++)
111 if (confidences[i] < _min_conf)
continue;
114 tb.
box = cv::Rect_<float>(detections_cv[i]);
117 detections.push_back(tb);
123 for (
auto it = frameTrackingResult.begin(); it != frameTrackingResult.end(); it++)
125 int frame_age = frame_count - it->frame;
126 if (frame_age >= _max_age || frame_age < 0)
128 dead_trackers_id.push_back(it->id);
135 predictedBoxes.clear();
136 for (
unsigned int i = 0; i < trackers.size();)
138 cv::Rect_<float> pBox = trackers[i].predict();
139 if (pBox.x >= 0 && pBox.y >= 0)
141 predictedBoxes.push_back(pBox);
145 trackers.erase(trackers.begin() + i);
148 trkNum = predictedBoxes.size();
149 detNum = detections.size();
152 cost_matrix.resize(trkNum, vector<double>(detNum, 0));
154 for (
unsigned int i = 0; i < trkNum; i++)
156 for (
unsigned int j = 0; j < detNum; j++)
158 double iou = GetIOU(predictedBoxes[i], detections[j].box);
159 double dist = GetCentroidsDistance(predictedBoxes[i], detections[j].box) / image_diagonal;
160 if (trackers[i].classId != detections[j].classId || dist > max_centroid_dist_norm)
162 cost_matrix[i][j] = 1e9;
166 cost_matrix[i][j] = 1 - iou + (1 - detections[j].confidence) * 0.1;
173 HungAlgo.
Solve(cost_matrix, assignment);
175 unmatchedTrajectories.clear();
176 unmatchedDetections.clear();
178 matchedItems.clear();
182 for (
unsigned int n = 0; n < detNum; n++)
185 for (
unsigned int i = 0; i < trkNum; ++i)
186 matchedItems.insert(assignment[i]);
188 set_difference(allItems.begin(), allItems.end(),
189 matchedItems.begin(), matchedItems.end(),
190 insert_iterator<set<int>>(unmatchedDetections, unmatchedDetections.begin()));
192 else if (detNum < trkNum)
194 for (
unsigned int i = 0; i < trkNum; ++i)
195 if (assignment[i] == -1)
196 unmatchedTrajectories.insert(i);
202 matchedPairs.clear();
203 for (
unsigned int i = 0; i < trkNum; ++i)
205 if (assignment[i] == -1)
207 if (cost_matrix[i][assignment[i]] > 1 - _min_iou)
209 unmatchedTrajectories.insert(i);
210 unmatchedDetections.insert(assignment[i]);
213 matchedPairs.push_back(cv::Point(i, assignment[i]));
216 for (
unsigned int i = 0; i < matchedPairs.size(); i++)
218 int trkIdx = matchedPairs[i].x;
219 int detIdx = matchedPairs[i].y;
220 trackers[trkIdx].update(detections[detIdx].box);
221 trackers[trkIdx].classId = detections[detIdx].classId;
222 trackers[trkIdx].confidence = detections[detIdx].confidence;
226 for (
auto umd : unmatchedDetections)
229 trackers.push_back(tracker);
232 for (
auto it2 = dead_trackers_id.begin(); it2 != dead_trackers_id.end(); it2++)
234 for (
unsigned int i = 0; i < trackers.size();)
236 if (trackers[i].m_id == (*it2))
238 trackers.erase(trackers.begin() + i);
246 frameTrackingResult.clear();
247 for (
unsigned int i = 0; i < trackers.size();)
249 if ((trackers[i].m_hits >= _min_hits && trackers[i].m_time_since_update <= _max_missed) ||
250 frame_count <= _min_hits)
252 alive_tracker =
true;
254 res.
box = trackers[i].get_state();
255 res.
id = trackers[i].m_id;
256 res.
frame = frame_count;
257 res.
classId = trackers[i].classId;
259 frameTrackingResult.push_back(res);
263 if (trackers[i].m_time_since_update >= _max_age)
265 trackers.erase(trackers.begin() + i);