Simply understand what the IOU of target detection is

Table of contents

A very important concept in target detection is IOU

So what are IOUs?

So why do we use IOU?

Question: Given two rectangular boxes, please calculate the IOU of both of them.

Implementation code:


A very important concept in target detection is IOU

So what are IOUs?

IOU is a metric for evaluating object detectors.

The following figure is an example: the green box in the figure is the actual box, and the red box is the predicted box. When we need to judge the relationship between the two boxes, what indicators do we need to use?
At this time, IOU is needed. 

The formula for calculating IOU is:

 It can be seen that IOU is a ratio, that is, the intersection and union ratio.
In the numerator part, the value is the overlapping area between the predicted box and the actual box;


In the denominator part, the value is the total area occupied by the predicted box and the actual box.

The ratio of the intersection area and the union area is the IOU.

So why do we use IOU?

The result of the object detection task is the locking of the box,

So when we input a picture, the model should give a guess, that is, where it thinks there are objects in the photo, and the range of the object. As a result, there will be an error between the result of the model speculation and the target, and the method to evaluate the degree of this error is IoU.

Iou is an evaluation index that allows us to compare the pros and cons of different target detection tasks or models.

Question: Given two rectangular boxes, please calculate the IOU of both of them.

Implementation code:

import cv2
import numpy as np
def CountIOU(RecA, RecB):
    xA = max(RecA[0], RecB[0])
    yA = max(RecA[1], RecB[1])
    xB = min(RecA[2], RecB[2])
    yB = min(RecA[3], RecB[3])
    # 计算交集部分面积
    interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)
    # 计算预测值和真实值的面积
    RecA_Area = (RecA[2] - RecA[0] + 1) * (RecA[3] - RecA[1] + 1)
    RecB_Area = (RecB[2] - RecB[0] + 1) * (RecB[3] - RecB[1] + 1)
    # 计算IOU
    iou = interArea / float(RecA_Area + RecB_Area - interArea)
    
    return iou

img = np.zeros((512,512,3), np.uint8)   
img.fill(255)

RecA = [50,50,300,300]
RecB = [60,60,320,320]

cv2.rectangle(img, (RecA[0],RecA[1]), (RecA[2],RecA[3]), (0, 255, 0), 5)
cv2.rectangle(img, (RecB[0],RecB[1]), (RecB[2],RecB[3]), (255, 0, 0), 5)

IOU = CountIOU(RecA,RecB)
font = cv2.FONT_HERSHEY_SIMPLEX

cv2.putText(img,"IOU = %.2f"%IOU,(130, 190),font,0.8,(0,0,0),2)

cv2.imshow("image",img)
cv2.waitKey()
cv2.destroyAllWindows()

Guess you like

Origin blog.csdn.net/qq_51831335/article/details/125719420