I have list of bounding boxes. When I plot them, there are some boxes overlapping with other. How can I identify them?
Here is attached example
Here we can see box 3,4 is overlapping, 10,11 is overlapping and box 7 is inside box 6. So I want to remove box which are 80% overlapping or 80% inside other box.
Here are the coordinates
boxes=[(0, (156.52566528320312, 411.3934326171875, 508.0946350097656, 445.0401611328125)),
(1, (153.34573364257812, 447.56744384765625, 1044.3194580078125, 612.4976196289062)),
(2, (150.6321258544922, 662.0474243164062, 1076.75439453125, 899.3271484375)),
(3, (154.38674926757812, 945.8661499023438, 1060.038330078125, 1026.8682861328125)),
(4, (138.6205596923828, 951.3151245117188, 1035.56884765625, 1027.590087890625)),
(5, (1245.50048828125, 410.4453430175781, 1393.0701904296875, 445.3376770019531)),
(6, (1240.206787109375, 456.7169189453125, 2139.934326171875, 659.1046752929688)),
(7, (1236.478759765625, 568.0098876953125, 2145.948486328125, 654.7606201171875)),
(8, (1244.784912109375, 702.7620239257812, 2121.079345703125, 736.1748046875)),
(9, (1244.885986328125, 746.2633666992188, 2151.534423828125, 991.8198852539062)),
(10, (1251.84814453125, 1031.8487548828125, 2134.333251953125, 1153.9320068359375)),
(11, (1254.38330078125, 1035.0196533203125, 2163.969970703125, 1153.2939453125))]
Here is the code, I used to generate above image
import cv2
import matplotlib.pyplot as plt
import numpy as np
img = np.ones([1654,2339,3],dtype=np.uint8)*255
for i in boxes:
box=[int(i) for i in i[1]]
image = cv2.rectangle(img, (box[0],box[1]), (box[2],box[3]), (0,0,0), 5)
cv2.putText(
img = image,
text = str(i[0]),
org = (box[0]+int(np.random.randint(0, high=500, size=1)),box[1]),
fontFace = cv2.FONT_HERSHEY_DUPLEX,
fontScale = 3.0,
color = (0, 0, 0),
thickness = 3
)
plt.imshow(img)
The output I want is box 0,1,2,3 or 4 (the larger one), 5,6 ,8,9,10 or 11 (the larger one)
The solution I found work but not for all cases Here is my solution
#x_1, y_1, x_2, y_2
for i in range(len(boxes)-1):
x_min_1,y_min_1,x_max_1,y_max_1=boxes[i][1][0],boxes[i][1][1],boxes[i][1][2],boxes[i][1][3]
x_min_2,y_min_2,x_max_2,y_max_2=boxes[i+1][1][0],boxes[i+1][1][1],boxes[i+1][1][2],boxes[i+1][1][3]
box_1_in_box_2 = ((x_max_2> x_min_1 >= x_min_2) or \
(x_max_2>= x_max_1 >x_min_2)) and \
((y_max_2> y_min_1 >= y_min_2) or \
(y_max_2>= y_max_1 > y_min_2))
box_2_in_box_1 = ((x_max_1> x_min_2 >= x_min_1) or (x_max_1>= x_max_2 >x_min_1)) and ((y_max_1> y_min_2 >= y_min_1) or (y_max_1>= y_max_2 > y_min_1))
overlap = box_1_in_box_2 or box_2_in_box_1
print(i,overlap)



For each box, calculate it's area. Then, for each other box, calculate how much that box overlaps in the x axis, and if it's greater than 0, calculate how much it overlaps in the y axis.