Skip to content
DeepForgeby svx
All problems
HardComputer Visioncv-039

Intersection over Union

Problem

Compute the intersection over union (IoU) of two bounding boxes.

Boxes are [x1, y1, x2, y2] with continuous coordinates, so a box area is (x2 - x1) * (y2 - y1). Return intersection / union rounded to 4 decimal places, or 0.0 when the boxes do not overlap or a box is degenerate.

Hint

Intersection dimensions are clamped to zero when the boxes do not overlap.

Test cases

case 1
in: [[0,0,2,2],[0,0,2,2]]
expected: 1
case 2
in: [[0,0,2,2],[3,3,5,5]]
expected: 0
case 3
in: [[0,0,2,2],[1,0,3,2]]
expected: 0.3333
case 4
in: [[0,0,4,4],[2,2,6,6]]
expected: 0.1429
case 5
in: [[0,0,0,0],[0,0,2,2]]
expected: 0
Starter codePython
def iou(box_a, box_b):
    # Your code here
    pass

5 test cases · run your code against them in the editor