diff 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
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