diff python/cvutils.py @ 801:c5f98916297e

merged with dev branch
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 31 May 2016 17:07:23 -0400
parents 0662c87a61c9
children 52aa03260f03
line wrap: on
line diff
--- a/python/cvutils.py	Mon May 09 15:33:11 2016 -0400
+++ b/python/cvutils.py	Tue May 31 17:07:23 2016 -0400
@@ -218,31 +218,33 @@
         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 and frameNum<rawCount:
                 ret, img = capture.read()
                 i = 0
                 while not ret and i<10:
                     ret, img = capture.read()
                     i += 1
-                if img.size>0:
+                if img is not None and 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))