changeset 798:5b99b676265e dev

modified to get images very time step
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 30 May 2016 22:49:24 -0400
parents 2ffaf1a7cde9
children 0662c87a61c9
files python/cvutils.py
diffstat 1 files changed, 9 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/python/cvutils.py	Mon May 23 23:55:06 2016 -0400
+++ b/python/cvutils.py	Mon May 30 22:49:24 2016 -0400
@@ -218,19 +218,19 @@
         else:
             print('Video capture for {} failed'.format(filename))
 
-    def getImagesFromVideo(videoFilename, firstFrameNum = 0, nFrames = 1, saveImage = False, outputPrefix = 'image'):
+    def getImagesFromVideo(videoFilename, firstFrameNum = 0, lastFrameNum = 1, step = 1, saveImage = False, outputPrefix = 'image'):
         '''Returns nFrames images from the video sequence'''
         images = []
         capture = cv2.VideoCapture(videoFilename)
         if capture.isOpened():
             rawCount = capture.get(cv2.CAP_PROP_FRAME_COUNT)
             if rawCount < 0:
-                rawCount = firstFrameNum+nFrames+1
+                rawCount = lastFrameNum+1
             nDigits = int(floor(log10(rawCount)))+1
             ret = False
             capture.set(cv2.CAP_PROP_POS_FRAMES, firstFrameNum)
-            imgNum = 0
-            while imgNum<nFrames:
+            frameNum = firstFrameNum
+            while frameNum<=lastFrameNum:
                 ret, img = capture.read()
                 i = 0
                 while not ret and i<10:
@@ -238,11 +238,13 @@
                     i += 1
                 if img.size>0:
                     if saveImage:
-                        imgNumStr = format(firstFrameNum+imgNum, '0{}d'.format(nDigits))
-                        cv2.imwrite(outputPrefix+imgNumStr+'.png', img)
+                        frameNumStr = format(frameNum, '0{}d'.format(nDigits))
+                        cv2.imwrite(outputPrefix+frameNumStr+'.png', img)
                     else:
                         images.append(img)
-                    imgNum +=1
+                    frameNum +=step
+                    if step > 1:
+                        capture.set(cv2.CAP_PROP_POS_FRAMES, frameNum)
             capture.release()
         else:
             print('Video capture for {} failed'.format(videoFilename))