changeset 396:167f6ec44ec5

cleaned function to save images
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Sun, 28 Jul 2013 16:30:11 -0400
parents 6fba1ab040f1
children b36b00dd27c3
files python/cvutils.py
diffstat 1 files changed, 11 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/python/cvutils.py	Fri Jul 26 17:52:23 2013 -0400
+++ b/python/cvutils.py	Sun Jul 28 16:30:11 2013 -0400
@@ -135,25 +135,29 @@
                     key = cv2.waitKey(wait)
             cv2.destroyAllWindows()
 
-    def getImagesFromVideo(filename, nImages = 1, saveImage = False):
-        '''Returns nImages images from the video sequence'''
+    def getImagesFromVideo(videoFilename, firstFrameNum = 0, nFrames = 1, saveImage = False, outputPrefix = 'image'):
+        '''Returns nFrames images from the video sequence'''
+        from math import floor, log10
         images = []
-        capture = cv2.VideoCapture(filename)
+        capture = cv2.VideoCapture(videoFilename)
+        nDigits = int(floor(log10(capture.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))))+1
         if capture.isOpened():        
             ret = False
-            numImg = 0
-            while numImg<nImages:
+            capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum)
+            imgNum = 0
+            while imgNum<nFrames:
                 ret, img = capture.read()
                 i = 0
                 while not ret and i<10:
                     ret, img = capture.read()
                     i += 1
                 if img.size>0:
-                    numImg +=1
                     if saveImage:
-                        cv2.imwrite('image{0:04d}.png'.format(numImg), img)
+                        imgNumStr = format(firstFrameNum+imgNum, '0{}d'.format(nDigits))
+                        cv2.imwrite(outputPrefix+imgNumStr+'.png', img)
                     else:
                         images.append(img)
+                    imgNum +=1
         return images
 
     def displayTrajectories(videoFilename, objects, boundingBoxes, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True, rescale = 1.):