这个问题在这里已经有了答案:
Want to find contours -> ValueError: not enough values to unpack (expected 3, got 2), this appears
(2 个回答)
2年前关闭。
当我运行它时,我从互联网上得到了这个脚本,我得到了一些关于这行的错误,任何人都可以帮忙吗?
我不知道错误到底在哪里,我在互联网上搜索了一天以获得类似的错误。
import cv2
import numpy as np
def main():
#window_name="Cam feed"
#cv2.namedWindow(window_name)
cap=cv2.VideoCapture("C:\\Users\\ccie\\Desktop\\768x576.avi")
#filename = 'F:\sample.avi'
#codec=cv2.VideoWriter_fourcc('X','V','I','D')
#framerate=30
#resolution = (500,500)
# VideoFileOutput = cv2.VideoWriter(filename,codec,framerate,resolution)
if cap.isOpened():
ret,frame = cap.read()
else:
ret =False
ret,frame1 = cap.read()
ret,frame2 = cap.read()
while ret:
ret,frame = cap.read()
#VideoFileOutput.write(frame)
d=cv2.absdiff(frame1,frame2)
grey=cv2.cvtColor(d,cv2.COLOR_BGR2GRAY)
blur =cv2.GaussianBlur(grey,(5,5),0)
ret,th=cv2.threshold(blur,20,255,cv2.THRESH_BINARY)
dilated=cv2.dilate(th,np.ones((3,3),np.uint8),iterations=3)
img,c,h=cv2.findContours(dilated,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
最后我得到了这个错误
PS C:\python3.6> .\python.exe .\Contours_Opencv.py
Traceback (most recent call last):
File ".\Contours_Opencv.py", line 51, in <module>
main()
File ".\Contours_Opencv.py", line 37, in main
img,c,h=cv2.findContours(dilated,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
ValueError: not enough values to unpack (expected 3, got 2)
PS C:\python3.6>
最佳答案
该错误是由于不同版本的 openCV 造成的,其中 findContours 返回 3 个值,而您的版本返回 2 个值。
改变img,c,h=cv2.findContours(dilated,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
到c,h=cv2.findContours(dilated,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
它应该可以工作。
为了完整性(因为您标记了 openCV3.1):直到 openCV 3.1 版 findContours 返回 2 个值,3.2 到 3.6 版返回 3 个值,从 4.0 版开始它再次返回 2 个值。
关于python-3.x - 调用 cv2.findContours() 时出现 "ValueError: not enough values to unpack",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56754211/