Hungarian Algorithm for MOT
Implement the Hungarian Algorithm for solving the Multi-Object Tracking (MOT) association problem. The goal is to find the optimal assignment between detections and tracks, given a cost matrix representing the association costs between them.
The Hungarian Algorithm is a combinatorial optimization technique used to solve the Assignment Problem, which is a fundamental problem in computer science and operations research. In the context of MOT, the assignment problem involves finding the optimal mapping between a set of detections and a set of tracks, such that the total association cost is minimized. This is crucial in object tracking, as it enables the accurate association of detections across frames.
To solve this problem, the following steps are involved:
- Construct a cost matrix, where each entry represents the cost of assigning the detection to the track.
- Apply the Hungarian Algorithm to find the optimal assignment. The key formula for the assignment problem is:
where if detection is assigned to track and otherwise, with each detection and each track used at most once.
Return the assigned pairs as a list of (detection_index, track_index) tuples, ordered by detection index; when the extra detections or tracks are left unassigned. An empty matrix returns an empty list.
This technique is widely used in object tracking applications, such as surveillance and autonomous vehicles.
Example
Cost matrix
Assignment pairs
cost_matrixis an grid of non-negative costs: row is a detection, column a track, and is how poorly detection fits track .- Take the test matrix . Each detection needs a different track, so only two complete matchings exist.
- Straight: . Crossed: .
- The minimum is , so the assignment pairs are and : two pairs, matching the test's expected
2. - Notice that taking each row's cheapest entry can fail: for it gives , while the crossed matching costs . The objective is the whole sum.
- With 2 detections and 3 tracks you still get pairs; one track stays unmatched.
Constraints
- Input cost matrix: 2D numpy array of shape (n, m) where n is the number of detections and m is the number of tracks, with float values in [0, inf) representing association costs
- Valid ranges: n and m are positive integers, cost matrix values are non-negative
- Output format: List of tuples, where each tuple contains a detection index and a track index, representing the optimal assignment pairs
- Special conditions: The input cost matrix is not necessarily square (i.e., n may not equal m), and the algorithm should handle empty input matrices
- Input validation: The input cost matrix should not contain NaN or infinity values, except for the special case where the matrix is empty