1 #!/usr/bin/env python 2 3 import sys 4 import cv2.cv as cv 5 6 def findstereocorrespondence(image_left, image_right): 7 # image_left and image_right are the input 8-bit single-channel images 8 # from the left and the right cameras, respectively 9 (r, c) = (image_left.rows, image_left.cols) 10 disparity_left = cv.CreateMat(r, c, cv.CV_16S) 11 disparity_right = cv.CreateMat(r, c, cv.CV_16S) 12 state = cv.CreateStereoGCState(16, 2) 13 cv.FindStereoCorrespondenceGC(image_left, image_right, disparity_left, disparity_right, state, 0) 14 return (disparity_left, disparity_right) 15 16 17 if __name__ == '__main__': 18 19 (l, r) = [cv.LoadImageM(f, cv.CV_LOAD_IMAGE_GRAYSCALE) for f in sys.argv[1:]] 20 21 (disparity_left, disparity_right) = findstereocorrespondence(l, r) 22 23 disparity_left_visual = cv.CreateMat(l.rows, l.cols, cv.CV_8U) 24 cv.ConvertScale(disparity_left, disparity_left_visual, -16) 25 cv.SaveImage("disparity.pgm", disparity_left_visual) 26