NMS非极大值抑制算法--算法思想+python代码实现

NMS非极大值抑制算法

算法思想:
(1)设置目标框的置信度阈值,一般设置为0.5;
(2)然后根据置信度得分情况对候选框进行排序;
(3)然后选取得分最高的候选框,并将其输入到输出列表,同时从候选框列表中删除;
(4)计算得分最高的候选框与候选框列表中所有框的IoU值,如果所得IoU值大于设定的值则删掉;
(5)重复上述过程,直到候选框列表为空,返回输出列表。

具体python代码实现(可直接运行):

import numpy as np

def nms(bounding_boxes, Nt): #bounding_boxes是候选框;Nt为设定的阈值,一般为0.5
	if len(bounding_boxes) == 0:
		return [], []
	boxes = np.array(bounding_boxes)

	#取出候选框的左上角坐标、右下角坐标和得分,并计算它的面积
	x1 = boxes[:, 0]
	y1 = boxes[:, 1]
	x2 = boxes[:, 2]
	y2 = boxes[:, 3]
	scores = boxes[:, 4]
	areas = (x2 - x1 + 1) * (y2 - y1 + 1)
	
	#对置信度得分情况进行排序,获取排序后的下标序号
	order = np.argsort(scores) #argsort默认从小到大排序
	
	tmp = [] #新建一个列表
    while order.size > 0:
        i = order[-1] #将置信度最大的框增加到tmp列表中
        tmp.append(boxes[i])

		#获取当前置信度最大的候选框与其他任意候选框的相交面积
        xx1 = np.maximum(x1[i], x1[order[:-1]])
        yy1 = np.maximum(y1[i], y1[order[:-1]])
        xx2 = np.minimum(x2[i], y2[order[:-1]])
        yy2 = np.minimum(y2[i], y2[order[:-1]])

		#宽和高不能有负值
        w = np.maximum(0.0, xx2 - xx1 + 1) 
        h = np.maximum(0.0, yy2 - yy1 + 1)
        inter = w * h  #求出相交面积
        #求出当前置信度最大的候选框与其他任意候选框iou值
        ious = inter / (areas[i] + areas[order[:-1]]-inter) 
        inds = np.where(ious <= Nt)
        order = order[inds]
    return tmp
def main():
	bounding_boxes = [[100, 100, 200, 200, 0.5],
             [120, 120, 220, 220, 0.7],
             [91, 94, 171, 181, 0.8],
             [95, 96, 162, 182, 0.77],
             [99, 101, 146, 150, 0.79],
             [98, 111, 159, 202, 0.8]]
    ret = nms(bounding_boxes, 0.5)
    print(ret)

if __name__ == "__main__":
    main()

代码运行完结果为:

[array([ 98. , 111. , 159. , 202. ,   0.8]), array([ 99.  , 101.  , 146.  , 150.  ,   0.79]), array([120. , 120. , 220. , 220. ,   0.7])]

参考博客:https://blog.csdn.net/WYKB_Mr_Q/article/details/118370784?spm=1001.2014.3001.5501
主要是记录一下自己平时遇到的问题,和大家分享一下
如有侵犯,请联系我

点个赞支持一下吧

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值