NMS 非極大值抑制 實現

2021-08-23 14:15:36 字數 3200 閱讀 3170

nms演算法思路**於:

把置信度最高的乙個boundingbox(bbox)作為目標,然後對比剩下bbox與目標bbox之間的交叉區域

如果交叉區域大於設定的閾值,那麼在剩下的bbox中去除該bbox(即使該bbox的置信度與目標bbox的置信度一樣)—-這個操作就是抑制最大重疊區域

把第二置信度高的bbox作為目標,重複1、2

import numpy as np

def nms(dets, thresh):

x1 = dets[:, 0]

y1 = dets[:, 1]

x2 = dets[:, 2]

y2 = dets[:, 3]

score = dets[:, 4]

sort_id_list = score.argsort()[::-1].tolist()

res =

while len(sort_id_list) >= 1:

i = sort_id_list.pop(0)

#intersect area left top point(xx1, yy1): xx1 >= x1, yy1 >= y1

#intersect area right down point(xx2, yy2): xx2 <= x2, yy2 <= y2

xx1 = np.maximum(x1[i], x1[sort_id_list[:]])

yy1 = np.maximum(y1[i], y1[sort_id_list[:]])

xx2 = np.minimum(x2[i], x2[sort_id_list[:]])

yy2 = np.minimum(y2[i], y2[sort_id_list[:]])

inter_w = np.maximum(0, (xx2 - xx1))

inter_h = np.maximum(0, (yy2 - yy1))

intersect = inter_w * inter_h

#iou = intersect area / union; union = box1 + box2 - intersect

iou = intersect / ((x2[i] - x1[i]) * (y2[i] - y1[i]) +\

(x2[sort_id_list[:]] - x1[sort_id_list[:]]) * (y2[sort_id_list[:]] - y1[sort_id_list[:]]) -\

intersect)

for i in reversed(range(len(sort_id_list))):

if iou[i] > thresh:

sort_id_list.pop(i)

return res

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.3

res = nms(dets, thresh)

print(res)

import numpy as np

def nms(dets, thresh):

x1 = dets[:, 0]

y1 = dets[:, 1]

x2 = dets[:, 2]

y2 = dets[:, 3]

score = dets[:, 4]

order = score.argsort()[::-1]

area = (x2 - x1) * (y2 - y1)

res =

while order.size >= 1:

i = order[0]

#intersect area left top point(xx1, yy1): xx1 >= x1, yy1 >= y1

#intersect area right down point(xx2, yy2): xx2 <= x2, yy2 <= 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:]])

w = np.maximum(0.0, (xx2 - xx1))

h = np.maximum(0.0, (yy2 - yy1))

intersect = w * h

#iou = intersect area / union; union = box1 + box2 - intersect

iou = intersect / (area[i] + area[order[1:]] - intersect)

#update order index;ind +1:because ind is obtain by index [1:]

ind = np.where(iou <= thresh)[0]

order = order[ind +1]

return res

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.7

res = nms(dets, thresh)

print(res)

1、能在函式外通過一次計算實現的,盡量不要放到函式內進行多次迴圈計算,如area。

2、使用python高階庫有助於快速實現。

3、np.array 陣列本身可以作為另乙個np.array陣列的索引下標進行陣列訪問,如order[ind + 1](ind也為np.array陣列)

NMS 非極大值抑制 實現

1 iou計算 設兩個邊界框分別為a,b。a的座標為ax1,ax2,ay1,ay2,且ax1 ax2,ay1 ay2。b和a類似。則iou為a b除以a b。當兩個邊界框有重疊部分時,即位於上面邊框的y2大於下面邊框的y1,且左邊邊框的x2大於右邊邊框的x1時,iou才大於0.否則a和b不重疊,io...

非極大值抑制(NMS)

非極大值抑制 nms 非極大值抑制顧名思義就是抑制不是極大值的元素,搜尋區域性的極大值。這個區域性代表的是乙個鄰域,鄰域有兩個引數可變,一是鄰域的維數,二是鄰域的大小。這裡不討論通用的nms演算法,而是用於在目標檢測中用於提取分數最高的視窗的。例如在行人檢測中,滑動視窗經提取特徵,經分類器分類識別後...

NMS非極大值抑制

非極大值抑制演算法 non maximum suppression for object detection in python 非極大值抑制演算法 nms 非極大值抑制 矩形框融合 nms 卷積網路改進實現 筆記 人臉檢測視窗選擇辦法 nms convnet 開源 如何用soft nms實現目標檢...