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)
Run Code Online (Sandbox Code Playgroud)
当测试去那里时,出现错误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))
"因为我的opencv是3.0.0而findContours返回两个值" - 你错了:
>>> help(cv2.findContours)
Help on built-in function findContours:
findContours(...)
findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> image, contours, hierarchy
Run Code Online (Sandbox Code Playgroud)
看,它返回3个值,一个额外的图像(你应该丢弃)