码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • NMS(openCV API)


    上周面试让我手写 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.
        """
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 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​=eta⋅nms_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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    [[ 20.    40.    80.    90.     0.99]
     [100.   120.   170.   200.     0.98]]
    [1 0]
    
    • 1
    • 2
    • 3

    另外说一下,旋转目标检测有旋转过的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

  • 相关阅读:
    流(43)
    luffy-(2)
    基于javaweb的在线美食分享推荐系统(java+ssm+jsp+jquery+mysql)
    OpenAI Kubernetes 相关博文读后笔记
    FPGA模块——IIC协议(FPGA做主机操作24C64)
    Spring Cloud Alibaba系列之nacos:(4)配置管理
    2023-09-20 LeetCode每日一题(拿硬币)
    JavaScript小技能:对象
    Seata中的事务分组与集群
    包管理工具--》npm的配置及使用(一)
  • 原文地址:https://blog.csdn.net/HaoZiHuang/article/details/126460067
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号