IoU匹配
IOU匹配算法是以IoU距离为代价矩阵的匈牙利匹配算法。
1. 代码详解
这里只展示相关代码,整个deepsort工程见本人github,代码里添加详细注释,帮助大家理解。
def min_cost_matching(distance_metric, max_distance, tracks, detections,
track_indices=None, detection_indices=None):
if track_indices is None:
track_indices = np.arange(len(tracks))
if detection_indices is None:
detection_indices = np.arange(len(detections))
if len(detection_indices) == 0 or len(track_indices) == 0:
return [], track_indices, detection_indices # Nothing to match.
# 计算成本矩阵
cost_matrix = distance_metric(
tracks, detections, track_indices, detection_indices)
cost_matrix[cost_matrix > max_distance] = max_distance + 1e-5
# 执行匈牙利算法,得到指派成功的索引对,行索引为tracks的索引,列索引为detections的索引
row_indices, col_indices = linear_assignment(cost_matrix)
matches, unmatched_tracks, unmatched_detections = [], [], []
# 找出未匹配的detections
for col, detection_idx in enumerate(detection_indices):
if col not in col_indices:
unmatched_detections.append(detection_idx)
# 找出未匹配的tracks
for row, track_idx in enumerate(track_indices):
if row not in row_indices:
unmatched_tracks.append(track_idx)
# 遍历匹配的(track, detection)索引对
for row, col in zip(row_indices, col_indices):
track_idx = track_indices[row]
detection_idx = detection_indices[col]
# 如果相应的cost大于阈值max_distance,也视为未匹配成功
if cost_matrix[row, col] > max_distance:
unmatched_tracks.append(track_idx)
unmatched_detections.append(detection_idx)
else:
matches.append((track_idx, detection_idx))
return matches, unmatched_tracks, unmatched_detections
# Associate remaining tracks together with unconfirmed tracks using IOU.
# 将 不确定态的轨迹 和 级联匹配中刚刚没有匹配上的轨迹 组合为 iou匹配候选集合 iou_track_candidates
iou_track_candidates = unconfirmed_tracks + [k for k in unmatched_tracks_a if tracks[k].time_since_update == 1]
# 级联匹配中并非刚刚没有匹配上的轨迹
unmatched_tracks_a = [k for k in unmatched_tracks_a if tracks[k].time_since_update != 1]
# 对级联匹配中还没有匹配成功的目标, 使用匈牙利算法进行IoU匹配
matches_b, unmatched_tracks_b, unmatched_detections = linear_assignment.min_cost_matching(
iou_matching.iou_cost, max_iou_distance, tracks,
detections, iou_track_candidates, unmatched_detections)
本文作者: 崔玉君
版权声明: 转载请注明出处!
Enjoy!
If you like TeXt, don’t forget to give me a star.
PREVIOUSYolov5-DeepSort之级联匹配