Skip to content

Instantly share code, notes, and snippets.

@meyerjo
Last active December 5, 2023 17:13
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
  • Save meyerjo/dd3533edc97c81258898f60d8978eddc to your computer and use it in GitHub Desktop.
Save meyerjo/dd3533edc97c81258898f60d8978eddc to your computer and use it in GitHub Desktop.
Python code to compute the intersection of two boundingboxes
def bb_intersection_over_union(boxA, boxB):
# determine the (x, y)-coordinates of the intersection rectangle
xA = max(boxA[0], boxB[0])
yA = max(boxA[1], boxB[1])
xB = min(boxA[2], boxB[2])
yB = min(boxA[3], boxB[3])
# compute the area of intersection rectangle
interArea = abs(max((xB - xA, 0)) * max((yB - yA), 0))
if interArea == 0:
return 0
# compute the area of both the prediction and ground-truth
# rectangles
boxAArea = abs((boxA[2] - boxA[0]) * (boxA[3] - boxA[1]))
boxBArea = abs((boxB[2] - boxB[0]) * (boxB[3] - boxB[1]))
# compute the intersection over union by taking the intersection
# area and dividing it by the sum of prediction + ground-truth
# areas - the interesection area
iou = interArea / float(boxAArea + boxBArea - interArea)
# return the intersection over union value
return iou
if __name__ == '__main__':
# Pointing out a wrong IoU implementation in https://www.pyimagesearch.com/2016/11/07/intersection-over-union-iou-for-object-detection/
boxA = [0., 0., 10., 10.]
boxB = [1., 1., 11., 11.]
correct = bb_intersection_over_union(boxA, boxB)
print('Correct solution - also analytical: {0}\n'
'Solution by published function: {1}\n'
'Solution by correction (ptyshevs): {2}'.format(correct, '0.704225352113', '0.680672268908'))
print('Normalizing coordinates in a 100x100 coordinate system')
boxA = [a / 100. for a in boxA]
boxB = [b / 100. for b in boxB]
correct = bb_intersection_over_union(boxA, boxB)
print('Correct solution - also analytical: {0}\n'
'Solution by published function: {1}\n'
'Solution by correction: {2}'.format(correct, '0.964445166004', '0.680672268908'))
print('Two boxes with no overlap')
boxA = [0., 0., 10., 10.]
boxB = [12., 12., 22., 22.]
correct = bb_intersection_over_union(boxA, boxB)
print('Correct solution - also analytical: {0}\n'
'Solution by published function: {1}\n'
'Solution by correction (ptyshevs): {2}'.format(correct, '0.0', '0.0204081632653'))
print('Example in the comments from ptyshevs')
boxA = [0., 0., 2., 2.]
boxB = [1., 1., 3., 3.]
correct = bb_intersection_over_union(boxA, boxB)
print('Correct solution - also analytical: {0}\n'
'Solution by published function: {1}\n'
'Solution by correction (ptyshevs): {2}'.format(correct, '0.285714285714', '0.142857142857'))
@rajeev-samalkha
Copy link

Thank you Meyerjo

@prb2307
Copy link

prb2307 commented Nov 27, 2018

@meyejo Thanks for the implementation . will this work if rectangle inside the other rectangle?

@prb2307
Copy link

prb2307 commented Nov 27, 2018

@meyejo what if there are multiple bounding boxes?

@meyerjo
Copy link
Author

meyerjo commented Jan 14, 2019

@meyejo what if there are multiple bounding boxes?

Just iterate over all possible combinations

@meyerjo
Copy link
Author

meyerjo commented Jan 14, 2019

@meyejo Thanks for the implementation . will this work if rectangle inside the other rectangle?

This shouldn't be an issue. The union space of both boxes is 1. It depends on the respective size of the boxes, how large the IoU becomes.

@fader111
Copy link

fader111 commented Dec 5, 2019

Pointing out a wrong IoU implementation in https://www.pyimagesearch.com/2016/11/07/intersection-over-union-iou-for-object-detection/
is that still actual? if so, what the problem with his code?

@SamihaSara
Copy link

are the bounding box coordinates in this format [x1 y1 x2 y2] ? Using this code sometimes I am getting iou>1, What could be the reason?

@kkew3
Copy link

kkew3 commented Apr 25, 2020

Why is the abs in line 9 necessary? Isn't the two maxima always at least zero? Thanks!

@mhiyer
Copy link

mhiyer commented Jul 5, 2021

Pointing out a wrong IoU implementation in https://www.pyimagesearch.com/2016/11/07/intersection-over-union-iou-for-object-detection/
is that still actual? if so, what the problem with his code?

Is this the line that is problematic?
interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)

@jopagel
Copy link

jopagel commented Oct 21, 2021

Thanks meyerjo!

@markub3327
Copy link

interArea = abs(max((xB - xA, 0)) * max((yB - yA), 0))

Should be
interArea = max((xB - xA), 0) * max((yB - yA), 0)

Because max(x, 0) the absolute value is unnecessary.

@macqueen09
Copy link

interArea = abs(max((xB - xA, 0)) * max((yB - yA), 0))

Should be interArea = max((xB - xA), 0) * max((yB - yA), 0)

Because max(x, 0) the absolute value is unnecessary.

yep the abs is useless, which should be remove.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment