Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.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 OpenCV等高线-需要2个以上的值才能解包_Python_Python 2.7_Opencv - Fatal编程技术网

Python OpenCV等高线-需要2个以上的值才能解包

Python OpenCV等高线-需要2个以上的值才能解包,python,python-2.7,opencv,Python,Python 2.7,Opencv,我正在尝试使用以下代码实现轮廓 im = cv2.imread('C:\Users\Prashant\Desktop\T.jpg') imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(imgray,127,255,0) image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) i

我正在尝试使用以下代码实现轮廓

im = cv2.imread('C:\Users\Prashant\Desktop\T.jpg')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
img = cv2.drawContour(im, contours, -1, (0,255,0), 3)
cv2.imshow('Image1',img)
但是我不断地得到以下错误

Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile
    execfile(filename, namespace)
  File "C:/Users/Prashant/.spyder2/.temp.py", line 17, in <module>
    image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
ValueError: need more than 2 values to unpack
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“C:\Python27\lib\site packages\spyderlib\widgets\externalshell\sitecustomize.py”,第540行,在runfile中
execfile(文件名、命名空间)
文件“C:/Users/Prashant/.spyder2/.temp.py”,第17行,在
图像、轮廓、层次=cv2.findContours(阈值、cv2.RETR\u树、cv2.CHAIN\u近似值、简单值)
ValueError:需要2个以上的值才能解包
函数findContours是否需要更多参数? 我能做些什么来纠正它。

在OpenCV 2中,只返回两个值,
轮廓
层次
。当python尝试将这两个值分配给此语句左侧给出的三个名称时,会发生错误:

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

findContoursopencv3中仅返回三个值图像、轮廓和层次

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

它现在返回三个值:

findContours(image, mode, method[, contours[, hierarchy[, offset]]])
返回图像、轮廓、层次结构
  • findContours只返回两个值。所以就用,
  • 所以使用

    contours, hierarchy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    
    这将有助于:

    image, contours, hierarchy = cv2.findContours(thresh.copy(),
                                                  cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    
    Python版本2.7.14(v2.7.14:84471935ed,2017年9月16日,20:25:58)[MSC v.1500 64位(AMD64)]

    NumPy版本:1.16.1

    argparse版本:1.1

    CV2版本:4.0.0

    Traceback (most recent call last):
    
      File "omr.py", line 254, in <module>
    
        main()
    
      File "omr.py", line 237, in main
    
        answers, im = get_answers(args.input)
    
      File "omr.py", line 188, in get_answers
    
        contours = get_contours(im)
    
      File "omr.py", line 26, in get_contours
    
        im2, contours, hierarchy =cv2.findContours(image_gray,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    
    ValueError: need more than 2 values to unpack
    

    同时升级您的OpenCv版本

    自2019年起,我们有三个版本的OpenCv(OpenCV2、OpenCV3和OpenCV4)

    OpenCV4和OpenCV2具有类似的行为(从
    cv2.findContours
    返回两个值)。而OpenCV3返回三个值

    if cv2.getVersionMajor() in [2, 4]:
        # OpenCV 2, OpenCV 4 case
        contour, hier = cv2.findContours(
                        thresh.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
    else:
        # OpenCV 3 case
        image, contour, hier = cv2.findContours(
                        thresh.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
    

    根据OpenCV版本的不同,
    cv2.findContours()
    具有不同的返回签名。在OpenCV 3.4.X中,返回3项。在OpenCV 2.X和4.1.X中,返回2项

    无论版本如何,都可以轻松获得轮廓,如下所示:

    cnts = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    

    根据当前Opencv版本cv2。findContours返回2个值,即轮廓和继承人。轮廓可以简单地解释为连接所有连续点(沿边界)的曲线,具有相同的颜色或强度。轮廓是形状分析、目标检测和识别的有用工具。

    谢谢Warren。这很有帮助。:)对于OpenCV 3.0-beta版或更高版本,您的语法实际上是正确的,但您可能使用的是稳定的2.8版或其他版本。不同版本的OpenCV(OpenCV2、3、4)的行为是不同的。我已经在下面详细回答了。结果将保存在contoursCode中,但不鼓励回答。请点击并添加一些文字,总结您的代码如何解决问题,或者解释您的答案与之前的答案有何不同。感谢我有opencv3.4.4,我已经写了三个返回值,它仍然得到错误,轮廓,层次=cv2.findContours(thresh,cv2.RETR\u CCOMP,cv2.CHAIN\u近似无)cv2.error:OpenCV(3.4.4)C:\projects\OpenCV python\OpenCV\modules\imgproc\src\cours.cpp:195:error:(-210:不支持的格式或格式组合)[Start]FindContours在模式下仅支持CV_8UC1图像!=CV_RETR_FLOODFILL仅在函数“cvStartFindContours_Impl”中支持CV_32SC1图像。如何修复?
    cnts = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]