CV(目標(biāo)檢測(cè))中的IOU計(jì)算,附代碼
1. 目標(biāo)檢測(cè)中的IOU
假設(shè),我們有兩個(gè)框,與,我們要計(jì)算其。其中的計(jì)算公式為,其交叉面積除以其并集。

的數(shù)學(xué)公式為:
上代碼:
def compute_iou(rec1, rec2):
"""
computing IoU
param rec1: (y0, x0, y1, x1) , which reflects (top, left, bottom, right)
param rec2: (y0, x0, y1, x1) , which reflects (top, left, bottom, right)
return : scale value of IoU
"""
S_rec1 =(rec1[2] -rec1[0]) *(rec1[3] -rec1[1])
S_rec2 =(rec2[2] -rec2[0]) *(rec2[3] -rec2[1])
#computing the sum area
sum_area =S_rec1 +S_rec2
#find the each edge of interest rectangle
left_line =max(rec1[1], rec2[1])
right_line =min(rec1[3], rec2[3])
top_line =max(rec1[0], rec2[0])
bottom_line =min(rec1[2], rec2[2])
#judge if there is an intersect
if left_line >=right_line or top_line >=bottom_line:
return 0
else:
intersect =(right_line -left_line) *(bottom_line -top_line)
return intersect /(sum_area -intersect)
這里我們主要討論下這個(gè)判斷,我們以橫軸方向?yàn)槔?,其中?duì)縱軸方向是一樣的,我們來(lái)判斷兩個(gè)框重合與否。其中為左上角的坐標(biāo),是右下角的坐標(biāo)。為的左上角坐標(biāo),是的右下角坐標(biāo)。

2. 語(yǔ)義分割中的IOU
先回顧下一些基礎(chǔ)知識(shí):
常常將預(yù)測(cè)出來(lái)的結(jié)果分為四個(gè)部分: , , , ,其中就是指非物體標(biāo)簽的部分(可以直接理解為背景),positive$就是指有標(biāo)簽的部分。下圖顯示了四個(gè)部分的區(qū)別:

圖被分成四個(gè)部分,其中大塊的白色斜線標(biāo)記的是 (TN,預(yù)測(cè)中真實(shí)的背景部分),紅色線部分標(biāo)記是 (,預(yù)測(cè)中被預(yù)測(cè)為背景,但實(shí)際上并不是背景的部分),藍(lán)色的斜線是 (,預(yù)測(cè)中分割為某標(biāo)簽的部分,但是實(shí)際上并不是該標(biāo)簽所屬的部分),中間熒光黃色塊就是 (,預(yù)測(cè)的某標(biāo)簽部分,符合真值)。
同樣的,計(jì)算公式:

def compute_ious(pred, label, classes):
'''computes iou for one ground truth mask and predicted mask'''
ious = [] # 記錄每一類的iou
for c in classes:
label_c = (label == c) # label_c為true/false矩陣
pred_c = (pred == c)
intersection = np.logical_and(pred_c, label_c).sum()
union = np.logical_or(pred_c, label_c).sum()
if union == 0:
ious.append(float('nan'))
else
ious.append(intersection / union)
return np.nanmean(ious) #返回當(dāng)前圖片里所有類的mean iou
其中,對(duì)于與有多種形式。
如識(shí)別目標(biāo)為4類,那么的形式可以是一張圖片對(duì)應(yīng)一份,其中 為背景,我們省略,則可以為。也可以是對(duì)應(yīng)四份二進(jìn)制 , 這四層的取值為。為了。
總結(jié)
對(duì)于目標(biāo)檢測(cè),寫(xiě)那就是必考題,但是我們也要回顧下圖像分割的怎么計(jì)算的。
引用
https://blog.csdn.net/weixin_42135399/article/details/101025941 https://blog.csdn.net/lingzhou33/article/details/87901365 https://blog.csdn.net/lingzhou33/article/details/87901365
雙一流高校研究生團(tuán)隊(duì)創(chuàng)建 ↓
專注于計(jì)算機(jī)視覺(jué)原創(chuàng)并分享相關(guān)知識(shí) ?
整理不易,點(diǎn)贊三連!
