Depth Estimation from Disparity Map

1.5k Views Asked by At

I'm trying estimate the depth of a point from the disparity map. To start with, I did the stereo calibration and rectified the images, and proceeded to find the disparity map. I used the StereoSGBM in OpenCV. Since disparity refers to the distance between two corresponding points in the left and right image of a stereo pair then:

x_right = x_left + Disparity

From the calibration I obtained the extrinsic and intrinsic parameters and then computed the baseline and focal length. Since z_cm = (baseline_cm * focal_pixels) / (disparity_pixels)

## Read image
img_left=cv.imread('images/testLeft/testL0.png')
img_right=cv.imread('images/testRight/testR0.png')
    
# Grayscale Images
frame_left = cv.cvtColor(img_left,cv.COLOR_BGR2GRAY)
frame_right = cv.cvtColor(img_right,cv.COLOR_BGR2GRAY)

# Undistort and rectify images
frame_left_rect = cv.remap(frame_left, stereoMapL_x, stereoMapL_y, cv.INTER_LANCZOS4, cv.BORDER_CONSTANT,0)
frame_right_rect = cv.remap(frame_right, stereoMapR_x, stereoMapR_y, cv.INTER_LANCZOS4, cv.BORDER_CONSTANT,0)
    
# Creating an object of StereoBM algorithm
Left_matcher = cv.StereoSGBM_create(
   minDisparity=-1, numDisparities=16*3,  
   blockSize=5,
   P1=8 * 2 * blockSize**2,
   P2=32 * 2 * blockSize**2,
   disp12MaxDiff=1,
   uniquenessRatio=10,
   speckleWindowSize=100,
   speckleRange=32,
   mode=cv.STEREO_SGBM_MODE_SGBM_3WAY

#===========================================================================
# Compute Disparity Map
#===========================================================================
disparity = Left_Matcher.compute(frame_left_rect, frame_right_rect)
# Convert to float32 and divide by 16 - read documentation for point cloud
disparity = np.float32(np.divide(disparity,16.0))

disp_test = cv.applyColorMap(np.uint8(disparity), cv.COLORMAP_PLASMA)
cv.imshow("Disparity Map",disp_test)  

#==========================================================================
# Depth Map
#==========================================================================
depth_map = np.ones(disparity.shape)
# Focal Length - Pixels | Baseline -cm | Depth_map - cm
depth_map = focal_length * Baseline /disparity

My problem is that the depth is wrong. Can anyone help in how to use the disparity map to get to depth. I might use reprojectImageTo3D but i think i have problems in my disparity map.

Rectified Stereo Pair Disparity Map

1

There are 1 best solutions below

0
Sai Bharathwaj Sai Kannan On

Check if your camera parameters fx, fy, Cx, Cy are in line with the spatial dimension of the images.