检测评价函数IoU的理解与实现

2018-10-12  本文已影响0人  wenyilab

在检测评价体系中,有一个参数叫IoU(intersection-over-union ),简单来讲就是模型预测产生的矩形框和原来标签标记的矩形框的交叠率,具体到公式上就是检测结果与Ground Truth的交集比上它们的并集,如下所示:

IOU = \frac{DetectionResult \bigcap Ground Truth }{DetectionResult \bigcup Ground Truth}

这个公式也可以通过下图来直观理解:


wenyilab

代码实现如下:

def IoU(Reframe,GTframe):
    """
    自定义函数,计算两矩形 IOU,传入为均为矩形对角线,(x,y)  坐标。
    """
    x1 = Reframe[0]
    y1 = Reframe[1]
    width1 = Reframe[2]-Reframe[0]
    height1 = Reframe[3]-Reframe[1]

    x2 = GTframe[0]
    y2 = GTframe[1]
    width2 = GTframe[2]-GTframe[0]
    height2 = GTframe[3]-GTframe[1]

    endx = max(x1+width1,x2+width2)
    startx = min(x1,x2)
    width = width1+width2-(endx-startx)

    endy = max(y1+height1,y2+height2)
    starty = min(y1,y2)
    height = height1+height2-(endy-starty)
    """
    另一种求重叠区域面积的方式
    endx = min(x1+width1,x2+width2)
    startx = max(x1,x2)
    width = endx -startx

    endy = min(y1+height1,y2+height2)
    starty = max(y1,y2)
    height = endy - starty
    """
    if width <=0 or height <= 0:
        ratio = 0 # 重叠率为 0 
    else:
        Area = width*height # 两矩形相交面积
        Area1 = width1*height1
        Area2 = width2*height2
        ratio = Area*1./(Area1+Area2-Area)
    # return IoU
    return ratio,Reframe,GTframe
上一篇下一篇

猜你喜欢

热点阅读