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=AreaunionAreaintersection=AreaA+AreaB−AreaintersectionAreaintersection
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
This is essentially a small, careful implementation of interval intersection plus an area calculation.