TL;DR
Intersection over Union, usually written IoU, compares two regions by dividing the area they share by the area they cover together. If the two regions are identical the score is 1, and if they do not touch it is 0. Because it is a ratio, it does not depend on how big the regions are, so it can judge a small box and a large box on the same scale.
Builds on: Set Operations · Leads to: Non-Maximum Suppression, Object Detection
Where it's used
Scoring object detectors. A detector such as one that finds cars in street photos outputs a rectangle, called a bounding box, around each object it finds. Benchmarks such as PASCAL VOC count a detection as correct when its box has an IoU of at least 0.5 with a labelled box. COCO reports results averaged over thresholds from 0.5 up to 0.95, which rewards tight boxes.
Removing duplicate detections. A detector often produces many boxes around the same object. Non-Maximum Suppression keeps the highest-scoring box and deletes any other box whose IoU with it is above a set threshold, so IoU decides what counts as a duplicate.
Comparing segmentation masks. When the output is a mask that marks each pixel as object or background, the same ratio is computed on pixel sets: the pixels in both masks divided by the pixels in either mask. Medical and satellite image segmentation report this score routinely.
How it works
Take two rectangles on a grid, each described by its lower-left and upper-right corners. Box runs from to and box runs from to .
- Find the overlap. The two boxes share the strip where is between 1 and 2 and is between 0 and 2. Check: the strip is 1 unit wide and 2 units tall, so the overlap has area 2.
- Find each box's area. Box is and box is also . Check: both are squares of side 2.
- Find the total covered area. Adding the two areas counts the overlap twice, so subtract it once: . Check: this is the area of the union, the region covered by at least one box.
- Divide. IoU is . Check: the score is between 0 and 1, and it is one third here because the boxes share a third of their combined footprint.
flowchart LR A[Box A] --> C[Overlap: area of A and B together] B[Box B] --> C A --> D[Union: area covered by A or B] B --> D C --> E[IoU = overlap divided by union] D --> E
The math
For two regions and (sets of points, pixels or box areas), the Intersection over Union is
where is the set of points in both regions (the intersection), is the set of points in at least one region (the union), and denotes area or the number of pixels. The value lies between 0 and 1.
Each symbol in words: is the shared area, and is the total covered area. The denominator is never smaller than the numerator, which is why the ratio cannot exceed 1.
Step 1 in symbols (the overlap)
For axis-aligned boxes with corners and , the overlap has corners at the larger of the two left edges and bottom edges, and the smaller of the two right edges and top edges:
The overlap area is . The handles boxes that do not touch: the width comes out negative, so it is set to zero.
Step 2 and 3 in symbols (the union)
Inclusion and exclusion gives the area covered by at least one box:
Substituting into the definition gives , which is the form most code uses.
Step 4 in symbols (the ratio)
With , : .
Worked examples
Box covers , box covers .
- Overlap in : from to , width .
- Overlap in : from to , height .
- Overlap area: .
- Each box has area , so the union is .
- IoU: .
Now compare: if were exactly , the overlap would be 1 and the union 1, so IoU is 1. If were , the overlap would be empty, so IoU is 0. The score runs from 0 (no contact) to 1 (identical).
Box covers and box covers .
- Overlap in : from to , width .
- Overlap in : from to , height .
- Overlap area: .
- Area of : . Area of : .
- Union: .
- IoU: .
Interpretation: the squares overlap in a corner, covering half of the side length in each direction, yet the score is low because the union is seven times the overlap. IoU penalises both the missing part of the box and the extra part.
A detector finds a dog and outputs a box with corners and , in pixels. The labelled box has corners and . A common rule counts a detection as correct when IoU is at least 0.5.
- Box sizes: is pixels. is pixels.
- Overlap width: from to , so pixels.
- Overlap height: from to , so pixels.
- Overlap area: .
- Union: .
- IoU: .
Since , the detection is counted as correct.
Interpretation: the box is not perfect, since it sticks out past the labelled box on the left and bottom and falls short of it on the right and top, but it is close enough to count. A stricter benchmark that demands 0.75 would reject it.
Try it: the detector instead outputs box with corners and . Using the same ground truth and the 0.5 rule, is it correct?
Answer: is pixels. Overlap width is from to , so pixels; overlap height is from to , so pixels. Overlap area . Union . IoU , which is at least 0.5, so it is correct.
Common mistakes and tradeoffs
It is tempting to think IoU measures what fraction of one box is covered by the other. In fact it divides by the union, so it penalises both sides of a mismatch. A small box fully inside a large one has IoU equal to the small area divided by the large area, which can be low even though the small box is entirely inside the large one.
It is tempting to think that two boxes with IoU 0 are equally far from matching. In fact IoU is 0 for every pair of boxes that do not touch, however far apart they are. This is why training a detector directly on IoU is awkward: a box that misses completely gets no gradient to move it closer. Practical systems use an extra loss on box coordinates and treat IoU as the evaluation score or as a loss only once boxes overlap.
It is tempting to think a single threshold, such as 0.5, is always right. In fact the threshold is a choice. Lower thresholds accept loose boxes, and higher ones demand precise localisation, so a result is meaningful only with its threshold stated.
When to use it: for comparing any two regions that can be measured as sets, such as boxes, masks, or sets of tokens, when the two should be judged on a scale that does not depend on size. When not to use it: when the regions are very different in shape and what matters is distance rather than overlap, or when you need to rank an unmatched prediction against a missing one. Alternatives include the Jaccard Index, which is the same ratio defined for any finite sets, and distance-based scores that still give a signal when boxes do not overlap.
History
In 1901 the botanist Paul Jaccard published a comparison of plant species across Alpine regions and used the ratio of shared species to all species found in either region. The same ratio, under the name Jaccard index, spread through ecology and then into information retrieval, where it compares sets of documents or terms.
In computer vision the ratio became the standard match criterion for object detection. The PASCAL VOC challenge, which ran from 2005 and shaped much of early detection research, scored detections by their overlap with labelled boxes using this measure. Later benchmarks kept the idea and made the threshold stricter or averaged over several thresholds.
The reason it caught on is simple: it needs no tuning to the size of the objects, it is between 0 and 1, and it is easy to compute for both boxes and masks.
Recap
- IoU is the area of the overlap divided by the area of the union of two regions.
- It is 1 for identical regions, 0 for regions that do not touch, and does not depend on size.
- For boxes, the overlap comes from the larger left and bottom edges and the smaller right and top edges, with zero when there is no overlap.
- Detection benchmarks use a threshold, often 0.5, to decide which boxes are correct.
- Read next: Non-Maximum Suppression for removing duplicate boxes with IoU, and Object Detection for the full task that uses both.
Check yourself
QuestionRecall: what does an IoU of 1 mean, and what does an IoU of 0 mean?Show answer
An IoU of 1 means the two regions are identical, since the overlap equals the union. An IoU of 0 means the regions share no area, so their overlap is empty.
QuestionComputation: box A covers [0, 3] by [0, 3] and box B covers [1, 4] by [0, 3]. What is their IoU? Give two decimal places.Show answer
Overlap width: from 1 to 3, so 2. Overlap height: from 0 to 3, so 3. Overlap area: . Each box has area . Union: . IoU: .
QuestionWhy: why is IoU a poor training signal when two boxes do not overlap at all?Show answer
IoU is exactly 0 for any pair of boxes that do not touch, however far apart they are. A value that stays flat at zero gives no information about which direction to move a box, so the gradient is zero and the box does not learn. That is why a separate loss on box coordinates is usually added.
QuestionApplied: a detector finds a cat with a box that has IoU 0.45 with the labelled box. A benchmark uses the 0.5 rule. Is the detection counted, and what could you change to improve it?Show answer
It is not counted, because 0.45 is below 0.5. The box needs to be tighter, so the model's box regression should be improved, for example by training with more precise box targets or by adding a loss on coordinates. Lowering the threshold would count it but would make the benchmark easier, which hides the real problem.
Sources
- Wikipedia, "Jaccard index": https://en.wikipedia.org/wiki/Jaccard_index
- Everingham et al., "The PASCAL Visual Object Classes (VOC) Challenge", International Journal of Computer Vision, 2010: https://doi.org/10.1007/s11263-009-0275-4