Intersection Over Union

kamal_DS
2 min readMar 15, 2023

--

IOU (Intersection over Union) is an important concept in object detection that is used to evaluate the accuracy of predicted bounding boxes compared to the ground truth bounding boxes.

When an object detection model predicts the location of an object in an image, it outputs a bounding box that represents the predicted location of the object. The ground truth bounding box represents the true location of the object in the image. The IOU metric is used to calculate the overlap between the predicted bounding box and the ground truth bounding box.

The IOU is calculated by dividing the area of intersection between the predicted and ground truth bounding boxes by the area of their union. The formula for calculating IOU is as follows:

IOU = (Area of Intersection)/(Area of Union)

Here’s an example to help illustrate the calculation of IOU:

Suppose that the predicted bounding box for an object has coordinates (x<sub>p</sub>, y<sub>p</sub>, w<sub>p</sub>, h<sub>p</sub>), and the ground truth bounding box has coordinates (x<sub>g</sub>, y<sub>g</sub>, w<sub>g</sub>, h<sub>g</sub>).

The area of intersection between the two boxes can be calculated as follows:

Area of Intersection = max(0, min(x<sub>p</sub>+w<sub>p</sub>, x<sub>g</sub>+w<sub>g</sub>) — max(x<sub>p</sub>, x<sub>g</sub>)) * max(0, min(y<sub>p</sub>+h<sub>p</sub>, y<sub>g</sub>+h<sub>g</sub>) — max(y<sub>p</sub>, y<sub>g</sub>))

# Lets Take x1,y1,x2,y2 for ground truth and predicted box:

ground_truth = [2 , 3 , 15, 19]

predicted = [10 , 14 , 31, 33] # Take x3,y3,x4,y4 for predicted box


# Intersection:
# Formula = max(x1,x3) , max(y1,y3)
# max(x2 , x4) , max(y2 , y4)

pt1 = max(ground_truth[0] , predicted[0])
pt2 = max(ground_truth[1] , predicted[1])

pt3 = max(ground_truth[2],predicted[2])
pt4 = max(ground_truth[3] , predicted[3])


intersect = (pt3 - pt1) * (pt4 - pt2)

The area of union between the two boxes can be calculated as follows:

Area of Union = Area of Predicted Bounding Box + Area of Ground Truth Bounding Box — Area of Intersection

# union:


g_width = ground_truth[2] - ground_truth[0]
g_height = ground_truth[3] - ground_truth[1]

g_area = g_width * g_height

p_width = predicted[2] - predicted[0]
p_height = predicted[3] - predicted[1]

p_area = p_width * p_height

union = g_area + p_area - intersect

Using these two values, the IOU can be calculated as follows:

IOU = (Area of Intersection)/(Area of Union)


iou = intersect / union


print(iou)

The IOU metric is used to evaluate the accuracy of object detection models during training and testing. Typically, a threshold value is set for IOU, and any predicted bounding box with an IOU greater than the threshold is considered a true positive detection. The threshold value is usually set to 0.5, but it can be adjusted depending on the specific use case and requirements.

For Non-Max Suppression Follow: https://medium.com/p/b0569e89e9e7/edit

LinkedIn : https://www.linkedin.com/in/sai-kamal-korlakunta-a81326163/

Computer Vision Blogs : https://korlakuntasaikamal10.medium.com/

--

--

kamal_DS
kamal_DS

Written by kamal_DS

Interested to work in the field of Artificial Intelligence, Machine Learning, Deep Learning, NLP and Computer Vision.

No responses yet