检测评价函数 intersection-over-union(IoU) PYTHON实现

文章引用地址:https://www.ziiai.com/blog/752

在目标检测的评价体系中,有一个参数叫做 IoU,简单而言就是模型产生的目标区域与原来标记区域的交叠率。即检测结果区域(Detection Result)与真值区域(Ground Truth)的交集比上它们的并集,其计算表达式为:

图示:

DR = Detection Result;GT = Ground Truth;

检测评价函数 intersection-over-union(IoU) PYTHON实现_第1张图片

代码:

def IoU(frame_DR, framw_GT):"""
    计算两矩形的IoU,传入为均为矩形对角线两端点坐标(x1,y1,x2,y2)
    """
    x1 = frame_DR[0]
    y1 = frame_DR[1]
    width1 = frame_DR[2] - frame_DR[0]
    height1 = frame_DR[3] - frame_DR[1]


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


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


    starty = min(y1, y2)
    endy = max(y1 + height1, y2 + height2)
    height = height1 + height2 - (endy - starty)


    if width <= 0 or height <= 0:
        iou = 0  # 重叠率为 0else:
        area = width * height  # 两矩形重叠面积
        area1 = width1 * height1
        area2 = width2 * height2
        iou = area * 1. / (area1 + area2 - area)
    return iou, frame_DR, framw_GT

 

你可能感兴趣的:(检测评价函数 intersection-over-union(IoU) PYTHON实现)