Calculation Method of Intersection-Union Ratio (IOU)

        Intersetion Over Union (IOU, Intersetion Over Union), which means the ratio of intersection and union, is used to evaluate the area coincidence degree of two geometric figures. In the target detection algorithm model, it is usually used to calculate the predicted frame and the real frame. The error (loss function) is either used in the Non-Maximum Suppression (NMS) algorithm to filter redundant overlapping objects, or it is used to determine whether the object is predicted when calculating mAP. This is a very widely used algorithm. Here is a detailed introduction to its algorithm principle and code implementation.

Figure 1 Schematic diagram of cross-merge ratio

         As shown in Figure 1 is a schematic diagram of the intersection ratio of two rectangular boxes. The overlapping part in the middle is the intersection of rectangle A and rectangle B. Then the calculation formula of the intersection ratio of these two rectangular boxes is as follows:

IOU=\frac{A\cap B}{A\cup B}

Among them, the union of A and B can be calculated by the following formula:

A\cup B=A+B-A\cap B

Define the diagonal coordinates of rectangle A and rectangle B as [\left ( xmin_{A},ymin_{A} \right ),\left ( xmax_{A},ymax_{A} \right )], respectively [\left ( xmin_{B},ymin_{B} \right ),\left ( xmax_{B},ymax_{B} \right )], and at the same time define the diagonal coordinates of the intersection rectangle as [\left ( xmin_{AB},ymin_{AB} \right ),\left ( xmax_{AB},ymax_{AB} \right )], then the calculation method of the diagonal coordinates of the intersection rectangle is as follows:

xmin_{AB}=max(xmin_{A},xmin_{B})

ymin_{AB}=max(ymin_{A},ymin_{B})

xmax_{AB}=min(xmax_{A},xmax_{B})

ymax_{AB}=min(ymax_{A},ymax_{B})

Then, the intersection and union are calculated as follows:

A\cap B=\left ( xmax_{AB}-xmin_{AB} \right )*\left ( ymax_{AB} -ymin_{AB}\right )

W_{A}=xmax_{A}-xmin_{A}

H_{A}=ymax_{A}-ymin_{A}

W_{B}=xmax_{B}-xmin_{B}

H_{B}=xmax_{B}-xmin_{B}

A\cup B=W_{A}*H_{A}+W_{B}*H_{B}-A\cap B

The above formula is derived based on the intersection of two rectangles. If the two rectangles do not intersect, how does the above formula make a judgment? If there is no intersection between these two rectangles, then just judge A\cap B\leq 0, that is, if there is no intersection between the two rectangles, then the intersection ratio is 0. The calculation of the intersection and union ratio is implemented in python and C language codes respectively.

The python code is implemented as follows:

def iou(b1,b2):
    xmin1,ymin1,xmax1,ymax1=b1
    xmin2,ymin2,xmax2,ymax2=b2
    w1=xmax1-xmin1
    h1=ymax1-ymin1
    w2=xmax2-xmin2
    h2=ymax2-ymin2
    xmin_inter=max(xmin1,xmin2)
    ymin_inter=max(ymin1,ymin2)
    xmax_inter=min(xmax1,xmax2)
    ymax_inter=min(ymax1,ymax2)
    inter=(xmax_inter-xmin_inter)*(ymax_inter-ymin_inter)
    union=w1*h1+w2*h2-inter
    if inter<=0:
        return 0
    else:
        return inter/union

The C language code is implemented as follows:

struct Box
{
	double xmin;
	double ymin;
	double xmax;
	double ymax;
};

double iou(Box b1, Box b2)
{
	double w1 = b1.xmax - b1.xmin;
	double h1 = b1.ymax - b1.ymin;
	double w2 = b2.xmax - b2.xmin;
	double h2 = b2.ymax - b2.ymin;
	double xmin_inter,ymin_inter,xmax_inter,ymax_inter;
	xmin_inter = (b1.xmin > b2.xmin) ? b1.xmin : b2.xmin;
	ymin_inter = (b1.ymin > b2.ymin) ? b1.ymin : b2.ymin;
	xmax_inter = (b1.xmax < b2.xmax) ? b1.xmax : b2.xmax;
	ymax_inter = (b1.ymax < b2.ymax) ? b1.ymax : b2.ymax;
	double inter=(xmax_inter-xmin_inter)*(ymax_inter-ymin_inter);
	double un=w1*h1+w2*h2-inter;
	if(inter<=0)return 0;
	else return inter/un;
}

Guess you like

Origin blog.csdn.net/qq_28249373/article/details/129397006