在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
如果对您有帮助的话,就帮忙点个👍吧。