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

原代码

left_bottom = [0, canny_img.shape[0]]
right_bottom = [canny_img.shape[1], canny_img.shape[0]]
apex = [canny_img.shape[1]/2, 310]
vertices = np.array([left_bottom, right_bottom, apex], dtype=np.int32)
...
cv2.fillPoly(mask, vertices, ignore_mask_color)

错误说明

Traceback (most recent call last):
File “test.py”, line 116, in
roi_image = region_of_interest(canny_img, vertices)
File “test.py”, line 45, in region_of_interest
cv2.fillPoly(mask, vertices, ignore_mask_color)
cv2.error: OpenCV(4.0.0) /io/opencv/modules/imgproc/src/drawing.cpp:2403: error: (-215:Assertion failed) p.checkVector(2, CV_32S) >= 0 in function ‘fillPoly’

错误原因

API
cv2.fillPoly()函数功能是对多个多边形边界进行填充,pts这个参数使用一个多维数组来表达多个多边形,一个多边形是一个二维数组,多个多边形就是三维数组。一开始就是给定的数组维数对不上,所以一直会报这个错误。在定义多边形时多增加一个中括号,问题立马就解决了!

修改后的代码

left_bottom = [0, canny_img.shape[0]]
right_bottom = [canny_img.shape[1], canny_img.shape[0]]
apex = [canny_img.shape[1]/2, 310]
vertices = np.array([[left_bottom, right_bottom, apex]], dtype=np.int32)
...
cv2.fillPoly(mask, vertices, ignore_mask_color)

猜你喜欢

转载自blog.csdn.net/weixin_38498050/article/details/89094297