The IoU formula
For two boxes A and B:
IoU = |A ∩ B| ÷ |A ∪ B| = intersection ÷ (area A + area B − intersection)
The intersection is subtracted once in the denominator because adding both areas counts the overlap twice. IoU is the same quantity as the Jaccard index of two sets, applied to pixels. It does not care which box is the prediction and which is the ground truth, and it does not depend on the image size, so a 0.6 means the same fit for a small and a large object.
Worked example: four predicted boxes against their ground truth
Rows 2 to 9 are the box corners in pixels, one pair per column. Rows 10 to 16 calculate the IoU, rows 17 to 19 turn it into decisions and Dice. Row 20 shows the bug that the MAX(0, …) clamp prevents.
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Box (pixels) | Pair 1 | Pair 2 | Pair 3 | Pair 4 |
| 2 | Ground truth x1 | 100 | 50 | 400 | 700 |
| 3 | Ground truth y1 | 80 | 60 | 150 | 100 |
| 4 | Ground truth x2 | 300 | 210 | 600 | 800 |
| 5 | Ground truth y2 | 280 | 260 | 450 | 200 |
| 6 | Predicted x1 | 120 | 110 | 430 | 820 |
| 7 | Predicted y1 | 100 | 90 | 190 | 210 |
| 8 | Predicted x2 | 320 | 260 | 570 | 900 |
| 9 | Predicted y2 | 300 | 280 | 410 | 300 |
| 10 | Overlap width | 180 | 100 | 140 | 0 |
| 11 | Overlap height | 180 | 170 | 220 | 0 |
| 12 | Intersection area | 32,400 | 17,000 | 30,800 | 0 |
| 13 | Ground-truth area | 40,000 | 32,000 | 60,000 | 10,000 |
| 14 | Predicted area | 40,000 | 28,500 | 30,800 | 7,200 |
| 15 | Union = A + B − intersection | 47,600 | 43,500 | 60,000 | 17,200 |
| 16 | IoU | 0.681 | 0.391 | 0.513 | 0.000 |
| 17 | Hit at IoU ≥ 0.5? | TP | FP | TP | FP |
| 18 | Hit at IoU ≥ 0.75? | FP | FP | FP | FP |
| 19 | Dice = 2 × I ÷ (A + B) | 0.810 | 0.562 | 0.678 | 0.000 |
| 20 | Bug: overlap without MAX(0, …) | 32,400 | 17,000 | 30,800 | 200 |
- B10
=MAX(0,MIN(B4,B8)-MAX(B2,B6))Overlap width, clamped at 0 - B15
=B13+B14-B12Union counts the overlap once - B16
=B12/B15IoU - B19
=2*B12/(B13+B14)Dice coefficient
Corner format, y measured downwards from the top of the image as in most image libraries. The formulas work the same with y upwards.
Step 1: the overlap box with MAX and MIN
With boxes in corner format (x1, y1) top left and (x2, y2) bottom right, the overlap is itself a box. Its left edge is the larger of the two left edges, its right edge the smaller of the two right edges:
width = MIN(x2a, x2b) − MAX(x1a, x1b), and the same with y for the height.
In pair 1 that is MIN(300, 320) − MAX(100, 120) = 180 pixels wide and 180 high, an intersection of 32,400 square pixels. If the boxes do not overlap, the difference is negative. Wrap each side in MAX(0, …): in pair 4 the width is −20 and the height −10, and without the clamp the two negatives multiply to a positive "intersection" of 200, a classic bug shown in the last row of the example.
Step 2: union and IoU
Each box area is width × height: 200 × 200 = 40,000 for both boxes of pair 1. The union is 40,000 + 40,000 − 32,400 = 47,600, and IoU = 32,400 ÷ 47,600 = 0.681. Pair 2 shows how fast IoU falls with an offset: shifted by 60 pixels to the right and 30 down, with a slightly smaller box, the intersection is 17,000, the union 43,500 and the IoU 0.391. Pair 3 is a tight prediction fully inside a large ground-truth box. The intersection equals the smaller box, 30,800, the union equals the larger one, 60,000, and IoU is only 0.513, because half of the object is missed.
What counts as a good IoU
Detection benchmarks turn IoU into a yes/no decision. A prediction is a true positive if its IoU with a still unmatched ground-truth box is at least a threshold, otherwise a false positive. The classic Pascal VOC threshold is 0.5: pairs 1 and 3 pass, pair 2 fails. At the stricter 0.75 none of the four pairs passes, not even pair 1 with 0.681. COCO averages the result over ten thresholds from 0.5 to 0.95, written mAP@[.5:.95], which rewards tight boxes. As a rough guide, IoU above 0.5 is a usable localisation and above 0.7 to 0.8 a good one, but the right threshold depends on the task: counting cars in a parking lot needs less precision than a robot gripping a box.
Other box formats: centre and YOLO
Many models output boxes as centre, width and height, and YOLO labels normalise them by the image size. Convert to corners before calculating IoU: x1 = cx − w ÷ 2, x2 = cx + w ÷ 2, and the same for y. For normalised YOLO values, multiply by the image width and height first, or compute IoU on the normalised values directly, as long as both boxes use the same scale. A format mismatch, one box in corners and one in centre format, is the most common reason for IoU values that look random.
IoU vs Dice for segmentation masks
For segmentation, the same idea is counted in pixels: IoU = TP ÷ (TP + FP + FN), where TP are the pixels both masks share. The Dice coefficient (F1 score) is 2 × intersection ÷ (area A + area B). The two are linked, Dice = 2 × IoU ÷ (1 + IoU), so Dice is always at least as high as IoU: 0.810 against 0.681 for pair 1. Neither is better, but never compare a Dice score from one paper with an IoU from another. In object detection, IoU is the standard. For the precision, recall and F1 that follow from the TP and FP counts, see the confusion matrix, precision and recall guide.
Common mistakes
- Adding the two areas as the union. A + B counts the overlap twice. Subtract the intersection once, or the IoU is too low.
- No MAX(0, …) clamp. Two boxes apart in both directions give two negative sides, and their product is a positive fake intersection.
- Mixing box formats. One box in corners and one in centre-width-height format gives meaningless overlaps. Convert both to corners first.
- Mixing pixel and normalised coordinates. A YOLO box in 0 to 1 against a ground truth in pixels never overlaps. Use the same scale for both.
- Matching one ground truth twice. In evaluation, each ground-truth box can be matched by only one prediction, the one with the highest confidence. A second prediction on the same object is a false positive, even with a high IoU.
- Comparing IoU with Dice. Dice is always higher for the same masks. Convert with Dice = 2 × IoU ÷ (1 + IoU) before you compare numbers from different sources.
Frequently asked questions
How do you calculate intersection over union?
Find the overlap box with MAX of the two left and top edges and MIN of the two right and bottom edges, clamp negative sides at 0 and multiply width by height. Then IoU = intersection ÷ (area A + area B − intersection).
What is a good IoU score?
In object detection a prediction usually counts as correct from IoU 0.5, the Pascal VOC rule. Stricter benchmarks use 0.75 or average over 0.5 to 0.95 as in COCO. Above about 0.7 is generally a tight box.
Is IoU the same as the Jaccard index?
Yes. IoU is the Jaccard index applied to the pixels or areas of two regions: the size of the intersection divided by the size of the union.
What is the difference between IoU and Dice?
Both measure overlap. Dice = 2 × intersection ÷ (A + B), IoU = intersection ÷ union. Dice = 2 × IoU ÷ (1 + IoU), so Dice is always at least as high. Detection usually reports IoU, medical segmentation often Dice.