【计算机视觉】交并比IOU概念理解

前言

交并比IOU(Intersection over Union)是一种测量在特定数据集中检测相应物体准确度的一个标准。

图示

很简单,IoU相当于两个区域重叠的部分除以两个区域的集合部分得出的结果。

一般来说,这个score > 0.7 就可以被认为一个不错的结果了。

需要注意两个区域的位置,也有可能没有交集。

code

python版本:

# import the necessary packages
from collections import namedtuple
import numpy as np
import cv2
# define the `Detection` object
Detection = namedtuple("Detection", ["image_path", "gt", "pred"])
def bb_intersection_over_union(boxA, boxB):
    # determine the (x, y)-coordinates of the intersection rectangle
    xA = max(boxA[0], boxB[0])
    yA = max(boxA[1], boxB[1])
    xB = min(boxA[2], boxB[2])
    yB = min(boxA[3], boxB[3])
    # compute the area of intersection rectangle
    interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)
    # compute the area of both the prediction and ground-truth
    # rectangles
    boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
    boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)
    # compute the intersection over union by taking the intersection
    # area and dividing it by the sum of prediction + ground-truth
    # areas - the interesection area
    iou = interArea / float(boxAArea + boxBArea - interArea)
    # return the intersection over union value
    return iou

c++版本:

//compute iou.
float compute_iou(cv::Rect boxA, cv::Rect boxB)
{
   int xA = std::max(boxA.x, boxB.x);
   int yA = std::max(boxA.y, boxB.y);
   int xB = std::min(boxA.x+boxA.width, boxB.x+boxB.width);
   int yB = std::min(boxA.y+boxA.height, boxB.y+boxB.height);

   float inter_area = max(0, xB-xA+1) * max(0, yB-yA+1);
   float boxA_area = boxA.width * boxA.height;
   float boxB_area = boxB.width * boxB.height;

   float iou = inter_area / (boxA_area + boxB_area - inter_area);
   return iou;
}

 参考

1.oldpan博客

2.IOU

原文地址:https://www.cnblogs.com/happyamyhope/p/9629358.html