Mercurial > hg > nsaunier > traffic-intelligence
comparison scripts/undistort-video.py @ 572:9c429c7efe89
added script to generate undistorted images from video
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Tue, 12 Aug 2014 15:33:03 -0400 |
| parents | |
| children | 3058e00887bc |
comparison
equal
deleted
inserted
replaced
| 571:a9c1d61a89b4 | 572:9c429c7efe89 |
|---|---|
| 1 #! /usr/bin/env python | |
| 2 | |
| 3 import sys, argparse | |
| 4 | |
| 5 import numpy as np | |
| 6 import cv2 | |
| 7 | |
| 8 import cvutils | |
| 9 from math import ceil, log10 | |
| 10 | |
| 11 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 | |
| 12 $ mencoder 'mf://./*.png' -mf fps=[framerate]:type=png -ovc xvid -xvidencopts bitrate=[bitrate] -nosound -o [output.avi]''') | |
| 13 | |
| 14 parser.add_argument('-i', dest = 'videoFilename', help = 'filename of the video sequence') | |
| 15 parser.add_argument('--intrinsic', dest = 'intrinsicCameraMatrixFilename', help = 'name of the intrinsic camera file') | |
| 16 parser.add_argument('--distortion-coefficients', dest = 'distortionCoefficients', help = 'distortion coefficients', nargs = '*', type = float) | |
| 17 parser.add_argument('--undistorted-multiplication', dest = 'undistortedImageMultiplication', help = 'undistorted image multiplication', type = float) | |
| 18 parser.add_argument('-f', dest = 'firstFrameNum', help = 'number of first frame number to display', type = int) | |
| 19 parser.add_argument('-l', dest = 'lastFrameNum', help = 'number of last frame number to save', type = int) | |
| 20 | |
| 21 args = parser.parse_args() | |
| 22 | |
| 23 intrinsicCameraMatrix = np.loadtxt(args.intrinsicCameraMatrixFilename) | |
| 24 #distortionCoefficients = args.distortionCoefficients | |
| 25 #undistortedImageMultiplication = args.undistortedImageMultiplication | |
| 26 #firstFrameNum = params.firstFrameNum | |
| 27 | |
| 28 capture = cv2.VideoCapture(args.videoFilename) | |
| 29 width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) | |
| 30 height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) | |
| 31 [map1, map2] = cvutils.computeUndistortMaps(width, height, args.undistortedImageMultiplication, intrinsicCameraMatrix, args.distortionCoefficients) | |
| 32 if capture.isOpened(): | |
| 33 ret = True | |
| 34 frameNum = args.firstFrameNum | |
| 35 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, args.firstFrameNum) | |
| 36 if args.lastFrameNum == None: | |
| 37 from sys import maxint | |
| 38 lastFrameNum = maxint | |
| 39 else: | |
| 40 lastFrameNum = args.lastFrameNum | |
| 41 nZerosFilename = int(ceil(log10(lastFrameNum))) | |
| 42 while ret and frameNum < lastFrameNum: | |
| 43 ret, img = capture.read() | |
| 44 if ret: | |
| 45 img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR) | |
| 46 cv2.imwrite('undistorted-{{:0{}}}.png'.format(nZerosFilename).format(frameNum), img) | |
| 47 frameNum += 1 |
