I have been working on stereophotogrammetry to find depth data using OpenCV. I did the calibration and got the intrinsic, extrinsic, and distortion parameters to find the disparity map. Using these parameters, I rectified the images and used them to find a disparity map. But the gray colors in the disparity map of an object keeps on fluctuating from a low to high value i.e between 0-255. I have been trying to resolve this issue by changing lighting conditions and recalibrating the cameras based on that but still, it is happening. I am not understanding what parameters should I alter to get a proper disparity map. Any sort of help on this is appreciated. I have attached an image in which the colors of the square boxes keep on fluctuating even if it is on the same plane, located at the same distance from the camera. imagelink
The code for the disparity map is as follows:
def depth_map(self,imgL, imgR):
window_size = 3
left_matcher = cv2.StereoSGBM_create(
minDisparity=-1,
numDisparities=5*16,
blockSize=window_size,
P1=8 * 3 * window_size, # determines smoothness of the disparity map
P2=32 * 3 * window_size, # determines smoothness of the disparity map
disp12MaxDiff=12,
uniquenessRatio=10, # value should be in the range 5-15
speckleWindowSize=50, # value should be between 50-200
speckleRange=32,
preFilterCap=63,
mode=cv2.STEREO_SGBM_MODE_SGBM_3WAY
)
right_matcher = cv2.ximgproc.createRightMatcher(left_matcher)
# FILTER Parameters
lmbda = 80000
sigma = 1.3
visual_multiplier = 6
wls_filter = cv2.ximgproc.createDisparityWLSFilter(matcher_left=left_matcher)
wls_filter.setLambda(lmbda)
wls_filter.setSigmaColor(sigma)
displ = left_matcher.compute(imgL, imgR) # .astype(np.float32)/16
dispr = right_matcher.compute(imgR, imgL) # .astype(np.float32)/16
displ = np.int16(displ)
dispr = np.int16(dispr)
filteredImg = wls_filter.filter(displ, imgL, None, dispr)
filteredImg = cv2.normalize(src=filteredImg, dst=filteredImg, beta=0, alpha=255, norm_type=cv2.NORM_MINMAX);
filteredImg = np.uint8(filteredImg)
return filteredImg