有两个问题没有思考的很好,望各位可以留言讨论!
1. 为啥算面积时候要+1
2. 为啥np.where只用第一维[0]
import numpy as np
def nms(dets, prob_threshold):
x1 = dets[:, 0]
y1 = dets[:, 1]
x2 = dets[:, 2]
y2 = dets[:, 3]
scores = dets[:, 4]
areas = (x2 - x1 + 1) * (y2 - y1 + 1)
# 获得由大到小的分数索引
score_index = np.argsort(scores)[::-1]
keep = []
while score_index > 0:
max_index = score_index[0]
# 最大的肯定是需要的框
keep.append(max_index)
xx1 = np.maximum(x1[max_index], x1[score_index[1:]])
yy1 = np.maximum(y1[max_index], y1[score_index[1:]])
xx2 = np.maximum(x2[max_index], x2[score_index[1:]])
yy2 = np.maximum(y2[max_index], y2[score_index[1:]])
width = np.maximum(0.0, xx2 - xx1 + 1)
height = np.maximum(0.0, yy2 - yy1 + 1)
union = width * height
iou = union / (areas[max_index] + areas[score_index[1:]] - union)
ids = np.where(iou < prob_threshold)[0]
# 以为算iou的时候没把第一个参考框索引考虑进来,所以这里都要+1
score_index = score_index[ids+1]