Created
March 22, 2017 06:45
-
-
Save belltailjp/145906f7e63ce20685abc5b964cef493 to your computer and use it in GitHub Desktop.
Intersection over Union
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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