Sub topic 2
Intersection over Union: Measuring Box Overlap
Assume you know basic Python and that an object detector produces bounding boxes, class labels, and confidence scores. This chapter uses axis-aligned boxes, which have sides parallel to the image edges.
Representing Bounding Boxes
A bounding box is commonly represented by its corner coordinates:
(x_1, y_1, x_2, y_2)Here, (x1, y1) is the top-left corner and (x2, y2) is the bottom-right corner. The box must satisfy:
x1 < x2y1 < y2
The width is x2 - x1, and the height is y2 - y1.
| Format | Meaning | Example |
|---|---|---|
| Corner format | `(x1, y1, x2, y2)` | `(10, 20, 50, 80)` |
| Width-height format | `(x, y, width, height)` | `(10, 20, 40, 60)` |
| Center format | `(cx, cy, width, height)` | `(30, 50, 40, 60)` |
Some datasets use pixel coordinates. Others normalize coordinates to the range 0 to 1, where (1, 1) represents the image's bottom-right corner. IoU is unchanged when both boxes use the same coordinate system and scale.
The IoU Formula
Intersection over Union, or IoU, measures how much two boxes overlap. One box is usually a detector's prediction, and the other is a ground-truth annotation.
\mathrm{IoU} =
\frac{\text{area of intersection}}
{\text{area of union}}The intersection is the region shared by both boxes. The union is the total region covered by either box:
\text{union} =
\text{area of box A} +
\text{area of box B} -
\text{area of intersection}Subtracting the intersection prevents counting the shared region twice.
IoU ranges from 0 to 1:
0means the boxes do not overlap.1means the boxes are identical.- A larger value means stronger overlap, but it does not necessarily mean perfect alignment.
For example, let box A be (0, 0, 10, 10) and box B be (5, 5, 15, 15).
- The intersection has width
5and height5, so its area is25. - Each box has area
100. - The union area is
100 + 100 - 25 = 175. - The IoU is
25 / 175, or approximately0.1429.
Computing IoU in Python
The following function uses corner-format boxes. The max calls ensure that non-overlapping boxes produce an intersection area of zero.
def iou(a, b):
"""Return IoU for boxes in (x1, y1, x2, y2) format."""
ix1 = max(a[0], b[0])
iy1 = max(a[1], b[1])
ix2 = min(a[2], b[2])
iy2 = min(a[3], b[3])
intersection = max(0.0, ix2 - ix1) * max(0.0, iy2 - iy1)
area_a = (a[2] - a[0]) * (a[3] - a[1])
area_b = (b[2] - b[0]) * (b[3] - b[1])
union = area_a + area_b - intersection
return intersection / union if union > 0 else 0.0
print(iou((0, 0, 10, 10), (5, 5, 15, 15)))
print(iou((0, 0, 10, 10), (0, 0, 10, 10)))
print(iou((0, 0, 10, 10), (20, 20, 30, 30)))The output is approximately:
0.14285714285714285
1.0
0.0IoU as an Evaluation Decision
A detector's box is rarely judged as simply “overlapping” or “not overlapping.” Evaluation uses an IoU threshold. At a threshold of 0.5, a prediction with IoU 0.6 meets the overlap requirement, while one with IoU 0.4 does not.
The threshold is a policy choice:
- A lower threshold is more forgiving of inaccurate localization.
- A higher threshold demands tighter alignment.
- The same prediction can pass at
0.5and fail at0.75.
The prediction's class must also match the ground-truth class. A perfectly aligned box around a cat is not a correct detection for a dog.
Common implementation errors include reversing the corners, forgetting to clamp the intersection width or height at zero, and using different coordinate scales for the two boxes. Pixel-coordinate conventions can also differ between libraries, especially about whether a boundary is inclusive. Use one documented convention consistently.
Exercise
Box A is (10, 10, 30, 30) and box B is (20, 20, 40, 40). What are their intersection area, union area, and IoU?
Show answer
The intersection is 10 × 10 = 100. Each box has area 20 × 20 = 400, so the union is 400 + 400 - 100 = 700. IoU is 100 / 700, approximately 0.1429.