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

words written in front

I used the labelme labeling software to label the outline of the sow in the picture . The generated json file contains 66 coordinates of the outline points of the sow , which need to be converted into mask format and used in the cv2 in the opencv-python library . fillPoly function

original code

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

run error

    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'

Code after change

Note: json_data['shapes'][17]['points']It is the xy coordinates of 66 contour points, it is a list, and the dimension is (66,2), you need to add a dimension to convert (1,66,2) into a polygon, here you need to convert the list into a matrix .

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))

run successfully

insert image description here

Single-channel run results

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])

insert image description here

Replenish

If it dtype=np.uint32will be the following mistake

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

report error

TypeError: Expected Ptr<cv::UMat> for argument 'pts'

Guess you like

Origin blog.csdn.net/weixin_42899627/article/details/127640396