comparison scripts/compute-homography.py @ 538:bd1ad468e928

corrected bug and added capability to save undistorted image
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 03 Jul 2014 13:57:32 -0400
parents b0dac840c24f
children 9c429c7efe89
comparison
equal deleted inserted replaced
537:6c264b914846 538:bd1ad468e928
26 parser.add_argument('-u', dest = 'unitsPerPixel', help = 'number of units per pixel', default = 1., type = float) 26 parser.add_argument('-u', dest = 'unitsPerPixel', help = 'number of units per pixel', default = 1., type = float)
27 parser.add_argument('--display', dest = 'displayPoints', help = 'display original and projected points on both images', action = 'store_true') 27 parser.add_argument('--display', dest = 'displayPoints', help = 'display original and projected points on both images', action = 'store_true')
28 parser.add_argument('--intrinsic', dest = 'intrinsicCameraMatrixFilename', help = 'name of the intrinsic camera file') 28 parser.add_argument('--intrinsic', dest = 'intrinsicCameraMatrixFilename', help = 'name of the intrinsic camera file')
29 parser.add_argument('--distortion-coefficients', dest = 'distortionCoefficients', help = 'distortion coefficients', nargs = '*', type = float) 29 parser.add_argument('--distortion-coefficients', dest = 'distortionCoefficients', help = 'distortion coefficients', nargs = '*', type = float)
30 parser.add_argument('--undistorted-multiplication', dest = 'undistortedImageMultiplication', help = 'undistorted image multiplication', type = float) 30 parser.add_argument('--undistorted-multiplication', dest = 'undistortedImageMultiplication', help = 'undistorted image multiplication', type = float)
31 parser.add_argument('--undistort', dest = 'undistort', help = 'undistort the video (because features have been extracted that way)', action = 'store_true') 31 parser.add_argument('--undistort', dest = 'undistort', help = 'undistort the video (because features have been extracted that way', action = 'store_true')
32 parser.add_argument('--save', dest = 'saveImages', help = 'save the undistorted video frame (display option must be chosen)', action = 'store_true')
32 33
33 args = parser.parse_args() 34 args = parser.parse_args()
34 35
35 # TODO process camera intrinsic and extrinsic parameters to obtain image to world homography, taking example from Work/src/python/generate-homography.py script 36 # TODO process camera intrinsic and extrinsic parameters to obtain image to world homography, taking example from Work/src/python/generate-homography.py script
36 # cameraMat = load(videoFilenamePrefix+'-camera.txt'); 37 # cameraMat = load(videoFilenamePrefix+'-camera.txt');
106 worldImg = cv2.imread(args.worldFilename) 107 worldImg = cv2.imread(args.worldFilename)
107 videoImg = cv2.imread(args.videoFrameFilename) 108 videoImg = cv2.imread(args.videoFrameFilename)
108 if args.undistort: 109 if args.undistort:
109 [map1, map2] = cvutils.computeUndistortMaps(videoImg.shape[1], videoImg.shape[0], args.undistortedImageMultiplication, np.loadtxt(args.intrinsicCameraMatrixFilename), args.distortionCoefficients) 110 [map1, map2] = cvutils.computeUndistortMaps(videoImg.shape[1], videoImg.shape[0], args.undistortedImageMultiplication, np.loadtxt(args.intrinsicCameraMatrixFilename), args.distortionCoefficients)
110 videoImg = cv2.remap(videoImg, map1, map2, interpolation=cv2.INTER_LINEAR) 111 videoImg = cv2.remap(videoImg, map1, map2, interpolation=cv2.INTER_LINEAR)
112 if args.saveImages:
113 cv2.imwrite(utils.removeExtension(args.videoFrameFilename)+'-undistorted.png', videoImg)
111 invHomography = np.linalg.inv(homography) 114 invHomography = np.linalg.inv(homography)
112 projectedWorldPts = cvutils.projectArray(invHomography, worldPts.T).T 115 projectedWorldPts = cvutils.projectArray(invHomography, worldPts.T).T
113 projectedVideoPts = cvutils.projectArray(homography, videoPts.T).T 116 projectedVideoPts = cvutils.projectArray(homography, videoPts.T).T
114 for i in range(worldPts.shape[0]): 117 for i in range(worldPts.shape[0]):
115 # world image 118 # world image