NMS(openCV API)

7 篇文章 1 订阅
7 篇文章 0 订阅

上周面试让我手写 nms , 写的马马虎虎吧… 计算 iou 差点儿没写出来…
先上一下 进行目标检测 bbox nms 的 OpenCV API

def NMSBoxes(bboxes, scores, score_threshold, nms_threshold, eta=None, top_k=None): # real signature unknown; restored from __doc__
    """
    NMSBoxes(bboxes, scores, score_threshold, nms_threshold[, eta[, top_k]]) -> indices
    .   @brief Performs non maximum suppression given boxes and corresponding scores.
    .   
    .   * @param bboxes a set of bounding boxes to apply NMS.
    .   * @param scores a set of corresponding confidences.
    .   * @param score_threshold a threshold used to filter boxes by score.
    .   * @param nms_threshold a threshold used in non maximum suppression.
    .   * @param indices the kept indices of bboxes after NMS.
    .   * @param eta a coefficient in adaptive threshold formula: \f$nms\_threshold_{i+1}=eta\cdot nms\_threshold_i\f$.
    .   * @param top_k if `>0`, keep at most @p top_k picked indices.
    """
  • bboxes 是目标检测bbox, shape是[m, 4],bbox是left, top, width, height
  • scores 是每个bbox的置信度,shape是(m,)
  • score_threshold 是置信度阈值
  • nms_threshold 是两个框之间iou的阈值
  • eta 自适应阈值公式中的系数: n m s _ t h r e s h o l d i + 1 = e t a ⋅ n m s _ t h r e s h o l d i nms\_threshold_{i+1}=eta\cdot nms\_threshold_i nms_thresholdi+1=etanms_thresholdi (我不太懂)
  • top_k 最多保留 top_k 个选择的索引

关于bboxes参数为何是左上角坐标与wh,我是这么看的:
点开文档:
https://docs.opencv.org/4.6.0/d6/d0f/group__dnn.html
NMSBoxes:

会发现有三个:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
C++中根据bboxes的类型,来判断到底用哪个,第三个是旋转目标检测的
这篇文章说:
https://blog.csdn.net/hjxu2016/article/details/103516407

cv::Rect 有很多别名,包括cv::Rect2d

cv::Rect 定义中:
https://docs.opencv.org/4.6.0/d2/d44/classcv_1_1Rect__.html
在这里插入图片描述
要传入左上角坐标和wh,其实具体还要看PythonAPI是怎么写的,但是基本这里就可以判断Python接口,NMSBoxes第一个参数需要传入 left, top, width, height样的bbox

给个demo:

import numpy as np
import cv2
if __name__ == '__main__':
    
    dets = np.array([[100,120,170,200,0.98],
                     [20,40,80,90,0.99],
                     [20,38,82,88,0.96],
                     [200,380,282,488,0.9],
                     [19,38,75,91, 0.8]])

    nms_idx = cv2.dnn.NMSBoxes(dets[:, :-1], dets[:, -1], 0.5, 0.5, top_k=2)
    print(dets[nms_idx])
    print(nms_idx)
[[ 20.    40.    80.    90.     0.99]
 [100.   120.   170.   200.     0.98]]
[1 0]

另外说一下,旋转目标检测有旋转过的bbox,也要计算iou, 进行NMS,在OpenCV中是这个API: cv2.dnn.NMSBoxesRotated,来看下这个cv::RotatedRect 的定义:
https://docs.opencv.org/4.6.0/db/dd6/classcv_1_1RotatedRect.html#ae1be388780b8d5faf450be18cbbf30f1

在这里插入图片描述

参数需要中心,wh, 以及对应的角度

哦对了,这里也有softnms的实现,这是我没想到的:
https://docs.opencv.org/4.6.0/d6/d0f/group__dnn.html#gac568f9e7aac314c09ddbc73ab4dc4814

在这里插入图片描述

下一篇写Python实现:
https://blog.csdn.net/HaoZiHuang/article/details/126463032

有参考自:
https://blog.csdn.net/yapifeitu/article/details/105703625

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值