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 and :
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.
- 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.
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.
# 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.
Related concepts
Learn how normalized box coordinates, L1 loss, IoU, and Generalized IoU guide object localization, including disjoint boxes and containment limits.
Convert between xyxy, xywh and center-size boxes, normalize coordinates correctly, and avoid mistakes when resizing, clipping or flipping detection labels.
Understand object detection from image features to boxes, classes and scores. Compare one-stage and two-stage pipelines, training and duplicate handling.
Compare anchor-based vs anchor-free object detection: Faster R-CNN and RetinaNet anchors vs FCOS and CenterNet point-based methods.
Learn how DETR assigns unordered object targets to prediction slots, why greedy choices can fail, and how matching differs from training loss and evaluation.
Follow a query from a spatial reference through learned offsets, bilinear feature sampling and weighted aggregation, with exact non-square coordinate arithmetic.
