1. 程式人生 > >非極大值抑制--NMS

非極大值抑制--NMS

str -- ini 未處理 __main__ minimum png 核心 main

NMS(non maximum suppression)應用:邊緣檢測、目標檢測等。 背景: 絕大多數人臉檢測器的核心是分類器,即給定一個尺寸固定的圖片,分類器判斷是否為人臉; 而分類器晉升為檢測器的關鍵是: 常用滑動窗口在原始圖像上從多個尺度產生窗口,並resize到固定尺寸,然後送給分類器做判斷,如下圖所示: 技術分享圖片 技術分享圖片 目標: 一個對象只保留一個最優的框 ,用非極大值抑制,來抑制那些冗余的框: 抑制的過程是一個叠代-遍歷-消除的過程。 NMS 算法的核心: 1.將所有框的得分排序,選中置信度最高的一個boundingbox(bbox)框作為目標 2.遍歷其余的框,如果和目標bbox 最高分框的重疊面積(IOU)大於一定閾值,我們就在剩下的bbox中去除該bbox(即使該bbox的置信度與目標bbox的置信度一樣) 3.從未處理的框中繼續選一個得分最高的(第二置信度),重復上述1、2過程。
def
nms(dets, thresh): ‘‘‘ NMS非極大值抑制算法 ‘‘‘ x1 = dets[:, 0] y1 = dets[:, 1] x2 = dets[:, 2] y2 = dets[:, 3] scores = dets[:, 4] # confidence # 每個boundingbox的面積 areas = (x2 - x1 + 1) * (y2 - y1 + 1) # boundingbox的置信度排序 order = scores.argsort()[::-1] # 用來保存最後留下來的boundingbox
keep = [] while order.size > 0: i = order[0] # 置信度最高的boundingbox的index keep.append(i) # 添加本次置信度最高的boundingbox的index # 當前bbox和剩下bbox之間的交叉區域; 選擇大於x1,y1和小於x2,y2的區域 xx1 = np.maximum(x1[i], x1[order[1:]]) yy1 = np.maximum(y1[i], y1[order[1:]]) xx2
= np.minimum(x2[i], x2[order[1:]]) yy2 = np.minimum(y2[i], y2[order[1:]]) # 當前bbox和其他剩下bbox之間交叉區域的面積 w = np.maximum(0.0, xx2 - xx1 + 1) h = np.maximum(0.0, yy2 - yy1 + 1) inter = w * h # 交叉區域面積 / (bbox + 某區域面積 - 交叉區域面積) ovr = inter / (areas[i] + areas[order[1:]] - inter) # 保留交集小於一定閾值的boundingbox inds = np.where(ovr <= thresh)[0] order = order[inds + 1] return keep if __name__ == "__main__": dets = np.array([ [204, 102, 358, 250, 0.5], [257, 118, 380, 250, 0.7], [280, 135, 400, 250, 0.6], [255, 118, 360, 235, 0.7]]) thresh = 0.4 print "最大bbox:", nms(dets, thresh)

非極大值抑制--NMS