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