Created
October 31, 2022 12:32
-
-
Save gsoykan/c8b0f829da04b67fae0caa46eeeac2ca to your computer and use it in GitHub Desktop.
calculating iou
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 get_iou(a, b, epsilon=1e-5): | |
""" Given two boxes `a` and `b` defined as a list of four numbers: | |
[x1,y1,x2,y2] | |
where: | |
x1,y1 represent the upper left corner | |
x2,y2 represent the lower right corner | |
It returns the Intersect of Union score for these two boxes. | |
source: http://ronny.rest/tutorials/module/localization_001/iou/ | |
Args: | |
a: (list of 4 numbers) [x1,y1,x2,y2] | |
b: (list of 4 numbers) [x1,y1,x2,y2] | |
epsilon: (float) Small value to prevent division by zero | |
Returns: | |
(float) The Intersect of Union score. | |
""" | |
# COORDINATES OF THE INTERSECTION BOX | |
x1 = max(a[0], b[0]) | |
y1 = max(a[1], b[1]) | |
x2 = min(a[2], b[2]) | |
y2 = min(a[3], b[3]) | |
# AREA OF OVERLAP - Area where the boxes intersect | |
width = (x2 - x1) | |
height = (y2 - y1) | |
# handle case where there is NO overlap | |
if (width < 0) or (height < 0): | |
return 0.0 | |
area_overlap = width * height | |
# COMBINED AREA | |
area_a = (a[2] - a[0]) * (a[3] - a[1]) | |
area_b = (b[2] - b[0]) * (b[3] - b[1]) | |
area_combined = area_a + area_b - area_overlap | |
# RATIO OF AREA OF OVERLAP OVER COMBINED AREA | |
iou = area_overlap / (area_combined + epsilon) | |
return iou |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment