Tabellab
Data, tech, science & health

Intersection over union (IoU): formula and worked example

Intersection over union (IoU) measures how well a predicted bounding box matches the ground-truth box: the area where they overlap divided by the area they cover together, IoU = intersection ÷ (area A + area B − intersection). It runs from 0, no overlap, to 1, a perfect fit. For two boxes in corner format, the overlap width is MAX(0, MIN(x2a, x2b) − MAX(x1a, x1b)), and the same for the height. In the example below, a prediction shifted by 20 pixels in both directions against a 200 × 200 box still scores IoU = 0.681, a hit at the usual 0.5 threshold, while a box shifted 60 pixels to the right drops to 0.391 and counts as a false positive. A box fully inside the ground truth reaches only 0.513.

Updated 21 Oct 2026 6 min read By Tabellab team

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.

Worked example: four predicted boxes against their ground truth
ABCDE
1Box (pixels)Pair 1Pair 2Pair 3Pair 4
2Ground truth x110050400700
3Ground truth y18060150100
4Ground truth x2300210600800
5Ground truth y2280260450200
6Predicted x1120110430820
7Predicted y110090190210
8Predicted x2320260570900
9Predicted y2300280410300
10Overlap width1801001400
11Overlap height1801702200
12Intersection area32,40017,00030,8000
13Ground-truth area40,00032,00060,00010,000
14Predicted area40,00028,50030,8007,200
15Union = A + B − intersection47,60043,50060,00017,200
16IoU0.6810.3910.5130.000
17Hit at IoU ≥ 0.5?TPFPTPFP
18Hit at IoU ≥ 0.75?FPFPFPFP
19Dice = 2 × I ÷ (A + B)0.8100.5620.6780.000
20Bug: overlap without MAX(0, …)32,40017,00030,800200
Key formulas
  • 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.

Practise it in a lab

Related guides

Reading is step one. Doing is step two.

Every guide links to a lab where you build the same calculation in an online spreadsheet and get instant feedback.

Open the lab