Search

Search papers and lessons

Intersection over Union

Level 2 · 10 min read

Intersection over Union (IoU) scores how much two regions overlap: the shared area divided by the total area they cover together, giving 1 for a perfect match and 0 for no overlap.

On this page

  • Colour = topic mix
  • Computer vision
  • Discrete math
  • ReadReady to readNot yet

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 AA runs from (0,0)(0,0) to (2,2)(2,2) and box BB runs from (1,0)(1,0) to (3,2)(3,2).

  1. Find the overlap. The two boxes share the strip where xx is between 1 and 2 and yy is between 0 and 2. Check: the strip is 1 unit wide and 2 units tall, so the overlap has area 2.
  2. Find each box's area. Box AA is 2×2=42 \times 2 = 4 and box BB is also 2×2=42 \times 2 = 4. Check: both are squares of side 2.
  3. Find the total covered area. Adding the two areas counts the overlap twice, so subtract it once: 4+4−2=64 + 4 - 2 = 6. Check: this is the area of the union, the region covered by at least one box.
  4. Divide. IoU is 2/6≈0.3332 / 6 \approx 0.333. 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

Definition: Intersection over Union

For two regions AA and BB (sets of points, pixels or box areas), the Intersection over Union is

IoU⁡(A,B)=∣A∩B∣∣A∪B∣,\operatorname{IoU}(A, B) = \frac{\lvert A \cap B \rvert}{\lvert A \cup B \rvert},

where A∩BA \cap B is the set of points in both regions (the intersection), A∪BA \cup B is the set of points in at least one region (the union), and ∣⋅∣\lvert \cdot \rvert denotes area or the number of pixels. The value lies between 0 and 1.

Each symbol in words: ∣A∩B∣\lvert A \cap B \rvert is the shared area, and ∣A∪B∣\lvert A \cup B \rvert 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 (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2), 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:

w=max⁡(0, min⁡(x2A,x2B)−max⁡(x1A,x1B)),h=max⁡(0, min⁡(y2A,y2B)−max⁡(y1A,y1B)).w = \max(0,\ \min(x_2^A, x_2^B) - \max(x_1^A, x_1^B)), \qquad h = \max(0,\ \min(y_2^A, y_2^B) - \max(y_1^A, y_1^B)).

The overlap area is whw h. The max⁡(0,⋅)\max(0, \cdot) 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:

∣A∪B∣=∣A∣+∣B∣−∣A∩B∣.\lvert A \cup B \rvert = \lvert A \rvert + \lvert B \rvert - \lvert A \cap B \rvert.

Substituting into the definition gives IoU⁡=wh∣A∣+∣B∣−wh\operatorname{IoU} = \dfrac{wh}{\lvert A \rvert + \lvert B \rvert - wh}, which is the form most code uses.

Step 4 in symbols (the ratio)

With wh=2w h = 2, ∣A∣=∣B∣=4\lvert A \rvert = \lvert B \rvert = 4: IoU⁡=24+4−2=26=13\operatorname{IoU} = \dfrac{2}{4 + 4 - 2} = \dfrac{2}{6} = \dfrac{1}{3}.

Worked examples

Example
Example 1: two unit squares (tiny)

Box AA covers [0,1]×[0,1][0, 1] \times [0, 1], box BB covers [0.5,1.5]×[0,1][0.5, 1.5] \times [0, 1].

  1. Overlap in xx: from max⁡(0,0.5)=0.5\max(0, 0.5) = 0.5 to min⁡(1,1.5)=1\min(1, 1.5) = 1, width 0.50.5.
  2. Overlap in yy: from 00 to 11, height 11.
  3. Overlap area: 0.5×1=0.50.5 \times 1 = 0.5.
  4. Each box has area 11, so the union is 1+1−0.5=1.51 + 1 - 0.5 = 1.5.
  5. IoU: 0.5/1.5=1/3≈0.3330.5 / 1.5 = 1/3 \approx 0.333.

Now compare: if BB were exactly AA, the overlap would be 1 and the union 1, so IoU is 1. If BB were [2,3]×[0,1][2, 3] \times [0, 1], the overlap would be empty, so IoU is 0. The score runs from 0 (no contact) to 1 (identical).

Example
Example 2: two overlapping squares (standard)

Box AA covers [0,4]×[0,4][0, 4] \times [0, 4] and box BB covers [2,6]×[2,6][2, 6] \times [2, 6].

  1. Overlap in xx: from max⁡(0,2)=2\max(0, 2) = 2 to min⁡(4,6)=4\min(4, 6) = 4, width 22.
  2. Overlap in yy: from max⁡(0,2)=2\max(0, 2) = 2 to min⁡(4,6)=4\min(4, 6) = 4, height 22.
  3. Overlap area: 2×2=42 \times 2 = 4.
  4. Area of AA: 4×4=164 \times 4 = 16. Area of BB: 4×4=164 \times 4 = 16.
  5. Union: 16+16−4=2816 + 16 - 4 = 28.
  6. IoU: 4/28=1/7≈0.1434 / 28 = 1/7 \approx 0.143.

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.

Example
Example 3: a detection that just passes (realistic)

A detector finds a dog and outputs a box PP with corners (100,50)(100, 50) and (300,250)(300, 250), in pixels. The labelled box GG has corners (120,80)(120, 80) and (340,280)(340, 280). A common rule counts a detection as correct when IoU is at least 0.5.

  1. Box sizes: PP is 200×200=40000200 \times 200 = 40000 pixels. GG is 220×200=44000220 \times 200 = 44000 pixels.
  2. Overlap width: from max⁡(100,120)=120\max(100, 120) = 120 to min⁡(300,340)=300\min(300, 340) = 300, so 180180 pixels.
  3. Overlap height: from max⁡(50,80)=80\max(50, 80) = 80 to min⁡(250,280)=250\min(250, 280) = 250, so 170170 pixels.
  4. Overlap area: 180×170=30600180 \times 170 = 30600.
  5. Union: 40000+44000−30600=5340040000 + 44000 - 30600 = 53400.
  6. IoU: 30600/53400≈0.57330600 / 53400 \approx 0.573.

Since 0.573≥0.50.573 \ge 0.5, 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 QQ with corners (150,100)(150, 100) and (350,300)(350, 300). Using the same ground truth GG and the 0.5 rule, is it correct?

Answer: QQ is 200×200=40000200 \times 200 = 40000 pixels. Overlap width is from max⁡(150,120)=150\max(150, 120) = 150 to min⁡(350,340)=340\min(350, 340) = 340, so 190190 pixels; overlap height is from max⁡(100,80)=100\max(100, 80) = 100 to min⁡(300,280)=280\min(300, 280) = 280, so 180180 pixels. Overlap area 190×180=34200190 \times 180 = 34200. Union 40000+44000−34200=4980040000 + 44000 - 34200 = 49800. IoU =34200/49800≈0.687= 34200 / 49800 \approx 0.687, 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: 2×3=62 \times 3 = 6. Each box has area 3×3=93 \times 3 = 9. Union: 9+9−6=129 + 9 - 6 = 12. IoU: 6/12=0.506 / 12 = 0.50.

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