https://blog.csdn.net/qq_39852676/article/details/111066575
def nms(bboxes):
"""非极大抑制过程
:param bboxes: 同类别候选框坐标
:return:
"""
bboxes = np.array(bboxes.cpu())
# 取出n个的极坐标点
x1 = bboxes[:, 0]
y1 = bboxes[:, 1]
x2 = bboxes[:, 2]
y2 = bboxes[:, 3]
scores = bboxes[:, 4] * bboxes[:, 5]
# 2、对候选框进行NMS筛选
# 从大到小排列,取index
order = scores.argsort()[::-1]
areas = (x2 - x1) * (y2 - y1)
# 2、对候选框进行NMS筛选
# 返回的框坐标和分数
picked_boxes = []
while order.size > 0:
# 将当前置信度最大的框加入返回值列表中
index = order[-1]
picked_boxes.append(bboxes[index])
# 获取当前置信度最大的候选框与其他任意候选框的相交面积
x11 = np.maximum(x1[index], x1[order[:-1]])
y11 = np.maximum(y1[index], y1[order[:-1]])
x22 = np.minimum(x2[index], x2[order[:-1]])
y22 = np.minimum(y2[index], y2[order[:-1]])
# 计算当前矩形框与其余框的比值
rate = areas[index] / areas[order[:-1]]
# 计算其余框于u当前框的比值
rate1 = areas[order[:-1]] / areas[index]
w = np.maximum(0.0, x22 - x11)
h = np.maximum(0.0, y22 - y11)
intersection = w * h
# 利用相交的面积和两个框自身的面积计算框的交并比, 保留大于阈值的框
ratio = intersection / (areas[index] + areas[order[:-1]] - intersection)
# rate==ratio表示包含关系,保留不为包含关系的框
#修改2
keep_boxes_indics = np.where(abs(ratio - rate)>0.01)
keep_boxes_indics1 = np.where(abs(ratio - rate1)>0.01)
# 修改1
if keep_boxes_indics[0].__len__() < keep_boxes_indics1[0].__len__():
order = order[keep_boxes_indics]
else:
order = order[keep_boxes_indics1]
return picked_boxes
计算两个bbox相交的面积站两个面积和的比值
计算1个bbox站其他的比值
如果两个比值相等,那么完全包含。我写的是相比小于0.01 就是两个比值很相似也算是包含。



