当前位置:   article > 正文

数据关联_3.7

数据关联_3.7

目标

  • 利用匈牙利算法对目标框和检测框进行关联

在这里我们对检测框和跟踪框进行匹配,整个流程是遍历检测框和跟踪框,并进行匹配,匹配成功的将其保留,未成功的将其删除。

  1. def associate_detections_to_trackers(detections, trackers, iou_threshold=0.3):
  2. """
  3. 将检测框bbox与卡尔曼滤波器的跟踪框进行关联匹配
  4. :param detections:检测框
  5. :param trackers:跟踪框,即跟踪目标
  6. :param iou_threshold:IOU阈值
  7. :return:跟踪成功目标的矩阵:matchs
  8. 新增目标的矩阵:unmatched_detections
  9. 跟踪失败即离开画面的目标矩阵:unmatched_trackers
  10. """
  11. # 跟踪目标数量为0,直接构造结果
  12. if (len(trackers) == 0) or (len(detections) == 0):
  13. return np.empty((0, 2), dtype=int), np.arange(len(detections)), np.empty((0, 5), dtype=int)
  14. # iou 不支持数组计算。逐个计算两两间的交并比,调用 linear_assignment 进行匹配
  15. iou_matrix = np.zeros((len(detections), len(trackers)), dtype=np.float32)
  16. # 遍历目标检测的bbox集合,每个检测框的标识为d
  17. for d, det in enumerate(detections):
  18. # 遍历跟踪框(卡尔曼滤波器预测)bbox集合,每个跟踪框标识为t
  19. for t, trk in enumerate(trackers):
  20. iou_matrix[d, t] = iou(det, trk)
  21. # 通过匈牙利算法将跟踪框和检测框以[[d,t]...]的二维矩阵的形式存储在match_indices中
  22. result = linear_sum_assignment(-iou_matrix)
  23. matched_indices = np.array(list(zip(*result)))
  24. # 记录未匹配的检测框及跟踪框
  25. # 未匹配的检测框放入unmatched_detections中,表示有新的目标进入画面,要新增跟踪器跟踪目标
  26. unmatched_detections = []
  27. for d, det in enumerate(detections):
  28. if d not in matched_indices[:, 0]:
  29. unmatched_detections.append(d)
  30. # 未匹配的跟踪框放入unmatched_trackers中,表示目标离开之前的画面,应删除对应的跟踪器
  31. unmatched_trackers = []
  32. for t, trk in enumerate(trackers):
  33. if t not in matched_indices[:, 1]:
  34. unmatched_trackers.append(t)
  35. # 将匹配成功的跟踪框放入matches中
  36. matches = []
  37. for m in matched_indices:
  38. # 过滤掉IOU低的匹配,将其放入到unmatched_detections和unmatched_trackers
  39. if iou_matrix[m[0], m[1]] < iou_threshold:
  40. unmatched_detections.append(m[0])
  41. unmatched_trackers.append(m[1])
  42. # 满足条件的以[[d,t]...]的形式放入matches中
  43. else:
  44. matches.append(m.reshape(1, 2))
  45. # 初始化matches,以np.array的形式返回
  46. if len(matches) == 0:
  47. matches = np.empty((0, 2), dtype=int)
  48. else:
  49. matches = np.concatenate(matches, axis=0)
  50. return matches, np.array(unmatched_detections), np.array(unmatched_trackers)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/337704
推荐阅读
相关标签
  

闽ICP备14008679号