python實(shí)現(xiàn)IOU計(jì)算案例
計(jì)算兩個(gè)矩形的交并比,通常在檢測(cè)任務(wù)里面可以作為一個(gè)檢測(cè)指標(biāo)。你的預(yù)測(cè)bbox和groundtruth之間的差異,就可以通過IOU來體現(xiàn)。很簡(jiǎn)單的算法實(shí)現(xiàn),我也隨便寫了一個(gè),嗯,很簡(jiǎn)單。
1. 使用時(shí),請(qǐng)注意bbox四個(gè)數(shù)字的順序(y0,x0,y1,x1),順序不太一樣。
#!/usr/bin/env python# encoding: utf-8 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) :return: scala value of IoU ''' # computing area of each rectangles 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 intersect 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))*1.0 if __name__==’__main__’: rect1 = (661, 27, 679, 47) # (top, left, bottom, right) rect2 = (662, 27, 682, 47) iou = compute_iou(rect1, rect2) print(iou)
補(bǔ)充知識(shí):基于Python實(shí)現(xiàn)的IOU算法---最簡(jiǎn)單易懂的代碼實(shí)現(xiàn)
概念介紹:
交并比:(Intersection over Union)
如上圖所示,IOU值定位為兩個(gè)矩形框面積的交集和并集的比值。即:
交并比的實(shí)現(xiàn)也是非常簡(jiǎn)單的,執(zhí)行過程如下:
1. 交集形狀的寬度計(jì)算為:
IOU_W = min(x1,x2,x3,x4)+w1+w2-max(x1,x2,x3,x4)
2. 交集形狀的高度計(jì)算為:
IOU_H = min(y1,y2,y3,y4)+h1+h2-max(y1,y2,y3,y4)
其實(shí)是很簡(jiǎn)單的幾何關(guān)系變換,上面的圖可以幫助你很好的理解這個(gè)意思。
代碼實(shí)現(xiàn):001-IOU計(jì)算
以上這篇python實(shí)現(xiàn)IOU計(jì)算案例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. java實(shí)現(xiàn)圖形化界面計(jì)算器2. IntelliJ Idea2017如何修改緩存文件的路徑3. IntelliJ IDEA設(shè)置條件斷點(diǎn)的方法步驟4. IIS Express 取代 ASP.NET Development Server的配置方法5. python flask框架快速入門6. Spring-Richclient 0.1.0 發(fā)布7. javascript設(shè)計(jì)模式 ? 建造者模式原理與應(yīng)用實(shí)例分析8. 淺談SpringMVC jsp前臺(tái)獲取參數(shù)的方式 EL表達(dá)式9. Python使用oslo.vmware管理ESXI虛擬機(jī)的示例參考10. Express 框架中使用 EJS 模板引擎并結(jié)合 silly-datetime 庫(kù)進(jìn)行日期格式化的實(shí)現(xiàn)方法
