Skip to content

Instantly share code, notes, and snippets.

@belltailjp
Created March 22, 2017 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save belltailjp/145906f7e63ce20685abc5b964cef493 to your computer and use it in GitHub Desktop.
Save belltailjp/145906f7e63ce20685abc5b964cef493 to your computer and use it in GitHub Desktop.
Intersection over Union
def iou(r1, r2):
x1, y1, w1, h1 = r1
x2, y2, w2, h2 = r2
and_x1, and_y1 = max(x1, x2), max(y1, y2)
and_x2, and_y2 = min(x1 + w1, x2 + w2), min(y1 + h1, y2 + h2)
and_w = and_x2 - and_x1
and_h = and_y2 - and_y1
if and_w <= 0 or and_h <= 0:
return 0
and_area = and_w * and_h
area1 = w1 * h1
area2 = w2 * h2
or_area = area1 + area2 - and_area
return and_area / or_area
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment