v9.1.1
Menu

Intersection over Union (IoU) for Tracking

mediumPROObject TrackingTrackingCV: Motion Estimation

Implement IoU computation for bounding box overlap in object tracking. This task involves calculating the overlap between two bounding boxes, a crucial step in evaluating the performance of object tracking algorithms.

The concept of Intersection over Union (IoU) is fundamental in computer vision, particularly in object tracking and detection. It measures the overlap between two bounding boxes, AA and BB, and is defined as the ratio of the intersection area to the union area. The intersection area represents the common region between the two boxes, while the union area represents the total area covered by both boxes.

To compute IoU, we need to follow these steps:

  1. Calculate the intersection area by finding the overlapping region between the two boxes.
  2. Calculate the individual areas of the two boxes.
  3. Calculate the union area by adding the individual areas and subtracting the intersection area.
IoU=∣A∩B∣∣A∪B∣=Intersection AreaUnion AreaIoU = \frac{|A \cap B|}{|A \cup B|} = \frac{\text{Intersection Area}}{\text{Union Area}}

This technique is widely used in object tracking evaluation metrics, such as the MOTA metric.

Example

Input
box1 = [0, 0, 10, 10]
box2 = [5, 5, 15, 15]
Output
0.143
Reasoning
  • Inputs: each box is [x1,y1,x2,y2][x_1, y_1, x_2, y_2], its top-left corner (x1,y1)(x_1, y_1) and bottom-right corner (x2,y2)(x_2, y_2). Here box1 covers x,y∈[0,10]x, y \in [0, 10] and box2 covers x,y∈[5,15]x, y \in [5, 15].
  • Box areas: width times height, so box1 has (10−0)(10−0)=100(10-0)(10-0) = 100 and box2 has (15−5)(15−5)=100(15-5)(15-5) = 100.
  • Intersection: the region inside both boxes runs from x=5x = 5 to x=10x = 10 and from y=5y = 5 to y=10y = 10, a 5×55 \times 5 square with area 2525.
  • Union: adding the two areas counts that shared square twice, so ∣A∪B∣=100+100−25=175|A \cup B| = 100 + 100 - 25 = 175.
  • Output: IoU=25175=17≈0.142857\text{IoU} = \frac{25}{175} = \frac{1}{7} \approx 0.142857, shown as 0.143. The function returns that unrounded ratio as a single float in [0,1][0, 1].
  • Notice that shifting a box diagonally by half its side length in both xx and yy leaves an IoU of only 17\frac{1}{7}, not 12\frac{1}{2}: IoU punishes misalignment hard. Identical boxes score 11; boxes that share no area score 00.

Constraints

  • box1, box2: Bounding boxes as [x1, y1, x2, y2]
  • Return: IoU score (0-1)

Learn this in

More from CV: Motion Estimation

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.