Sub topic 3
Measuring Overlap with IoU
Prerequisites and the core question
Assume you know how object detectors represent objects with bounding boxes and that you can read basic NumPy code. This chapter uses boxes in the form [x1, y1, x2, y2], where (x1, y1) is the top-left corner and (x2, y2) is the bottom-right corner.
Multi-object tracking compares a new detection with an existing track. The tracker needs a similarity measure that answers:
> How much do these two boxes cover the same image area?
Intersection over Union, usually called IoU, answers this question. It divides the area shared by two boxes by the area covered by either box:
IoU = intersection area / union area
IoU ranges from 0 to 1.
0means the boxes do not overlap.1means the boxes are identical.- A larger value usually indicates a better match.
Calculating intersection and union
For two boxes, first find their intersection:
- The intersection's left edge is
max(x1 values). - Its top edge is
max(y1 values). - Its right edge is
min(x2 values). - Its bottom edge is
min(y2 values).
If the right edge is left of the left edge, or the bottom edge is above the top edge, the boxes do not overlap. Clipping the width and height to zero handles this case.
The union is the total area of both boxes minus the intersection area. Subtracting the intersection prevents counting shared pixels twice.
import numpy as np
def iou(box_a, box_b):
"""Return IoU for boxes [x1, y1, x2, y2]."""
x1 = max(box_a[0], box_b[0])
y1 = max(box_a[1], box_b[1])
x2 = min(box_a[2], box_b[2])
y2 = min(box_a[3], box_b[3])
intersection = max(0.0, x2 - x1) * max(0.0, y2 - y1)
area_a = max(0.0, box_a[2] - box_a[0]) * max(0.0, box_a[3] - box_a[1])
area_b = max(0.0, box_b[2] - box_b[0]) * max(0.0, box_b[3] - box_b[1])
union = area_a + area_b - intersection
return intersection / union if union > 0 else 0.0
first = [10, 10, 50, 50]
second = [30, 20, 70, 60]
print(iou(first, second))The code returns approximately 0.325. The boxes overlap, but neither contains most of the other box.
From one score to an IoU matrix
A tracker usually has several tracks and several detections. It computes every track-detection pair and stores the results in a matrix.
For example, with three tracks and two detections, the matrix has shape (3, 2). Entry [i, j] is the IoU between track i and detection j. A later association step can use this matrix to select compatible pairs.
Thresholds and limitations
A tracker often rejects a candidate pair when its IoU is below a threshold such as 0.3. This is called gating. Gating prevents a track from jumping to a distant detection.
IoU is useful but incomplete:
- A fast-moving object can have low IoU even when it is the correct object.
- Two nearby objects can have similar boxes and cause an identity error.
- Camera movement can reduce overlap for every object.
- IoU does not describe color, texture, or identity.
The threshold depends on detector quality, frame rate, object size, and camera motion. A fixed threshold is a starting point, not a universal rule.
Exercise
Two boxes are [0, 0, 10, 10] and [5, 5, 15, 15]. What is their IoU?
Show answer
The intersection area is 25, each box has area 100, and the union is 100 + 100 - 25 = 175. The IoU is 25 / 175, or approximately 0.143.