python - findContours是否有理由给我一个错误,尽管它不应该这样吗?

标签 python opencv

---------------------------------------------------------------------------

ValueError跟踪(最近一次通话)

1 im = image_list[4] 2 ret,thresh = cv2.threshold(im,127,255,0) ----> 3 image, contours, hierarchy = cv2.findContours(thresh , cv2.RETR_TREE , cv2.CHAIN_APPROX_SIMPLE) 4 image = cv2.drawContours(image, contours, -1, (0,255,0), 3) 5
ValueError:没有足够的值可解包(预期3,得到2)

最佳答案

不同版本的OpenCV从cv2.findContours返回不同数量的项目。

OpenCV 4和OpenCV 2具有相似的行为,返回两个项目,而OpenCV 3返回三个项目。

您的版本显然只需要2个项目。所以尝试

contours, hierarchy = cv2.findContours(thresh , cv2.RETR_TREE , cv2.CHAIN_APPROX_SIMPLE)

或者如果您想要某种版本无关的东西,那么如果您需要层次结构使用
contours = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
hierarchy = contours[1] if len(contours) == 2 else contours[2]
contours = contours[0] if len(contours) == 2 else contours[1]

或者如果您只是想要轮廓
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]

关于python - findContours是否有理由给我一个错误,尽管它不应该这样吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62272551/

相关文章:

python - 尝试通过 Python 运行宏,宏运行但在执行后崩溃并出现错误

python 读取视频

opencv - 编译libccv(静态库)时出现Opencv-错误

c++ - OpenCV + OpenGL - 获取 OpenGL 图像作为 OpenCV 相机

python - 为什么在OpenCV中为内核使用一个数组?

Python周数生成

python - 使用 print() 时如何换行和缩进长行?

python - 在 Python 中打印带分隔符的表格的最佳方法是什么

python - 在 Python 中将可变长度字符串拆分为变量的最佳方法是什么?

android - 带Camera2的Android RTL