comparison python/cvutils.py @ 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
comparison
equal deleted inserted replaced
509:935430b1d408 510:b0dac840c24f
107 size = (int(round(img.shape[1]*rescale)), int(round(img.shape[0]*rescale))) 107 size = (int(round(img.shape[1]*rescale)), int(round(img.shape[0]*rescale)))
108 resizedImg = resize(img, size) 108 resizedImg = resize(img, size)
109 cv2.imshow(windowName, resizedImg) 109 cv2.imshow(windowName, resizedImg)
110 else: 110 else:
111 cv2.imshow(windowName, img) 111 cv2.imshow(windowName, img)
112
113 def computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients):
114 from copy import deepcopy
115 from numpy import identity, array
116 newImgSize = (int(round(width*undistortedImageMultiplication)), int(round(height*undistortedImageMultiplication)))
117 newCameraMatrix = deepcopy(intrinsicCameraMatrix)
118 newCameraMatrix[0,2] = newImgSize[0]/2.
119 newCameraMatrix[1,2] = newImgSize[1]/2.
120 return cv2.initUndistortRectifyMap(intrinsicCameraMatrix, array(distortionCoefficients), identity(3), newCameraMatrix, newImgSize, cv2.CV_32FC1)
112 121
113 def playVideo(filename, firstFrameNum = 0, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1.): 122 def playVideo(filename, firstFrameNum = 0, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1.):
114 '''Plays the video''' 123 '''Plays the video'''
115 wait = 5 124 wait = 5
116 if frameRate > 0: 125 if frameRate > 0:
204 213
205 def displayTrajectories(videoFilename, objects, boundingBoxes = {}, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True, rescale = 1., nFramesStep = 1, saveAllImages = False, undistort = False, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = 1.): 214 def displayTrajectories(videoFilename, objects, boundingBoxes = {}, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True, rescale = 1., nFramesStep = 1, saveAllImages = False, undistort = False, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = 1.):
206 '''Displays the objects overlaid frame by frame over the video ''' 215 '''Displays the objects overlaid frame by frame over the video '''
207 from moving import userTypeNames 216 from moving import userTypeNames
208 from math import ceil, log10 217 from math import ceil, log10
209 from numpy import identity, array
210 218
211 capture = cv2.VideoCapture(videoFilename) 219 capture = cv2.VideoCapture(videoFilename)
212 width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) 220 width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH))
213 height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) 221 height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))
214 222
215 if undistort: # setup undistortion 223 if undistort: # setup undistortion
216 newImgSize = (int(round(width*undistortedImageMultiplication)), int(round(height*undistortedImageMultiplication))) 224 [map1, map2] = computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients)
217 from copy import deepcopy
218 newCameraMatrix = deepcopy(intrinsicCameraMatrix)
219 newCameraMatrix[0,2] = newImgSize[0]/2.
220 newCameraMatrix[1,2] = newImgSize[1]/2.
221 [map1, map2] = cv2.initUndistortRectifyMap(intrinsicCameraMatrix, array(distortionCoefficients), identity(3), newCameraMatrix, newImgSize, cv2.CV_32FC1)
222
223 if capture.isOpened(): 225 if capture.isOpened():
224 key = -1 226 key = -1
225 ret = True 227 ret = True
226 frameNum = firstFrameNum 228 frameNum = firstFrameNum
227 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) 229 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum)