TypeError: Layout of the output array image is incompatible with cv::Mat (step[ndims-1] != elemsiz

问题描述

使用opencv-pythoncv2.findContours,曝出如题错误:

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

源码:

t=cv2.findContours(segments2,mode=cv2.RETR_LIST,
    method=cv2.CHAIN_APPROX_SIMPLE)

其中,segments2numpy.ndarray 类型,代表二值图像。

解决方法

在使用segments2之前,转为无符号整型8位。

segments2=segments2.astype(np.uint8)
t=cv2.findContours(segments2,mode=cv2.RETR_LIST,
    method=cv2.CHAIN_APPROX_SIMPLE)

猜你喜欢

转载自blog.csdn.net/nima1994/article/details/81008653