Mercurial Hosting > traffic-intelligence
changeset 1024:acb4f6f6545d
corrected issues with update to OpenCV 3
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Sun, 10 Jun 2018 23:26:14 -0400 |
parents | a13f47c8931d |
children | 6ba30b259525 |
files | scripts/undistort-video.py |
diffstat | 1 files changed, 12 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/undistort-video.py Wed Jun 06 16:51:15 2018 -0400 +++ b/scripts/undistort-video.py Sun Jun 10 23:26:14 2018 -0400 @@ -7,7 +7,7 @@ import cvutils from math import ceil, log10 -from os import path, mkdir +from pathlib import Path 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]''') @@ -15,7 +15,7 @@ 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('--undistorted-multiplication', dest = 'undistortedImageMultiplication', help = 'undistorted image multiplication', type = float, default = 1.) parser.add_argument('--mask', dest = 'maskFilename', help = 'name of the mask file, to undistort to see how it covers the undistortion errors') parser.add_argument('-f', dest = 'firstFrameNum', help = 'number of first frame number to display', type = int, default = 0) parser.add_argument('-l', dest = 'lastFrameNum', help = 'number of last frame number to save', type = int) @@ -28,18 +28,15 @@ intrinsicCameraMatrix = np.loadtxt(args.intrinsicCameraMatrixFilename) if args.destinationDirname is None: - destinationDirname = '' + destinationPath = Path('.') else: - if not args.destinationDirname.endswith('/'): - destinationDirname = args.destinationDirname+'/' - else: - destinationDirname = args.destinationDirname - if not path.exists(destinationDirname): - mkdir(destinationDirname) + destinationPath = Path(destinationPath) + if not destinationPath.exists(): + destinationPath.mkdir() 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)) +width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)) +height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) [map1, map2], newCameraMatrix = cvutils.computeUndistortMaps(width, height, args.undistortedImageMultiplication, intrinsicCameraMatrix, args.distortionCoefficients) if args.maskFilename is not None: mask = cv2.imread(args.maskFilename) @@ -48,7 +45,7 @@ if capture.isOpened(): ret = True frameNum = args.firstFrameNum - capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, args.firstFrameNum) + capture.set(cv2.CAP_PROP_POS_FRAMES, args.firstFrameNum) if args.lastFrameNum is None: lastFrameNum = float('inf') else: @@ -58,9 +55,9 @@ ret, img = capture.read() if ret: img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR) - cv2.imwrite(destinationDirname+'undistorted-{{:0{}}}.png'.format(nZerosFilename).format(frameNum), img) + cv2.imwrite(str(destinationPath/Path('undistorted-{{:0{}}}.png'.format(nZerosFilename).format(frameNum))), img) if args.maskFilename is not None: - cv2.imwrite(destinationDirname+'undistorted+mask-{{:0{}}}.png'.format(nZerosFilename).format(frameNum), cv2.multiply(img, undistortedMask)) + cv2.imwrite(str(destinationPath/Path('undistorted+mask-{{:0{}}}.png'.format(nZerosFilename).format(frameNum))), cv2.multiply(img, undistortedMask, dtype = 16)) frameNum += 1 if args.encodeVideo: @@ -68,5 +65,5 @@ 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) + check_call("mencoder \'mf://"+destinationPath+"*.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()