The starting address: mp.weixin.qq.com/s/PacfVzd4e…

It doesn’t matter whether the wind is southeast or northwest if you don’t know which port you’re heading for. Minimalism Paradigm 2: Figuring out what to do is half the battle

 

0. Tease

IOU is called jiaounion ratio in Chinese, which means the ratio of intersection and union. Is a commonly used algorithm in target detection

Ious principle

 

As shown in the figure above, calculate the ratio of the shaded part above to the shaded part below.

So let’s split up the task, so the numerator is the area where Box1 and Box2 intersect, so let’s call it A1. The denominator is the area of Box1 and Box2 combined. Call it A2, which is also S1 of Box1 plus S2 of Box2 minus A1 (Box1 and Box2 overlap A1)

IoU = A1 / A2

A2 = S1 + S2 -A1

IoU = A1 / (S1 + S2 -A1)

S1 is the area of Box1, S2 is the area of Box2. So the problem turns into solving for Box and A1.

Let’s move on to the disassembly task

0.0: Definition of coordinate axes

OpenCV coordinates detector

 

So let’s first look at the definition of axes in OpenCV. The origin is located in the upper left corner of the image. To the right is the positive X direction, and to the down is the positive Y direction (which is different from the mathematical definition).

0.1:Box represents and calculates

There are two common expressions for Box:

1: Boxa = (xmin,ymin,xmax,ymax) (coco and VOC format) 2: Boxb = (xCenter, yCenter,w,h) (yOLO format).

It’s essentially the same thing depending on how it’s expressed.

The corresponding transformation relationship is given below:

xmin,ymin,xmax,ymax = round(xcenter-(w/2.0)),round(ycenter-(h/2.0)),round(xcenter+(w/2.0)),round(ycenter+(h/2.0)) 
xcenter,ycenter,w,h = round((xmin+xmax)/2.0),round((ymin+ymax)/2.0),round(xmax-xmin),round(ymax-ymin)
Boxa = (round(Boxb[0]-(Boxb[2] /2.0)),round(Boxb[1]-(Boxb[3] /2.0),round(Boxb[0]+(Boxb[2] /2.0),round(Boxb[1]+(Boxb[3] /2.0))
Copy the code

The area of Box is calculated as w*h

S_Boxa = (xmax-xmin)*(ymax-ymin) = (Boxa[2]-Boxa[0])*(Boxa[3]-Boxa[1])

S_Boxb = w*h = Boxb[2]*Boxb[3]

0.2: Representation and calculation of A1

Some cases where box intersects

Just like we did in the last problem, we just have to figure out what w and H are at the intersection and if they don’t intersect, it’s 0. The following rule can be found from the figure above: If the intersection

xmin =max(xmin1, xmin2)Xmin is the maximum value of the upper-left x-coordinate of the two boxes:
ymin =max(ymin1, ymin2)# ymin is the maximum value of the y-coordinate in the upper left corner of the two boxes:
xmax =min(xmax1, xmax2)Xmax is the maximum value of the x coordinates in the lower right corner of the two boxes:
ymax =min(ymax1, ymax2)# ymax is the maximum value of the y coordinates in the lower right corner of the two boxes:
Copy the code

Finally, we can deal with the situation that we do not want to hand in. It can be found that when we do not want to hand in, there will be at least one of the following situations:

xmax<=xmin or ymax<ymin

So the solution is simple: in either case, either w or H is equal to 0, so that the calculated area is also 0

w =max(0, xmax – xmin)

h =max(0, ymax – ymin)

So the whole code is waved out, isn’t it also quite simple 🙂

Code 1.

def cal_iou(box1, box2) :
    """ :param box1: = [xmin1, ymin1, xmax1, ymax1] :param box2: = [xmin2, ymin2, xmax2, ymax2] :return: """
    xmin1, ymin1, xmax1, ymax1 = box1
    xmin2, ymin2, xmax2, ymax2 = box2
    Calculate the area of each rectangle
    s1 = (xmax1 - xmin1) * (ymax1 - ymin1)  The area of b1
    s2 = (xmax2 - xmin2) * (ymax2 - ymin2)  Area of b2
 
    # Compute intersecting rectangles
    xmin = max(xmin1, xmin2)
    ymin = max(ymin1, ymin2)
    xmax = min(xmax1, xmax2)
    ymax = min(ymax1, ymax2)
 
    w = max(0, xmax - xmin)
    h = max(0, ymax - ymin)
    a1 = w * h  # C∩G area
    a2 = s1 + s2 - a1
    iou = a1 / a2 #iou = a1/ (s1 + s2 - a1)
    return iou
Copy the code

1. Reference

1.0 :blog.csdn.net/guyuealian/…

1.1 :blog.csdn.net/weixin_4092…