1、概念

在目标检测的评价体系中,有一个参数叫做 IoU ,简单来讲就是模型产生的目标窗口和原来标记窗口的交叠率。具体我们可以简单的理解为: 即检测结果(DetectionResult)Ground Truth 的交集比上它们的并集,即为检测的准确率 IoU :

IOU=DetectionResultGroundTruthDetectionResultGroundTruthIOU=DetectionResult⋂GroundTruthDetectionResult⋃GroundTruth
<script type="math/tex; mode=display" id="MathJax-Element-1">IOU = \frac{DetectionResult \bigcap Ground Truth }{DetectionResult \bigcup Ground Truth}</script>

如下图所示:GT = GroundTruth; DR = DetectionResult;
黄色边框框起来的是:

GTDRGT⋂DR
<script type="math/tex; mode=display" id="MathJax-Element-2">GT\bigcap DR</script>
绿色框框起来的是:
GTDRGT⋃DR
<script type="math/tex; mode=display" id="MathJax-Element-3">GT\bigcup DR</script>

应该够详细了,上幅图直观些。当然最理想的情况就是 DR 与 GT 完全重合,即

IoU=1IoU=1
<script type="math/tex; mode=display" id="MathJax-Element-4">IoU = 1</script>
下面附上图例说明,及 IOU 的python实现,已经测试无误,自行取用。

这里写图片描述

原图:
这里写图片描述

# -*- coding: utf-8 -*-
"""
Created on Sun Aug 07 14:26:51 2016

@author: Eddy_zheng
"""

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)

    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
Logo

欢迎加入 MCP 技术社区!与志同道合者携手前行,一同解锁 MCP 技术的无限可能!

更多推荐