changeset 216:51acf43e421a

modified the function to read and save images from a movie
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 21 Jun 2012 00:57:26 -0400
parents 5e2983b05d4e
children ba71924cadf5
files python/cvutils.py
diffstat 1 files changed, 11 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/python/cvutils.py	Fri Jun 08 18:44:27 2012 -0400
+++ b/python/cvutils.py	Thu Jun 21 00:57:26 2012 -0400
@@ -103,18 +103,25 @@
                     cv2.imshow('frame', img)
                     key = cv2.waitKey(5)
 
-    def getImagesFromVideo(filename, nImages = 1):
+    def getImagesFromVideo(filename, nImages = 1, saveImage = False):
         '''Returns nImages images from the video sequence'''
         images = []
         capture = cv2.VideoCapture(filename)
         if capture.isOpened():        
             ret = False
-            while len(images)<nImages:
+            numImg = 0
+            while numImg<nImages:
                 ret, img = capture.read()
-                while not ret:
+                i = 0
+                while not ret and i<10:
                     ret, img = capture.read()
+                    i += 1
                 if img.size>0:
-                    images.append(img)
+                    numImg +=1
+                    if saveImage:
+                        cv2.imwrite('image{0:04d}.png'.format(numImg), img)
+                    else:
+                        images.append(img)
         return images
 
     def displayTrajectories(videoFilename, objects, homography = None):