I want to use the ZED 2 to make a disparity map using Opencv2 in python. Because it is a school assignement I need to implement the algorithm myself, and I cant use the ZED SDK. I can use opencv librarys and functions such as cv2.StereoBM_create() stereo.compute(imgL_gray, imgR_gray).
I followed a few online turtorials like Turtorial and these work for the images they provide, but they do not work with my images from the ZED2.
I used the ZED 2.
Here is the image I got from the ZED converted to png.
Original Image
The best endresult i got is this. As you can see, it has some form of depth, but it is definetly not a nice disparity map.
Disparity map
This is the code I used. I tried a lot of them but none got better results than this. I would love some help on how to improve these results. I also tried a lot of different parameters already.
import cv2
import numpy as np
from matplotlib import pyplot as plt
# Load the image in grayscale
image = cv2.imread('TestImage4.png', 0)
# Split the image into left and right halves
left_right_image = np.split(image, 2, axis=1)
# Display the left and right images
cv2.imshow("Left Image", left_right_image[0])
cv2.imshow("Right Image", left_right_image[1])
cv2.waitKey(0)
cv2.destroyAllWindows()
# Parameters for the StereoSGBM algorithm
block_size = 11
min_disp = 0
max_disp = 255
num_disp = max_disp - min_disp
uniquenessRatio = 5
speckleWindowSize = 200
speckleRange = 2
disp12MaxDiff = 0
# Create the StereoSGBM object
stereo = cv2.StereoSGBM_create(
minDisparity=min_disp,
numDisparities=num_disp,
blockSize=block_size,
uniquenessRatio=uniquenessRatio,
speckleWindowSize=speckleWindowSize,
speckleRange=speckleRange,
disp12MaxDiff=disp12MaxDiff,
P1=8 * 3 * block_size ** 2,
P2=32 * 3 * block_size ** 2,
mode=cv2.STEREO_SGBM_MODE_SGBM
)
# Compute the disparity map
disparity = stereo.compute(left_right_image[0], left_right_image[1])
# Normalize the disparity map for displaying
disparity = cv2.normalize(disparity, disparity, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)
disparity = np.uint8(disparity)
# Resize the disparity map for better viewing
disparity_resized = cv2.resize(disparity, (disparity.shape[1] // 2, disparity.shape[0] // 2))
# Display the disparity map
cv2.imshow("Disparity", disparity_resized)
cv2.waitKey(0)
cv2.destroyAllWindows()
gyan fransen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.