计算边界框预测的 IOU

Calculating IOU for bounding box predictions

我有图片中给出的这两个边界框。方框坐标如下:

框 1 = [0.23072851 0.44545859 0.56389928 0.67707491] 方框 2 = [0.22677664 0.38237819 0.85152483 0.75449795]

坐标是这样的:ymin, xmin, ymax, xmax

我正在计算借条如下:

def get_iou(box1, box2):
    """
    Implement the intersection over union (IoU) between box1 and box2
        
    Arguments:
        box1 -- first box, numpy array with coordinates (ymin, xmin, ymax, xmax)
        box2 -- second box, numpy array with coordinates (ymin, xmin, ymax, xmax)
    """
    # ymin, xmin, ymax, xmax = box
    
    y11, x11, y21, x21 = box1
    y12, x12, y22, x22 = box2
    
    yi1 = max(y11, y12)
    xi1 = max(x11, x12)
    yi2 = min(y21, y22)
    xi2 = min(x21, x22)
    inter_area = max(((xi2 - xi1) * (yi2 - yi1)), 0)
    # Calculate the Union area by using Formula: Union(A,B) = A + B - Inter(A,B)
    box1_area = (x21 - x11) * (y21 - y11)
    box2_area = (x22 - x12) * (y22 - y12)
    union_area = box1_area + box2_area - inter_area
    # compute the IoU
    iou = inter_area / union_area
    return iou

根据我的理解,这两个盒子完全重叠,所以 IOU 应该是 1。但是我得到的 IOU 是 0.33193138665968164 .是不是我做错了什么,或者我对它的解释不正确。在这方面的任何建议都会有所帮助。

您对 IoU 的解释不正确。

如果注意你的示例,你会注意到两个边界框的面积的并集比面积的交集大得多。因此,IoU——确实是交集/并集——比一个小得多是有道理的。

当你说

Based on my understanding these 2 boxes completely overlap each other so IOU should be 1.

事实并非如此。在您的情况下,两个边界框仅在一个完全包含在另一个中的意义上重叠。但是如果这种情况没有受到惩罚,IoU 总是可以最大化预测一个与图像一样大的边界框——这显然没有意义。