码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 从二值 Mask 获取外接矩形坐标


    从二值 Mask 获取外接矩形坐标

    语雀:https://www.yuque.com/lart/blog/lxnpun

    Numpy-Based

    np.where(condition) / np.nonzero(condition)

    • https://numpy.org/doc/stable/reference/generated/numpy.where.html
    • https://numpy.org/doc/stable/reference/generated/numpy.nonzero.html#numpy.nonzero

    该方法只适用于获得标准的,即非倾斜的外接矩形。

    这里的 condition 可以使自定义的等式,或者直接是一个确定的数组。使用后者时返回每个不等于 0 的位置的坐标。该坐标结果包含两个数组,为了便于理解这两个数组的含义,可以参考官方提供的例子:

    >>> x = np.array([[3, 0, 0], [0, 4, 0], [5, 6, 0]])
    >>> x
    array([[3, 0, 0],
           [0, 4, 0],
           [5, 6, 0]])
    >>> np.nonzero(x)
    (array([0, 1, 2, 2]), array([0, 1, 0, 1]))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    可以看到,返回的两个数组,第一个是第 0 维度的坐标,第二个是第 1 维度的坐标。也就是 按照索引维度由前向后依此对应。

    y_coords, x_coords = np.nonzero(ori_mask)  
    x_min = x_coords.min()  
    x_max = x_coords.max()  
    y_min = y_coords.min()  
    y_max = y_coords.max()
    
    • 1
    • 2
    • 3
    • 4
    • 5

    由于会返回非零区域内像素坐标,所以同时这两个函数可以被用来计算质心坐标,可见我的另一篇文档:https://www.yuque.com/lart/blog/gpbigm

    OpenCV-Based

    cv2.minAreaRect(contour)

    • https://docs.opencv.org/4.5.5/d3/dc0/group__imgproc__shape.html#ga3d476a3417130ae5154aea421ca7ead9

    这可以获得最小的外接矩形,其会得到一个尽可能保卫对应轮廓的矩形,甚至是倾斜的。

    核心代码为:

    """
    rect[0]返回矩形的中心点,(x,y),实际上为y行x列的像素点
    rect[1]返回矩形的长和宽,顺序一定不要弄错了,在旋转角度上有很重要的作用
    rect[2]返回矩形的旋转角度,角度范围是[-90,0)
    """
    rect = cv2.minAreaRect(contour)
    box = cv2.boxPoints(rect) # 将边界框表示转化为四点坐标形式
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    一个典型的例子如下,要注意这里 cv2.minAreaRect 在返回点的时候的顺序有些特别。一般情况下最好不要对点的顺序有特别的要求。

    def detect_rect(contours):
        squares = []
        for c in contours:
            rect = cv2.minAreaRect(c)
            """
            The lowest point of the rectangle(does not matter left or right) 
            will always be the first sub-list of the "box" ndarray. 
            Now this point will be the reference point to decide 
            what the next sub-list represents. 
            Meaning, the next sub-list will always represent the point 
            that you first get when you move in the clockwise direction.
            """
            box = cv2.boxPoints(rect)  
            squares.append(clockwise_box)
        return np.asarray(squares, dtype=np.int32)
    
    contours, hierarchy = cv2.findContours(image, mode=cv2.RETR_LIST, method=cv2.CHAIN_APPROX_SIMPLE)
    squares = detect_rect(contours=contours)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
  • 相关阅读:
    IP-Guard如何禁止运行U盘程序
    【附源码】计算机毕业设计JAVA合租吧管理系统
    JS构造函数与class类中的封装、继承和多态
    0110闭区间上连续函数的性质-函数与极限-高等数学
    了解 NFT 质押:Web3 中赚取被动收益的另一种方式
    反射——面试常问
    TypeScript 学习笔记
    【英语:基础高阶_全场景覆盖表达】K12.口语主题陈述——教育类
    S32K144芯片焊接完成后使用S32DS初次下载无法下载解决方法
    大端与小端
  • 原文地址:https://blog.csdn.net/P_LarT/article/details/126604438
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | 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号