Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 想要找到轮廓->;ValueError:没有足够的值来解包(预期为3,得到2),出现此错误_Python_Python 3.x_Opencv Contour - Fatal编程技术网

Python 想要找到轮廓->;ValueError:没有足够的值来解包(预期为3,得到2),出现此错误

Python 想要找到轮廓->;ValueError:没有足够的值来解包(预期为3,得到2),出现此错误,python,python-3.x,opencv-contour,Python,Python 3.x,Opencv Contour,我的简单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(thres

我的简单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)
这很有效。但是最近我甚至没有做任何改变。这是扔给我的

ret、等高线、层次=cv2.查找到的多边形(阈值、cv2.RETR_树、cv2.链近似_简单)

ValueError:没有足够的值来解包(预期为3,实际为2)

帮帮我,伙计们


谢谢。

函数
cv2.findContours()
已更改为仅返回轮廓和层次结构,而不返回ret

您应该将其更改为:

contours,hierachy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
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)

这为您提供了一个可以在最新或更旧版本的OpenCV上运行的代码。

谢谢,但这项更改是在什么版本中引入的?因此,它们再次破坏了兼容性,但python模块仍然是cv2。@Osman pasha在这个上下文中是谁?另外,cv2中的2只是一个名称空间,与实际版本无关。他们,就像OpenCV的开发者一样,实际上遵循语义版本控制,这种变化是在将主要版本从3.x更改为4.x时引入的,这很好。您在“再次破坏兼容性”中设置的语气表明,您可能不喜欢正确的处理方式。为了回答您的问题,它是在4.0版中引入的,是的,它仍然使用
cv2
名称空间。@swalog是的,您很明白我的意思。你看,互联网上的很多例子都没有提到使用了什么OpenCV版本,只是告诉你安装OpenCV(或者什么都不说)。突然之间,这些示例不起作用了,因为API已经改变了。如果他们(是的,OpenCV开发人员)将cv2用于OpenCV 2,将cv3用于OpenCV 3等等,那么就不会有这样的混淆,因为从代码中可以清楚地看出使用哪个主要版本。我确信他们有理由保留CV2,它更与C++ API相关,然后与主要版本相关,但是追溯到我看来这是一个糟糕的下降。这能回答你的问题吗?