Unable to get good disparity map

Hello all!

I’ve been trying around for days to get a decent disparity map, but without success.
I have calibrated my cameras, and then calibrated my stereo setup. taken some pictures, rectified them, and saved them. (see below)

Then I use this code to generate a disparity map, but the result is very bad. SGBM does not produce anything better.

I have verified and my cameras are not swapped. the rectified images look properly rectified. I cannot figure out what I am doing wrong and/or missing. I tried a lot of combinations of the sliders, I tried to have the cameras parallel or slightly convergent, I recalibrated multiple times with the calibration plate either parallel and not parallel to the camera.

any help is greatly appreciated! I cannot figure out what else to try and what I am doing wrong.

Thank you

Best regards


here are my rectified images: https://imgur.com/a/aPaYo6t

import cv2
import numpy as np
import matplotlib.pyplot as plt

def nothing(x):

if True:
    cv2.namedWindow('disp', cv2.WINDOW_NORMAL)
    cv2.resizeWindow('disp', 800, 1200)

    cv2.createTrackbar('numDisparities', 'disp', 1, 50, nothing)
    cv2.createTrackbar('blockSize', 'disp', 5, 50, nothing)
    cv2.createTrackbar('preFilterType', 'disp', 1, 1, nothing)
    cv2.createTrackbar('preFilterSize', 'disp', 2, 25, nothing)
    cv2.createTrackbar('preFilterCap', 'disp', 5, 62, nothing)
    cv2.createTrackbar('textureThreshold', 'disp', 10, 100, nothing)
    cv2.createTrackbar('uniquenessRatio', 'disp', 15, 100, nothing)
    cv2.createTrackbar('speckleRange', 'disp', 0, 100, nothing)
    cv2.createTrackbar('speckleWindowSize', 'disp', 3, 25, nothing)
    cv2.createTrackbar('disp12MaxDiff', 'disp', 0, 100, nothing)
    cv2.createTrackbar('minDisparity', 'disp', 0, 200, nothing)

# Creating an object of StereoBM algorithm
# stereo = cv2.StereoBM_create()
stereo = cv2.StereoSGBM_create()

Left_nice = cv2.imread('c:/calimages/nice_cam_left_.jpg')
Right_nice = cv2.imread('c:/calimages/nice_cam_right_.jpg')
Left_nice = cv2.cvtColor(Left_nice, cv2.COLOR_BGR2GRAY)
Right_nice = cv2.cvtColor(Right_nice, cv2.COLOR_BGR2GRAY)

if True:
    while True:
        # Updating the parameters based on the trackbar positions
        numDisparities = cv2.getTrackbarPos('numDisparities', 'disp') * 16
        blockSize = cv2.getTrackbarPos('blockSize', 'disp') * 2 + 5
        preFilterType = cv2.getTrackbarPos('preFilterType', 'disp')
        preFilterSize = cv2.getTrackbarPos('preFilterSize', 'disp') * 2 + 5
        preFilterCap = cv2.getTrackbarPos('preFilterCap', 'disp')
        textureThreshold = cv2.getTrackbarPos('textureThreshold', 'disp')
        uniquenessRatio = cv2.getTrackbarPos('uniquenessRatio', 'disp')
        speckleRange = cv2.getTrackbarPos('speckleRange', 'disp')
        speckleWindowSize = cv2.getTrackbarPos('speckleWindowSize', 'disp') * 2
        disp12MaxDiff = cv2.getTrackbarPos('disp12MaxDiff', 'disp')
        minDisparity = cv2.getTrackbarPos('minDisparity', 'disp')

        # Setting the updated parameters before computing disparity map

        # Calculating disparity using the StereoBM algorithm
        disparity = stereo.compute(Left_nice, Right_nice)
        # NOTE: Code returns a 16bit signed single channel image,
        # CV_16S containing a disparity map scaled by 16. Hence it
        # is essential to convert it to CV_32F and scale it down 16 times.

        # Converting to float32
        disparity = disparity.astype(np.float32)

        # Scaling down the disparity values and normalizing them
        disparity = (disparity / 16.0 - minDisparity) / numDisparities

        # Displaying the disparity map
        cv2.imshow("disp", disparity)
        # Close window using esc key
        if cv2.waitKey(1) == 27: