报错 error: (-215:Assertion failed) p.checkVector(2, CV_32S) >= 0 in function ‘fillPoly‘

本文介绍如何使用labelme软件标注母猪轮廓,并通过opencv-python将轮廓点坐标转换为mask格式。解决cv2.fillPoly函数在使用过程中的常见错误。

写在前面的话

我用labelme标注软件对图中的母猪的轮廓进行了标注,生成的json文件包含的是母猪的轮廓点的66个坐标,需要转成mask格式,用到opencv-python库里面的cv2.fillPoly函数

原始代码

seg_img = np.zeros_like(image), dtype=np.int8)#黑底
cv2.fillPoly(seg_img, np.array(json_data['shapes'][17]['points'], dtype=np.int32), (255,255,255))#白色mask

运行报错

    cv2.fillPoly(seg_img, np.array(json_data['shapes'][17]['points'], dtype=np.int32), (255,255,255))
cv2.error: OpenCV(4.5.1) /tmp/pip-req-build-ms668fyv/opencv/modules/imgproc/src/drawing.cpp:2395: error: (-215:Assertion failed) p.checkVector(2, CV_32S) >= 0 in function 'fillPoly'

更改后代码

注意:json_data['shapes'][17]['points']是66个轮廓点的xy坐标,是一个列表,维度是(66,2),需要增加一个维度转成(1,66,2)表示为一个多边形才可以,这里需要将列表转成矩阵。

seg_img = np.zeros_like(image), dtype=np.int8)#黑底
cv2.fillPoly(seg_img, np.array([json_data['shapes'][17]['points']], dtype=np.int32), (255,255,255))

运行成功

在这里插入图片描述

单通道的运行结果

seg_img = np.zeros((image.shape[0], image.shape[1]), dtype=np.int8)
cv2.fillPoly(seg_img, np.array([json_data['shapes'][17]['points']], dtype=np.int32), [255])

在这里插入图片描述

补充

若是 dtype=np.uint32 会下面那个错

cv2.fillPoly(seg_img, np.array([json_data['shapes'][17]['points']], dtype=np.uint32), (255,255,255))

报错

TypeError: Expected Ptr<cv::UMat> for argument 'pts'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

G果

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

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

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

打赏作者

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

抵扣说明:

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

余额充值