void Tracker::pruning(Detection &selected_detections, std::vector<int> &final_select, std::vector<track_ptr> &tacks_in) { // TODO const uint &TrackSize = tacks_in.size(); const uint &detSize = selected_detections.size(); final_select.resize(detSize); cv::Mat assigmentsBin = cv::Mat::zeros(cv::Size(detSize, TrackSize), CV_32SC1); cv::Mat costMat = cv::Mat(cv::Size(detSize, TrackSize), CV_32FC1); // cosmatrix (cols rows) std::vector<int> assignments; std::vector<float> costs(detSize * TrackSize); for (uint i = 0; i < TrackSize; ++i) { for (uint j = 0; j < detSize; ++j) { costs.at(i + j * TrackSize) = euclideanDist(selected_detections[j].position, tracks_[i]->GetState()); costMat.at<float>(i, j) = costs.at(i + j * TrackSize); } } // std::cout<<"######## pruning costMat ############### \n"<<costMat<<" \n"<<std::endl; AssignmentProblemSolver APS; // 匈牙利算法 APS.Solve(costs, TrackSize, detSize, assignments, AssignmentProblemSolver::optimal); const uint &assSize = assignments.size(); // 这个的大小应该是检测结果的大小,里边对应的是目标的编号 for (uint i = 0; i < assSize; ++i) { if (assignments[i] != -1 && costMat.at<float>(i, assignments[i]) < 0.8) { assigmentsBin.at<int>(i, assignments[i]) = 1; } } const uint &rows = assigmentsBin.rows; const uint &cols = assigmentsBin.cols; std::vector<bool> choosen(detSize, false); std::vector<bool> trackchoosen(TrackSize, false); for (uint i = 0; i < rows; ++i) { for (uint j = 0; j < cols; ++j) { if (assigmentsBin.at<int>(i, j)) { final_select[j] = tacks_in[i]->GetId(); trackchoosen[i] = true; tracks_[i]->UpdateBox(selected_detections[j]); tracks_[i]->UpdateMeasure(selected_detections[j].position(0), selected_detections[j].position(1)); choosen[j] = true; } } } for (int i = 0; i < choosen.size(); ++i) { if (!choosen[i]) { not_associated_.push_back(selected_detections[i]); } } for (int i = 0; i < trackchoosen.size(); ++i) { if (!trackchoosen[i]) { tracks_[i]->MarkMissed(); } } // std::cout<<"######## pruning not asso ###############"<<not_associated_.size()<<std::endl; }
时间: 2024-04-28 09:23:33 浏览: 112
这段代码实现了目标跟踪的一个重要步骤:匹配检测结果和已有跟踪目标。具体来说,它通过计算每个检测结果和每个跟踪目标之间的欧式距离,构建了一个代价矩阵,然后使用匈牙利算法求解最小代价匹配。匹配成功的检测结果与跟踪目标之间建立一一对应关系,并更新跟踪目标的状态和位置;未匹配成功的检测结果被标记为未关联的目标,未匹配成功的跟踪目标被标记为漏检目标。最终,函数返回匹配成功的目标编号列表和未匹配成功的检测结果列表。需要注意的是,这段代码中使用了OpenCV中的Mat类来处理代价矩阵,其中使用了CV_32SC1和CV_32FC1两种数据类型。
阅读全文