目標檢測 IOU(交並比) 理解筆記


交並比(Intersection-over-Union,IoU):

目標檢測中使用的一個概念
是產生的候選框(candidate bound)與原標記框(ground truth bound)的交疊率
它們的交集與並集的比值。最理想情況是完全重疊,即比值為1。

基礎知識:

交集:
集合論中,設A,B是兩個集合,由所有屬於集合A且屬於集合B的元素所組成的集合,叫做集合A與集合B的交集,記作A∩B。

eg:
A={1,2,3} B={2,3,4}
A n B = {2,3}

並集:
給定兩個集合A,B,把他們所有的元素合並在一起組成的集合,叫做集合A與集合B的並集,記作A∪B,讀作A並B。
eg:
A={1,2,3} B={2,3,4}
A U B = {1,2,3,4}

圖示

IOU:

python實現

import numpy as np
def compute_iou(box1, box2, wh=False):
    """
    compute the iou of two boxes.
    Args:
        box1, box2: [xmin, ymin, xmax, ymax] (wh=False) or [xcenter, ycenter, w, h] (wh=True)
        wh: the format of coordinate.
    Return:
        iou: iou of box1 and box2.
    """
    if wh == False:
        xmin1, ymin1, xmax1, ymax1 = box1
        xmin2, ymin2, xmax2, ymax2 = box2
    else:
        xmin1, ymin1 = int(box1[0]-box1[2]/2.0), int(box1[1]-box1[3]/2.0)
        xmax1, ymax1 = int(box1[0]+box1[2]/2.0), int(box1[1]+box1[3]/2.0)
        xmin2, ymin2 = int(box2[0]-box2[2]/2.0), int(box2[1]-box2[3]/2.0)
        xmax2, ymax2 = int(box2[0]+box2[2]/2.0), int(box2[1]+box2[3]/2.0)

    ## 獲取矩形框交集對應的左上角和右下角的坐標(intersection)
    xx1 = np.max([xmin1, xmin2])
    yy1 = np.max([ymin1, ymin2])
    xx2 = np.min([xmax1, xmax2])
    yy2 = np.min([ymax1, ymax2])
    
    ## 計算兩個矩形框面積
    area1 = (xmax1-xmin1) * (ymax1-ymin1) 
    area2 = (xmax2-xmin2) * (ymax2-ymin2)
    
    inter_area = (np.max([0, xx2-xx1])) * (np.max([0, yy2-yy1])) #計算交集面積
    iou = inter_area / (area1+area2-inter_area+1e-6) #計算交並比

    return iou


參考

https://blog.csdn.net/sinat_34474705/article/details/80045294
https://blog.csdn.net/mdjxy63/article/details/79343733


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM