# How to get good disparity map

**URL:** <https://forum.opencv.org/t/how-to-get-good-disparity-map/17332>\
**Category:** Python\
**Created:** [April 18, 2024, 12:37pm UTC](https://forum.opencv.org/t/how-to-get-good-disparity-map/17332 "2024-04-18T12:37:18Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hiki](https://avatars.discourse-cdn.com/v4/letter/h/c6cbf5/32.png) [@Hiki](https://forum.opencv.org/u/Hiki)\
**Post date:** [April 18, 2024, 12:37pm UTC](https://forum.opencv.org/t/how-to-get-good-disparity-map/17332/1 "2024-04-18T12:37:18Z")

</div>

Hello,

This is pic after rectification,but disparity map is seemly wrong

[[Album] imgur.com ![](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/7/7e6950eb5689a4e9ced18c944204835f3d37ca4e.jpeg "imgur.com") ](https://imgur.com/a/UYdnjv9)

should I amend something?

def stereoMatchSGBM(left\_image, right\_image, down\_scale=False):  
# SGBM  
if left\_image.ndim == 2:  
img\_channels = 1  
else:  
img\_channels = 3  
blockSize = 3  
paraml = {‘minDisparity’: 0,  
‘numDisparities’: 128,  
‘blockSize’: blockSize,  
‘P1’: 8 \* img\_channels \* blockSize \*\* 2,  
‘P2’: 32 \* img\_channels \* blockSize \*\* 2,  
‘disp12MaxDiff’: 1,  
‘preFilterCap’: 63,  
‘uniquenessRatio’: 15,  
‘speckleWindowSize’: 100,  
‘speckleRange’: 1,  
‘mode’: cv2.STEREO\_SGBM\_MODE\_SGBM\_3WAY  
}

```
left_matcher = cv2.StereoSGBM_create(**paraml)
paramr = paraml
paramr['minDisparity'] = -paraml['numDisparities']
right_matcher = cv2.StereoSGBM_create(**paramr)

size = (left_image.shape[1], left_image.shape[0])
if down_scale == False:
    disparity_left = left_matcher.compute(left_image, right_image)
    disparity_right = right_matcher.compute(right_image, left_image)
else:
    left_image_down = cv2.pyrDown(left_image)
    right_image_down = cv2.pyrDown(right_image)
    factor = left_image.shape[1] / left_image_down.shape[1]

    disparity_left_half = left_matcher.compute(left_image_down, right_image_down)
    disparity_right_half = right_matcher.compute(right_image_down, left_image_down)
    disparity_left = cv2.resize(disparity_left_half, size, interpolation=cv2.INTER_AREA)
    disparity_right = cv2.resize(disparity_right_half, size, interpolation=cv2.INTER_AREA)
    disparity_left = factor * disparity_left
    disparity_right = factor * disparity_right

trueDisp_left = disparity_left.astype(np.float32) / 16.
trueDisp_right = disparity_right.astype(np.float32) / 16.

return trueDisp_left, trueDisp_right

```

Thank u
