在python中使用cv2.findContours函数时的ValueError

时间:2015-03-17 11:33:03

标签: python opencv

import cv2
import numpy as np 

img = cv2.imread("img.jpg")
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 127, 255,0)
contours,hierarchy = cv2.findContours(thresh,2,1)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "test.py", line 7, in <module>
    contours,hierarchy = cv2.findContours(thresh,2,1)

当测试去了那里时,发生了一个错误ValueError: too many values to unpack,任何人都可以告诉我为什么它发生了,因为我的opencv是3.0.0而findContours返回两个值,如文档中所述,以及如何解决这个问题问题

打印出阈值功能的结果后,返回

(127.0, array([[255, 255, 255, ..., 255, 255, 255], [255, 255, 255, ..., 255, 255, 255], [255, 255, 255, ..., 255, 255, 255], ..., [255, 255, 255, ..., 255, 255, 255], [255, 255, 255, ..., 255, 255, 255], [255, 255, 255, ..., 255, 255, 255]], dtype=uint8))

1 个答案:

答案 0 :(得分:5)

&#34;因为我的opencv是3.0.0而findContours返回两个值&#34; - 你关于这个问题wrong

>>> help(cv2.findContours)
Help on built-in function findContours:

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

看,它返回3个值,一个额外的图像(你应该丢弃)