码农知识堂 - 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

  • 相关阅读:
    Unity之MVC思想(通过普通方法和使用MVC思想完成同一个小案例:掌握MVC简单框架)
    python高级进阶
    ggcor替代包:linkET,相关图,mantel test可视化
    Typora mac新手入门教程
    nmake简介
    Linux_C_入门篇学习笔记
    Vue项目实战——【基于 Vue3.x + NodeJS】实现的课程表排课系统二(week-title)
    Flink报错could not be loaded due to a linkage failure
    HBase入门至进阶以及开发等知识梳理
    广州蓝景分享—程序员在面试时必须要做的五件事,最后一件很重要
  • 原文地址:https://blog.csdn.net/HaoZiHuang/article/details/126460067
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号