用Numpy实现NMS(非极大值抑制)

本文探讨了非极大值抑制(NMS)算法中两个关键步骤的原因:计算边界框面积时加1的操作,以及使用np.where时仅考虑第一维度的原因。通过具体示例解释了这些操作背后的逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有两个问题没有思考的很好,望各位可以留言讨论!
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]

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值