Mercurial Hosting > traffic-intelligence
changeset 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 | a9c1d61a89b4 |
children | cae4e5f3fe9f |
files | scripts/compute-homography.py scripts/undistort-video.py |
diffstat | 2 files changed, 48 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/compute-homography.py Thu Aug 07 00:05:14 2014 -0400 +++ b/scripts/compute-homography.py Tue Aug 12 15:33:03 2014 -0400 @@ -106,7 +106,7 @@ if args.displayPoints and args.videoFrameFilename != None and args.worldFilename != None and homography.size>0: worldImg = cv2.imread(args.worldFilename) videoImg = cv2.imread(args.videoFrameFilename) - if args.undistort: + if args.undistort: [map1, map2] = cvutils.computeUndistortMaps(videoImg.shape[1], videoImg.shape[0], args.undistortedImageMultiplication, np.loadtxt(args.intrinsicCameraMatrixFilename), args.distortionCoefficients) videoImg = cv2.remap(videoImg, map1, map2, interpolation=cv2.INTER_LINEAR) if args.saveImages:
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/undistort-video.py Tue Aug 12 15:33:03 2014 -0400 @@ -0,0 +1,47 @@ +#! /usr/bin/env python + +import sys, argparse + +import numpy as np +import cv2 + +import cvutils +from math import ceil, log10 + +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]''') + +parser.add_argument('-i', dest = 'videoFilename', help = 'filename of the video sequence') +parser.add_argument('--intrinsic', dest = 'intrinsicCameraMatrixFilename', help = 'name of the intrinsic camera file') +parser.add_argument('--distortion-coefficients', dest = 'distortionCoefficients', help = 'distortion coefficients', nargs = '*', type = float) +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) + +args = parser.parse_args() + +intrinsicCameraMatrix = np.loadtxt(args.intrinsicCameraMatrixFilename) +#distortionCoefficients = args.distortionCoefficients +#undistortedImageMultiplication = args.undistortedImageMultiplication +#firstFrameNum = params.firstFrameNum + +capture = cv2.VideoCapture(args.videoFilename) +width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) +height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) +[map1, map2] = cvutils.computeUndistortMaps(width, height, args.undistortedImageMultiplication, intrinsicCameraMatrix, args.distortionCoefficients) +if capture.isOpened(): + ret = True + frameNum = args.firstFrameNum + capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, args.firstFrameNum) + if args.lastFrameNum == None: + from sys import maxint + lastFrameNum = maxint + else: + lastFrameNum = args.lastFrameNum + nZerosFilename = int(ceil(log10(lastFrameNum))) + while ret and frameNum < lastFrameNum: + 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) + frameNum += 1