Skip to main content

Intersection over Union (IoU)

Calculate box intersection and union, explore how position and size change IoU, and distinguish overlap used for evaluation, NMS and localization losses.

Two boxes can overlap without describing the same region well. Intersection over Union measures how much area they share relative to all the area either one covers. It gives a dimensionless score that accounts for both missed coverage and excess coverage.

Build the ratio from areas

For regions AA and BB:

IoU⁡(A,B)=∣A∩B∣∣A∪B∣=∣A∩B∣∣A∣+∣B∣−∣A∩B∣\operatorname{IoU}(A,B) = \frac{|A\cap B|}{|A\cup B|} = \frac{|A\cap B|}{|A|+|B|-|A\cap B|}

Subtract the intersection once in the denominator because adding the two areas counts it twice. IoU is 1 for identical, nonempty regions and 0 when they have no shared area.

Move box B until it matches A, then change only its width. A box can fully contain another and still have low IoU because its excess area increases the union.

Overlap divided by combined area
A · fixedB · adjustable
80
70
100
0.50
Area A / B
8000 / 8000
Intersection
5600
Union
10400
IoU
0.538
IoU ≥ 0.50
Yes

Calculated geometry in square pixels. Passing an IoU threshold alone does not make a true positive: evaluation must also check the class and one-to-one assignment.

The example uses continuous coordinates. Its areas are in square pixels; it does not add one to the width or height. The ratio itself has no units.

Calculate an overlap by hand

Suppose both boxes are 100 × 80. Box A starts at (60, 60) and B starts at (80, 70). Their shared horizontal span is 80 and their shared vertical span is 70, giving intersection 5600.

∣A∪B∣=8000+8000−5600=10400|A\cup B| = 8000 + 8000 - 5600 = 10400
IoU⁡=560010400≈0.538\operatorname{IoU} = \frac{5600}{10400} \approx 0.538

For corner-format boxes, the intersection starts at the larger left/top coordinates and ends at the smaller right/bottom coordinates. Clamp each resulting extent at zero. Otherwise, multiplying two negative extents could incorrectly give a positive area for separated boxes.

python
# Continuous xyxy boxes; each input has nonnegative width and height.
def box_iou(a, b):
    width = max(0, min(a[2], b[2]) - max(a[0], b[0]))
    height = max(0, min(a[3], b[3]) - max(a[1], b[1]))
    intersection = width * height
    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

Returning zero for a zero-area union is an explicit convention in this example. Invalid boxes should normally be rejected or cleaned before evaluation. For batches of boxes, use a vectorized operation that returns pairwise overlaps. Torchvision box IoU

The same overlap serves different decisions

During evaluation, a prediction may qualify to match a ground-truth object if its class agrees and IoU passes a threshold. Assignment rules then decide which prediction gets that match; repeated boxes should not all receive credit for one object.

During NMS, the comparison is between predictions. High overlap may cause a lower-scoring candidate to be suppressed. There is no ground truth available to decide which one is correct.

For localization training, a loss such as 1 − IoU rewards overlap. Ordinary IoU is flat at zero for disjoint boxes, which motivates alternatives that also account for enclosure or distance. That training issue is separate from choosing an evaluation threshold.

Interpret the score in context

The same coordinate error can have different effects at different scales. Shifting a 10 × 10 box by 5 pixels horizontally gives IoU one third; shifting a 100 × 100 box by the same amount gives about 0.905. Small objects are especially sensitive to localization error.

IoU also ignores class correctness and score calibration. Continue to precision, recall, AP, and mAP to turn geometric overlap into an evaluation of the complete prediction list.

If you found this explanation helpful, consider sharing it with others.

Mastodon