1. 程式人生 > >Intersection over Union(IoU) algorithms

Intersection over Union(IoU) algorithms

  IoU演算法可用與評估兩個多維度資料的相似度,舉一個實際應用,做CV,物件檢測,我們需要評估模型的識別準確率,不同於二元類問題,普通的評估演算法不合適,於是用到了這個演算法,這個演算法簡單易懂,評估效果也不錯。

  這裡主要討論如何計算並評估兩個矩形相交程度。有空再訓練一個物件檢測器,來試試水。。

  第一種對於資料形狀是這樣的 $ (x_{top-left}, y_{top-left}, w, h) $,意思是:給出了起始座標,矩形沿著 $ w, h $ 擴充套件開。

  演算法實現:

double IoU(int*a, int*b)
{
    int overlap_w = min(a[0] + a[2], b[0] + b[2]) - max(a[0], b[0]);
    int overlap_h = min(a[1] + a[3], b[1] + b[3]) - max(a[1], b[1]);
    int overlap_s = overlap_w*overlap_h;
    return overlap_s / double(a[2]*a[3] + b[2]*b[3] - overlap_s);
}

  第二種資料形狀是這樣的 $ (x_{top-left}, y_{top-left}, x_{right-down}, y_{right-down}) $,意思是:給出了起始座標和終點座標,如圖:

  

  演算法實現:

double IoU_2(int*a, int*b)
{
    int overlap_w = min(a[2], b[2]) - max(a[0], b[0]);
    int overlap_h = min(a[3], b[3]) - max(a[1], b[1]);
    int overlap_s = overlap_w*overlap_h;
    return overlap_s / double((a[2] - a[0])*(a[3] - a[1]) + (b[2] - b[0])*(b[3] - b[1]) - overlap_s);
}

  用這幾組資料測試一下:

1: [39, 63, 203, 112], [54, 66, 198, 114]
2: [49, 75, 203, 125], [42, 78, 186, 126]
3: [31, 69, 201, 125], [18, 63, 235, 135]
4: [50, 72, 197, 121], [54, 72, 198, 120]
5: [35, 51, 196, 110], [36, 60, 180, 108]

  output:

0.825758
0.795771
=====================
0.809624
0.787838
=====================
0.791962
0.609319
=====================
0.947743
0.946628
=====================
0.79667
0.727656
=====================

 

  參考:

    1.https://www.pyimagesearch.com/2016/11/07/intersection-over-union-iou-for-object-detection/

    2.https://en.wikipedia.org/wiki/Jaccard_index