Ask Your Question
0

Use contours in a video?

asked 2015-11-23 04:27:48 -0600

arp1561 gravatar image

This is the program I use for drawing all the contours around an image

                import numpy as np
                import cv2
                im = cv2.imread('test.jpg')
                imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
                ret,thresh = cv2.threshold(imgray,127,255,0)
                im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

For drawing contours in a video, I use this program.

                import cv2
                cap = cv2.VideoCapture(0)
                while True:
                          ret,frame=cap.read()
                          gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
                          ret,thresh = cv2.threshold(gray,140,255,0)
                          image,contours,hie = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
                          frame = cv2.drawContours(frame, contours, 3, (0,255,0), 3)
                          cv2.imshow("vid",frame)
                          if cv2.waitKey(1) & 0xFF == ord('q'):
                                           break

cap.release() cv2.destroyAllWindows()

Now the problem is that when I run the second program, there are multiple errors

1-> Need more than 2 values to unpack.

2-> When I remove the image parameter from the findContours(), and try to display, it still gives me an error.

Can someone please explain to me why this is happening?

Thanks in advance! :)

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-04-28 12:02:27 -0600

Hi I came to know this Question was asked on 2014. But I am in opencv from last month. May this will help others in future.

import numpy as np
import cv2


cap = cv2.VideoCapture("mydata/video.avi")

while(True):
  # Capture frame-by-frame
   ret, frame = cap.read()

   # Our operations on the frame come here
   gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
   blur = cv2.GaussianBlur(gray,(5,5),0)
   ret, thresh_img = cv2.threshold(blur,91,255,cv2.THRESH_BINARY)

    contours =  cv2.findContours(thresh_img,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)[-2]
    for c in contours:
        cv2.drawContours(frame, [c], -1, (0,255,0), 3)

     # Display the resulting frame
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-11-23 04:27:48 -0600

Seen: 8,170 times

Last updated: Nov 23 '15