nms
nms函数是保留选框中得分最高的那一个
Python代码如下
def nms(boxes, threshold, method):
"""
boxes: [x1, y1, x2, y2, score]
"""
# if input is empty return directly
if boxes.size == 0:
return np.empty(shape=[0])
x1 = boxes[:, 0] # up-left point
y1 = boxes[:, 1] # of the bounding box
x2 = boxes[:, 2] # bottom-right point
y2 = boxes[:, 3] # of the bounding box
s = boxes[:, 4] # score of the bounding box
# area of the bounding boxes
area = (x2 - x1 + 1) * (y2 - y1 + 1)
# sort by score
I = np.argsort(s)
# initial
pick = np.zeros_like(s, dtype=np.uint16)
counter = 0
while I.size > 0:
i = I[-1] # hightest score
pick[counter] = i
counter += 1
idx = I[0:-1]
# area of intersection
xx1 = np.maximum(x1[i], x1[idx])
yy1 = np.maximum(y1[i], y1[idx])
xx2 = np.minimum(x2[i], x2[idx])
yy2 = np.minimum(y2[i], y2[idx])
w = np.maximum(0.0, xx2 - xx1 + 1)
h = np.maximum(0.0, yy2 - yy1 + 1)
inter = w * h
if method is 'Min':
# inter_area/minium_area
o = inter / np.minimum(area[i], area[idx])
else:
# inter_area/union_area
o = inter / (area[i] + area[idx] - inter)
I = I[np.where(o <= threshold)]
pick = pick[0:counter]
return pick