opencv - Python: How to get real depth from disparity map -


i complete beginner trying obtain real depth map left , right image. i've used opencv disparity map via block matching can see in code bellow.

import cv2 import cv2.cv cv import sys import numpy np  def getdisparity(imgleft, imgright, method="bm"):  gray_left = cv2.cvtcolor(imgleft, cv.cv_bgr2gray) gray_right = cv2.cvtcolor(imgright, cv.cv_bgr2gray) print gray_left.shape c, r = gray_left.shape if method == "bm":     sbm = cv.createstereobmstate()     disparity = cv.createmat(c, r, cv.cv_32f)     sbm.sadwindowsize = 11     sbm.prefiltertype = 1     sbm.prefiltersize = 5     sbm.prefiltercap = 61     sbm.mindisparity = -50     sbm.numberofdisparities = 112     sbm.texturethreshold = 507     sbm.uniquenessratio= 0     sbm.specklerange = 8     sbm.specklewindowsize = 0      gray_left = cv.fromarray(gray_left)     gray_right = cv.fromarray(gray_right)      cv.findstereocorrespondencebm(gray_left, gray_right, disparity, sbm)     disparity_visual = cv.createmat(c, r, cv.cv_8u)     cv.normalize(disparity, disparity_visual, 0, 255, cv.cv_minmax)     disparity_visual = np.array(disparity_visual)  elif method == "sgbm":     sbm = cv2.stereosgbm()     sbm.sadwindowsize = 9;     sbm.numberofdisparities = 0;     sbm.prefiltercap = 63;     sbm.mindisparity = -21;     sbm.uniquenessratio = 7;     sbm.specklewindowsize = 0;     sbm.specklerange = 8;     sbm.disp12maxdiff = 1;     sbm.fulldp = false;      disparity = sbm.compute(gray_left, gray_right)     disparity_visual = cv2.normalize(disparity, alpha=0, beta=255, norm_type=cv2.cv.cv_minmax, dtype=cv2.cv.cv_8u)  return disparity_visual  imgleft = cv2.imread('1.png') imgright = cv2.imread('2.png') try:     method = "bm" except indexerror:     method = "bm"  disparity = getdisparity(imgleft, imgright, method) cv2.imshow("disparity", disparity) #cv2.imshow("left", imgleft) #cv2.imshow("right", imgright) cv2.waitkey(0) 

my question easiest way obtain real depth map (distance) disparity using python?

in order calculate depth stereo, need know translation , rotation between cameras. if have that, can take each disparity value , use triangulation calculate depth 3d point.

i recommend reading http://www.robots.ox.ac.uk/~vgg/hzbook/ detailed explanation.


Comments