Sub topic 5
Matching Detections to Tracks with the Hungarian Algorithm
Prerequisites and the matching problem
Assume you know basic Python, NumPy, object-detection bounding boxes, and matrix notation. You should also know that a tracker has existing tracks from earlier frames and receives new detections from the current frame.
The tracker must decide which detection belongs to which track. This is called data association. A good association preserves object identities over time.
Each possible track–detection pair receives a similarity score. Intersection over union (IoU) is a common score:
\text{IoU} = \frac{\text{area of intersection}}{\text{area of union}}IoU is 1 when two boxes are identical and 0 when they do not overlap. The tracker can turn similarity into a cost:
\text{cost} = 1 - \text{IoU}The resulting matrix has one row per track and one column per detection.
| Pair | IoU | Cost |
|---|---|---|
| Track 1, Detection 1 | 0.80 | 0.20 |
| Track 1, Detection 2 | 0.10 | 0.90 |
| Track 2, Detection 1 | 0.25 | 0.75 |
The Hungarian algorithm
The Hungarian algorithm finds a one-to-one assignment that minimizes the total cost. One detection cannot update two tracks, and one track cannot claim two detections.
The algorithm supports rectangular matrices. Therefore, the number of tracks and detections does not need to be equal. After assignment, a tracker applies a gating threshold. Gating rejects a pair when its IoU is too low to be credible.
import numpy as np
from scipy.optimize import linear_sum_assignment
def iou_matrix(track_boxes, detection_boxes):
"""Return pairwise IoU for boxes [x1, y1, x2, y2]."""
tracks = np.asarray(track_boxes, dtype=float).reshape(-1, 1, 4)
detections = np.asarray(detection_boxes, dtype=float).reshape(1, -1, 4)
left = np.maximum(tracks[..., 0], detections[..., 0])
top = np.maximum(tracks[..., 1], detections[..., 1])
right = np.minimum(tracks[..., 2], detections[..., 2])
bottom = np.minimum(tracks[..., 3], detections[..., 3])
intersection = (
np.clip(right - left, 0, None)
* np.clip(bottom - top, 0, None)
)
track_area = (tracks[..., 2] - tracks[..., 0]) * (
tracks[..., 3] - tracks[..., 1]
)
detection_area = (detections[..., 2] - detections[..., 0]) * (
detections[..., 3] - detections[..., 1]
)
union = track_area + detection_area - intersection
return intersection / np.maximum(union, 1e-9)
def associate(track_boxes, detection_boxes, iou_threshold=0.3):
if not track_boxes or not detection_boxes:
return [], list(range(len(track_boxes))), list(range(len(detection_boxes)))
ious = iou_matrix(track_boxes, detection_boxes)
rows, columns = linear_sum_assignment(1.0 - ious)
matches = [
(track_index, detection_index)
for track_index, detection_index in zip(rows, columns)
if ious[track_index, detection_index] >= iou_threshold
]
matched_tracks = {row for row, _ in matches}
matched_detections = {column for _, column in matches}
unmatched_tracks = [
i for i in range(len(track_boxes)) if i not in matched_tracks
]
unmatched_detections = [
j for j in range(len(detection_boxes)) if j not in matched_detections
]
return matches, unmatched_tracks, unmatched_detections
tracks = [[10, 10, 50, 50], [100, 100, 150, 160]]
detections = [[102, 104, 152, 162], [12, 11, 52, 52], [300, 300, 340, 340]]
print(associate(tracks, detections))Association flow
Common errors
- Minimizing IoU instead of minimizing
1 - IoU. - Allowing one detection to update multiple tracks.
- Forgetting unmatched detections, which may start new tracks.
- Using a threshold so high that ordinary motion creates many missed matches.
- Computing IoU with inconsistent box formats, such as mixing
[x, y, width, height]with[x1, y1, x2, y2].
Exercise
If two tracks and one detection are assigned by the Hungarian algorithm, how many tracks can receive that detection, and what happens to the other track?
Show answer
Only one track can receive the detection. The other track is unmatched and can be kept temporarily using its motion prediction.