OpenCV等高线-需要超过2个值才能取消固定

2024-04-29 00:56:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用以下代码实现等高线。。

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

findContours函数需要更多参数吗? 我能做什么来纠正它呢。


Tags: inimagetreehierarchylinecv2usersfile
3条回答

在OpenCV 2中,^{}只返回两个值,contourshierarchy。当python试图将这两个值赋给此语句左侧给出的三个名称时,将发生错误:

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

findContours只返回opencv3中的图像、轮廓和层次结构三个值

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

它现在返回三个值:

findContours(image, mode, method[, contours[, hierarchy[, offset]]])

返回图像、轮廓、层次

相关问题 更多 >