Open In App

Calculate Percentage of Bounding Box Overlap, for Image Detector Evaluation using Python

Last Updated : 13 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

There occurs various instances when you need to calculate by how much percentage two boxes in an image overlap. Did a similar situation has occurred to you? Read the article below to know how you can deal with that problem. What we exactly do is obtain the dimensions of two boxes from the user.  Then, calculate the intersection of two boxes and the union of two boxes. Finally, divide the intersection by union and multiply by 100 for calculating the bounding box percentage.

Stepwise Implementation:

Step 1: Importing the libraries

First of all, we import the library Polygon. The package Polygon handles polygonal shapes in 2D

from shapely.geometry import Polygon

Step 2: Obtain the dimensions of the boxes

Now, we take the dimensions of the two polygons from the user for which they want to calculate bounding box percentage.

box_shape_1 = [#Dimensions of Box-1]
box_shape_2 = [#Dimensions of Box-2]

Step 3: Draw polygon from dimensions

In this step, we will draw the polygons using Polygon() function that is a drawing function introduced in wand.drawing module for drawing complex shapes.

polygon_1 = Polygon(box_shape_1)
polygon_2 = Polygon(box_shape_2)

Step 4: Calculate the intersection of bounding boxes

Moreover, dividing the intersection of two polygons with the union of two polygons. The intersection of two polygons is calculated using the intersection() function, while the union of two polygons is calculated using the union() function respectively.

intersect = polygon_1.intersection(polygon_2).area / polygon_1.union(polygon_2).area

Step 5: Print the intersection percentage

Finally, multiply the result obtained from the last step by 100 for calculating the percentage and then printing the result. Also, don’t forget to round off the result by 2 digits. 

print(round(intersect*100,2),'%')

Example 1: 

In this example, we have used the polygon with 3 sides, i.e., triangle, and then calculated the bounding box percentage of those polygons.

Python3




# Python program to calculate
# percentage of bounding
# box overlap, for Image
# Detector Evaluation
 
# Import the library Polygon
from shapely.geometry import Polygon
 
# Give dimensions of shape 1
box_shape_1 = [[4, 2], [6, 4], [4, 6]]
 
# Give dimensions of shape 2
box_shape_2 = [[5, 3], [1, 4], [5, 5]]
 
# Draw polygon 1 from shape 1
# dimensions
polygon_1 = Polygon(box_shape_1)
 
# Draw polygon 2 from shape 2
# dimensions
polygon_2 = Polygon(box_shape_2)
 
# Calculate the intersection of
# bounding boxes
intersect = polygon_1.intersection(
    polygon_2).area / polygon_1.union(polygon_2).area
 
# Print the intersection percentage
print(round(intersect*100, 2), '%')


Output:

28.0 %

Example 2:

In this example, we have used the polygon with 4 sides, i.e., triangle, and then calculated the bounding box percentage of those polygons.

Python3




# Python program to calculate
# percentage of bounding
# box overlap, for Image
# Detector Evaluation
 
# Import the library Polygon
from shapely.geometry import Polygon
 
# Give dimensions of shape 1
box_shape_1 = [[511, 41], [577, 41],
               [577, 76], [511, 76]]
 
# Give dimensions of shape 2
box_shape_2 = [[530, 59], [610, 59],
               [610, 94], [530, 94]]
 
# Draw polygon 1 from shape 1
# dimensions
polygon_1 = Polygon(box_shape_1)
 
# Draw polygon 2 from shape 2
# dimensions
polygon_2 = Polygon(box_shape_2)
 
# Calculate the intersection of
# bounding boxes
intersect = polygon_1.intersection(
    polygon_2).area / polygon_1.union(polygon_2).area
 
# Print the intersection percentage
print(round(intersect*100, 2), '%')


Output:

18.53 %


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads