changeset 760:d72e4bcc1e36 dev

added functionality to generate avi from the undistorted videos
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 04 Nov 2015 17:44:07 -0500
parents a05b70f307dd
children 15ddc8715236
files scripts/undistort-video.py
diffstat 1 files changed, 23 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/undistort-video.py	Wed Nov 04 17:12:06 2015 -0500
+++ b/scripts/undistort-video.py	Wed Nov 04 17:44:07 2015 -0500
@@ -7,6 +7,7 @@
 
 import cvutils
 from math import ceil, log10
+from os import path, mkdir
 
 parser = argparse.ArgumentParser(description='''The program converts a video into a series of images corrected for distortion. One can then use mencoder to generate a movie, eg
 $ mencoder 'mf://./*.png' -mf fps=[framerate]:type=png -ovc xvid -xvidencopts bitrate=[bitrate] -nosound -o [output.avi]''')
@@ -17,6 +18,10 @@
 parser.add_argument('--undistorted-multiplication', dest = 'undistortedImageMultiplication', help = 'undistorted image multiplication', type = float)
 parser.add_argument('-f', dest = 'firstFrameNum', help = 'number of first frame number to display', type = int)
 parser.add_argument('-l', dest = 'lastFrameNum', help = 'number of last frame number to save', type = int)
+parser.add_argument('-d', dest = 'destinationDirname', help = 'name of the directory where the undistorted frames are saved')
+parser.add_argument('--encode', dest = 'encodeVideo', help = 'indicate if video is generated at the end (default Xvid)', action = 'store_true')
+parser.add_argument('--fps', dest = 'fps', help = 'frame per second of the output video file if encoding', type = float, default = 30)
+parser.add_argument('--bitrate', dest = 'bitrate', help = 'bitrate of the output video file if encoding', type = int, default = 5000)
 
 args = parser.parse_args()
 
@@ -24,6 +29,15 @@
 #distortionCoefficients = args.distortionCoefficients
 #undistortedImageMultiplication = args.undistortedImageMultiplication
 #firstFrameNum = params.firstFrameNum
+if args.destinationDirname is None:
+    destinationDirname = ''
+else:
+    if not args.destinationDirname.endswith('/'):
+        destinationDirname = args.destinationDirname+'/'
+    else:
+        destinationDirname = args.destinationDirname
+    if not path.exists(destinationDirname):
+        mkdir(destinationDirname)
 
 capture = cv2.VideoCapture(args.videoFilename)
 width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH))
@@ -43,5 +57,13 @@
             ret, img = capture.read()
             if ret:
                 img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR)
-                cv2.imwrite('undistorted-{{:0{}}}.png'.format(nZerosFilename).format(frameNum), img)
+                cv2.imwrite(destinationDirname+'undistorted-{{:0{}}}.png'.format(nZerosFilename).format(frameNum), img)
             frameNum += 1
+
+if args.encodeVideo:
+    print('Encoding the images files in video')
+    from subprocess import check_call
+    from storage import openCheck
+    out = openCheck("err.log", "w")
+    check_call("mencoder \'mf://"+destinationDirname+"*.png\' -mf fps={}:type=png -ovc xvid -xvidencopts bitrate={} -nosound -o ".format(args.fps, args.bitrate)+destinationDirname+"undistort.avi", stderr = out, shell = True)
+    out.close()