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

ValueError when using cv2.findContours function in python

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 return 两个值如中所述文档及如何解决这个问题

打印阈值函数的结果后,return就是这个

(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))

"since my opencv is 3.0.0 and the findContours return two values" - 你 wrong 关于这个:

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

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

看,它 returns 3 个值,一个附加图像(您应该丢弃)