Python OpenCV TypeError: Layout of the output array img is incompatible with cv

时间:2024-05-23 22:04:33

参考Python Opencv根据点来填充区域写出如下代码:

# -*- coding:utf-8 -*-
__author__ = 'ShawDa'

import cv2
import numpy as np


img = np.full((512, 512, 1), 255)
points = [[1, 142],[142, 142],[142, 300],[1, 142]]
res = cv2.fillPoly(img, [np.array(points)], 0)
cv2.imwrite('res.png',res)

会报错:

TypeError: Layout of the output array img is incompatible with cv::Mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels)

解决方法——设置img的dtype:

img = np.full((512, 512, 1), 255,dtype=np.int32)

既可以得到结果:

Python OpenCV TypeError: Layout of the output array img is incompatible with cv