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'

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

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

查看**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

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

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

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

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

参考文献

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

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

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Trouble..

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值