Mercurial Hosting > traffic-intelligence
changeset 510:b0dac840c24f
compute homography works with undistortion
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Fri, 23 May 2014 17:33:11 -0400 |
parents | 935430b1d408 |
children | ad518f0c3218 |
files | python/cvutils.py scripts/compute-homography.py |
diffstat | 2 files changed, 20 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/python/cvutils.py Fri May 23 16:27:26 2014 -0400 +++ b/python/cvutils.py Fri May 23 17:33:11 2014 -0400 @@ -110,6 +110,15 @@ else: cv2.imshow(windowName, img) + def computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients): + from copy import deepcopy + from numpy import identity, array + newImgSize = (int(round(width*undistortedImageMultiplication)), int(round(height*undistortedImageMultiplication))) + newCameraMatrix = deepcopy(intrinsicCameraMatrix) + newCameraMatrix[0,2] = newImgSize[0]/2. + newCameraMatrix[1,2] = newImgSize[1]/2. + return cv2.initUndistortRectifyMap(intrinsicCameraMatrix, array(distortionCoefficients), identity(3), newCameraMatrix, newImgSize, cv2.CV_32FC1) + def playVideo(filename, firstFrameNum = 0, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1.): '''Plays the video''' wait = 5 @@ -206,20 +215,13 @@ '''Displays the objects overlaid frame by frame over the video ''' from moving import userTypeNames from math import ceil, log10 - from numpy import identity, array capture = cv2.VideoCapture(videoFilename) width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) if undistort: # setup undistortion - newImgSize = (int(round(width*undistortedImageMultiplication)), int(round(height*undistortedImageMultiplication))) - from copy import deepcopy - newCameraMatrix = deepcopy(intrinsicCameraMatrix) - newCameraMatrix[0,2] = newImgSize[0]/2. - newCameraMatrix[1,2] = newImgSize[1]/2. - [map1, map2] = cv2.initUndistortRectifyMap(intrinsicCameraMatrix, array(distortionCoefficients), identity(3), newCameraMatrix, newImgSize, cv2.CV_32FC1) - + [map1, map2] = computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients) if capture.isOpened(): key = -1 ret = True
--- a/scripts/compute-homography.py Fri May 23 16:27:26 2014 -0400 +++ b/scripts/compute-homography.py Fri May 23 17:33:11 2014 -0400 @@ -25,6 +25,10 @@ parser.add_argument('-n', dest = 'nPoints', help = 'number of corresponding points to input', default = 4, type = int) parser.add_argument('-u', dest = 'unitsPerPixel', help = 'number of units per pixel', default = 1., type = float) parser.add_argument('--display', dest = 'displayPoints', help = 'display original and projected points on both images', action = 'store_true') +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('--undistort', dest = 'undistort', help = 'undistort the video (because features have been extracted that way)', action = 'store_true') args = parser.parse_args() @@ -76,6 +80,9 @@ elif args.videoFrameFilename != None and args.worldFilename != None: worldImg = plt.imread(args.worldFilename) videoImg = plt.imread(args.videoFrameFilename) + 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) print('Click on {0} points in the video frame'.format(args.nPoints)) plt.figure() plt.imshow(videoImg) @@ -98,6 +105,9 @@ 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: + [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) invHomography = np.linalg.inv(homography) projectedWorldPts = cvutils.projectArray(invHomography, worldPts.T).T projectedVideoPts = cvutils.projectArray(homography, videoPts.T).T