TypeError: Layout of the output array image is incompatible 问题解决
本周在使用findContours的过程中遇到了以下问题: TypeError: Layout of the output array image is incompatible with cv::Mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels) 感谢这篇文章: https://www.jianshu.com/p/cc3f4baf35bb ,引导我往正确的方向进行思考。 先讲结论造成以上问题的原因是: 输入与输出的numpy格式不一致 。 我一开始的第一反应是为什么findContours函数会有“output array image”,我只关注contours。这其实是我在写代码中一个盲区,对于findContours函数我一般都是这么写: _, contours, _ = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 对于第一个与第三个返回值现阶段直接就跳过,这其实恰恰是产生问题的关键原因,完整的findContours函数输出应该如下: binary, contours, hierarchy = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN