非極大值抑制(Non-Maximum Suppression,NMS)
阿新 • • 發佈:2021-08-13
大家好,這是我的第一篇blog,寫作的主要是記錄後面兩個月的學習情況。
宣告,該篇文章主要內容來源於:https://www.cnblogs.com/makefile/p/nms.html,其中NMS原理其實非常簡單,就是首先排序它的置信度,然後從置信度高的位置開始找框的重合率,如果框的重合率大於某個閾值,則剔除該置信度小的框,下面上傳他的程式碼部分:
def py_cpu_nms(dets, thresh): """Pure python NMS baseline.""" #x1、y1、x2、y2、以及score賦值 x1 = dets[:, 0] y1= dets[;, 1] x2 = dets[;, 2] y2 = dets[;, 3] scores = dets[:, 4] #每一個檢測框的面積 areas = (x2 - x1 + 1) * (y2 - y1 + 1) #按照score置信度降序排列 order = scores.argsort()[::-1] keep = [] while order.size > 0: i = order[0] keep.append[i] #保留該類剩餘box中得分最高的一個#得到相交區域,左上及下 xx1 = np.maximum(x1[i], x1[order[1:]]) yy1 = np.maximum(y1[i], y1[order[1:]]) xx2 = np.maximum(x2[i], x2[order[1:]]) yy2 = np.maximum(y2[i], y2[order[1:]]) #每一個檢測框的面積 w = np.maximum(0.0, xx2 - xx1 + 1) h= np.maximum(0.0, yy2 - yy1 + 1) inter = w * h ovr = inter / (areas[i] + areas[order[1:]] - inter) inds = np.where(ovr <= thresh)[0] order = order[inds + 1] #因為ovr陣列的長度比order陣列少一個,所以這裡要將所有下標後移一位 return keep