Intersection over Union (IoU)
You are given two axis-aligned bounding boxes and need to calculate their Intersection over Union (IoU), the standard metric for evaluating object detection accuracy.
Bounding boxes are represented as [x1, y1, x2, y2] where (x1, y1) is the top-left corner and (x2, y2) is the bottom-right corner.
IoU=AreaunionβAreaintersectionββ=AreaAβ+AreaBββAreaintersectionβAreaintersectionββ
The algorithm:
- Find the intersection rectangle (if any):
- x1_inter = max(x1_A, x1_B)
- y1_inter = max(y1_A, y1_B)
- x2_inter = min(x2_A, x2_B)
- y2_inter = min(y2_A, y2_B)
- Compute intersection area (0 if boxes don't overlap)
- Compute union = Area_A + Area_B - intersection
- IoU = intersection / union
IoU ranges from 0 (no overlap) to 1 (perfect overlap).
Example:
box1 = [0, 0, 10, 10] box2 = [5, 5, 15, 15]
0.1429
-
Find intersection rectangle:
- x1_inter = max(0, 5) = 5
- y1_inter = max(0, 5) = 5
- x2_inter = min(10, 15) = 10
- y2_inter = min(10, 15) = 10
- Intersection = [5, 5, 10, 10]
-
Calculate areas:
- Area_intersection = (10-5) Γ (10-5) = 25
- Area_box1 = (10-0) Γ (10-0) = 100
- Area_box2 = (15-5) Γ (15-5) = 100
- Area_union = 100 + 100 - 25 = 175
-
IoU = 25 / 175 = 0.1429
Constraints:
- Boxes are in [x1, y1, x2, y2] format (top-left and bottom-right corners)
- x2 > x1 and y2 > y1 for valid boxes
- Coordinates can be any real numbers
- Return IoU rounded to 4 decimal places
You want to compute how much two axis-aligned rectangles overlap, expressed as a ratio between 0 and 1 called Intersection over Union (IoU). This is a pure geometry / implementation exercise with constant-time operations.
1. Background Knowledge
In object detection, a model predicts bounding boxes for objects in an image, and these predictions are compared against ground-truth boxes. The most common accuracy metric is Intersection over Union (IoU), which measures how much the predicted box overlaps with the ground-truth box relative to their total combined area.
Each bounding box is typically represented as [x1β,y1β,x2β,y2β], where:
- (x1β,y1β) is the top-left corner,
- (x2β,y2β) is the bottom-right corner,
- and the box is axis-aligned (its edges are parallel to the image axes).
The IoU between two boxes A and B is:
IoU=AreaunionβAreaintersectionββ=AreaAβ+AreaBββAreaintersectionβAreaintersectionββIf boxes do not overlap, Areaintersectionβ=0 and so IoU=0. If they match exactly, intersection = union and IoU=1.
2. Algorithm / Approach
The key pattern is:
- Treat overlap in x and y dimensions independently.
- Compute a 1D overlap length along x and along y using max/min.
- Multiply these to get the intersection area (or 0 if no overlap).
- Compute each boxβs area.
- Compute union using the inclusionβexclusion formula.
- Return intersection / union.
This is a standard geometry / interval-overlap problem in 2D, framed as two 1D overlaps (x and y).
3. Step-by-Step Strategy
-
Unpack coordinates For box A: (x1Aβ,y1Aβ,x2Aβ,y2Aβ) For box B: (x1Bβ,y1Bβ,x2Bβ,y2Bβ)
-
Compute intersection rectangle coordinates
- x1interβ=max(x1Aβ,x1Bβ)
- y1interβ=max(y1Aβ,y1Bβ)
- x2interβ=min(x2Aβ,x2Bβ)
- y2interβ=min(y2Aβ,y2Bβ)
- Compute intersection width and height
inter_w = x2_inter - x1_inter
inter_h = y2_inter - y1_inter
- If inter_w <= 0 or inter_h <= 0, then no overlap: intersection area = 0.
- Intersection area
inter_area = max(inter_w, 0) * max(inter_h, 0)
- Individual box areas
area_A = max(x2_A - x1_A, 0) * max(y2_A - y1_A, 0)
area_B = max(x2_B - x1_B, 0) * max(y2_B - y1_B, 0)
- Union area
union_area = area_A + area_B - inter_area
- Compute IoU
- If union_area == 0 (degenerate boxes), define IoU as 0 (or handle specially).
iou = inter_area / union_area if union_area > 0 else 0.0
4. Common Pitfalls
-
Not checking for non-overlap: Directly multiplying negative inter_w or inter_h gives a negative βarea.β Always clamp with max(..., 0) or explicitly check with if inter_w <= 0 or inter_h <= 0.
-
Coordinate convention confusion: The problem states that (x1β,y1β) is top-left and (x2β,y2β) is bottom-right. Make sure you do not assume center-width-height form or bottom-left origin.
-
Degenerate boxes: When x1β=x2β or y1β=y2β, area is zero. Decide what IoU should be if both boxes have zero area and overlap at a point; for coding problems, usually return 0 to avoid division by zero.
-
Integer division: In some languages, dividing integers yields integer results. Cast to float to avoid IoU being truncated to 0 or 1.
-
Wrong formula for union: Union is not simply area_A + area_B. You must subtract the intersection once: union = area_A + area_B - intersection.
5. Time & Space Complexity
-
Time complexity: All operations are constant-time arithmetic; no loops or recursion. O(1).
-
Space complexity: Only a fixed number of variables are used. O(1) extra space.
This is essentially a small, careful implementation of interval intersection plus an area calculation.