0

I got this script from internet when i run it i got some errors regarding this lines can any one help ? i dont know where is the error exactly i was searching on internet for one day to get a similar error.

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)

at the end i got this error

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>
zteffi
  • 660
  • 5
  • 18
xCCIE
  • 53
  • 5

1 Answers1

2

The error is due to a different version of openCV, where findContours returns 3 values, where your version returns 2 values.

Change
img,c,h=cv2.findContours(dilated,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

to
c,h=cv2.findContours(dilated,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

and it should work.

For completeness (since you tagged openCV3.1): up to openCV version 3.1 findContours returned 2 values, versions 3.2 through 3.6 returned 3 values, and from version 4.0 it returns 2 values again.

J.D.
  • 4,511
  • 2
  • 7
  • 20