码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • cv2.fillPoly的大坑


    在labelme标注图像后,想将其json文件转化为png的图像,我们这里使用cv2.fillPoly()进行转化,但是遇到了一个巨坑,问题描述如下所示:

    points = []
    for shape in shapes:
         points.append(np.array(shape['points'],np.int8))
    image = cv2.fillPoly(image, points, color=(255, 255, 255))
    cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-n4ekh6o5\opencv\modules\imgproc\src\drawing.cpp:2395: error: (-215:Assertion failed) p.checkVector(2, CV_32S) >= 0 in function 'cv::fillPoly'
    
    • 1
    • 2
    • 3
    • 4
    • 5

    传入的points数据形式为以下形式:

    points=[[[250, 200], [300, 100], [750, 800], [100, 1000]],[[1000, 200], [1500, 200], [1500, 400], [1000, 400]]]
    
    • 1

    查看**cv2.fillPoly()**源代码之后,发现其实目前的传入数据格式是没有问题的。

    def fillPoly(img, pts, color, lineType=None, shift=None, offset=None): # real signature unknown; restored from __doc__
        """
        fillPoly(img, pts, color[, lineType[, shift[, offset]]]) -> img
        .   @brief Fills the area bounded by one or more polygons.
        .   
        .   The function cv::fillPoly fills an area bounded by several polygonal contours. The function can fill
        .   complex areas, for example, areas with holes, contours with self-intersections (some of their
        .   parts), and so forth.
        .   
        .   @param img Image.
        .   @param pts Array of polygons where each polygon is represented as an array of points.
        .   @param color Polygon color.
        .   @param lineType Type of the polygon boundaries. See #LineTypes
        .   @param shift Number of fractional bits in the vertex coordinates.
        .   @param offset Optional offset of all points of the contours.
        """
        pass
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    在网上查阅一堆资料后,在网上查看相关的问题描述,但是没有找到解决方案。甚至开始怀疑数据格式的问题,在经过一番调试后,终于发现问题的所在:

    points.append(np.array(shape['points'],np.int8))
    
    • 1

    这行代码看起来没有问题,但是问题就出在np.int8,有些小数据可能不会出现问题,但是对于大图像标注来说,它不能满足数据的存储空间,所以将其改为以下形式及就没有出现问题了:

    points.append(np.array(shape['points'], np.int32))
    
    • 1

    参考文献

    https://blog.csdn.net/u012135425/article/details/84983265

    如果对您有帮助的话,就帮忙点个👍吧。

  • 相关阅读:
    事业编招聘:雄安新区公开选聘专业骨干人才
    Jnekins Active动态参数 集成Gitlab实践
    会议OA项目(送审后审批人签字功能)
    mysqldump数据备份
    人工智能大模型之开源大语言模型汇总(国内外开源项目模型汇总)
    基于springboot+vue的纺织品企业财务管理系统
    【Linux】【操作】Linux操作集锦系列之四——获取磁盘空间相关信息之(一)命令行方式
    问题解决:MapReduce输出结果乱码(Eclipse)
    Python GRPC 10min掌握使用
    Java创建对象的最佳方式:单例模式(Singleton)
  • 原文地址:https://blog.csdn.net/qq_45041871/article/details/128079313
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | 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号