概述
sort(Simple Online and Realtime Tracking),简单的在线实时跟踪,常用于多目标追踪(Multiple Object Tracking)中,其中不涉及到图像识别、特征匹配等算法,sort算法以目标检测结果作为输入,用数据关联算法(匈牙利算法)进行目标匹配,并以卡尔曼进行目标预测,通过IOU也建立目标间的关系,其主要目的在于连续跟踪目标的ID,优点是跟踪速度快。
sort算法流程图
sort关键参数
self.max_age = 1 # 在跟踪列表中有几帧没有关联匹配成功就从跟踪列表中移除,否则就将失去关联计数加1
self.min_hits = 3 # 连续3帧以上都匹配成功,则作为跟踪结果输出
self.iou_threshold = 0.3 # IOU匹配的阈值
self.trackers = [] # 跟踪缓存
self.frame_count = 0 # 帧率统计
sort过程代码
1.从现有跟踪器中获取预测位置
const auto get_pred_boxes = [] (auto &trackers) {
std::vector<cv::Rect2f> boxes;
boxes.reserve(trackers.size());
for (auto it = trackers.begin(); it != trackers.end();)
{
const cv::Rect2f &box = (*it)->Predict();
if (box.x >= 0 && box.y >= 0)
{
boxes.emplace_back(box);
it++;
}
else
{
it = trackers.erase(it);
}
}
return boxes;
};
const std::vector<cv::Rect2f> &pred_boxes = get_pred_boxes(trackers_);
2.得到所有检测框与跟踪框的匹配矩阵
std::vector<int> assignment;
const auto trk_num = pred_boxes.size();
const auto det_num = detected_box.size();
const auto &iou_matrix = CalcIou(detected_box, pred_boxes);
// 使用匈牙利算法进行分配问题
hung_algo_.Solve(iou_matrix, trk_num, det_num, assignment);
// 过滤低IOU的框,得到unmatched_trajectories, unmatched_detections, matched_pairs
3.更新trackers跟踪器
// 对跟踪列表关联成功的检测结果,使用检测结果进行更新
for (unsigned int i = 0; i < matched_pairs.size(); i++)
{
// 跟踪列表索引
const int trk_idx = matched_pairs[i].x;
// 检测列表的索引
const int det_idx = matched_pairs[i].y;
// 用检测结果的信息来更新跟踪结果
trackers_[trk_idx]->Update(detected_box[det_idx]);
}
4.创建并初始化新的跟踪器给没匹配上的检测结果
for (auto umd : unmatched_detections)
{
cv::Rect2f rect;
rect.x = detected_box[umd].rect.x;
rect.y = detected_box[umd].rect.y;
rect.width = detected_box[umd].rect.width;
rect.height = detected_box[umd].rect.height;
trackers_.push_back(std::make_unique<TrackerManager>(rect, counter_));
counter_++;
}
匈牙利算法
匈牙利算法是解决二分图最大匹配问题的算法,是一种数据关联算法,其目的在于匹配个数最大化,而不是匹配的准不准问题,所以可能会匹配错误。
它的输入是代价矩阵IOU。