天天看點

IOU計算方式

我能想到的有7種

IOU計算方式

如果隻是簡單的按照如下的來找left up和right down的點進行計算是不能囊括全部情況的

IOU計算方式

按照計算機圖形學的方法:

def cal_iou(box1, box2):
    # 從x軸看無交點,直接不用計算
    if box1[2] < box2[0] or box1[0] > box2[2]:
        return -1
    if box1[3] < box2[1] or box1[1] > box2[3]:
        return -1
    def cal_area(box):
        return (box[2] - box[0]) * (box[3] - box[1])
    area1 = cal_area(box1)
    area2 = cal_area(box2)
    # inner
    x = [box1[0], box1[2], box2[0], box2[2]]
    y = [box1[1], box1[3], box2[1], box2[3]]
    # 排序
    x.sort()
    y.sort()
    innerArea = abs(x[1]-x[2]) * abs(y[1]-y[2])
    return innerArea / (area1 + area2 - innerArea)
           

該算法的思想是,不管是7種情況中的哪種,裡面的矩形的寬高都能通過找到中間的兩條線之間的距離進行計算