Python 想要找到轮廓 -> ValueError: not enough values to unpack (expected 3, got 2), this出现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54164630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Want to find contours -> ValueError: not enough values to unpack (expected 3, got 2), this appears
提问by Indunil Aravinda
My simple python code is this
我的简单python代码是这样的
import cv2
img=cv2.imread('Materials/shapes.png')
blur=cv2.GaussianBlur(img,(3,3),0)
gray=cv2.cvtColor(blur,cv2.COLOR_BGR2GRAY)
returns,thresh=cv2.threshold(gray,80,255,cv2.THRESH_BINARY)
ret,contours,hierachy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
area=cv2.contourArea(cnt) #contour area
if (area>1220):
cv2.drawContours(img,[cnt],-1,(0,255,0),2)
cv2.imshow('RGB',img)
cv2.waitKey(1000)
print(len(cnt))
import numpy as np
contours=np.array(contours)
print(contours)
This worked fine. But recently without me even making any changes. This was throwed to me
这工作得很好。但最近我什至没有做出任何改变。这是扔给我的
ret,contours,hierachy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
ValueError: not enough values to unpack (expected 3, got 2)
ret,contours,hierachy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
ValueError:没有足够的值来解包(预期为 3,得到 2)
Help me guys.
帮帮我吧伙计们。
Thanks.
谢谢。
回答by Rami Isam
the function cv2.findContours()
has been changed to return only the contours and the hierarchy and not ret
该函数cv2.findContours()
已更改为仅返回轮廓和层次结构,而不返回 ret
you should change it to:
你应该把它改成:
contours,hierachy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
回答by Thomas Di Martino
Well explained in thispython code example, the best way to make your code version-proof is with this following syntax:
在这个python 代码示例中得到了很好的解释,使您的代码版本证明的最佳方法是使用以下语法:
# check OpenCV version
major = cv2.__version__.split('.')[0]
if major == '3':
ret, contours, hierarchy = cv2.findContours(im.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
else:
contours, hierarchy = cv2.findContours(im.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
This provides you a code that could run on last or older version of OpenCV.
这为您提供了可以在最新或更旧版本的 OpenCV 上运行的代码。