current position:Home>Python CV. StereoSGBM_create rounding method parameters and usage

Python CV. StereoSGBM_create rounding method parameters and usage

2022-09-09 04:09:54Qiao Qing


Create a StereoSGBM (semi-global block matching) object.This object implements the semi-global matching algorithm.


cv.StereoSGBM_create([, minDisparity[, numDisparities[, blockSize[, P1[, P2[, disp12MaxDiff[, preFilterCap[, uniquenessRatio[, speckleWindowSize[, speckleRange[, mode]]]]]]]]]]]) -> retval


  • minDisparity: The smallest possible disparity value.Normally it is zero, but sometimes the correction algorithm moves the image so this parameter needs to be adjusted accordingly.
  • numDisparities: The search range for disparity, that is, the maximum difference minus the minimum difference.This value is always greater than zero.In the current implementation, this parameter must be divisible by 16.
  • blockSize: The linear size of the block.The size should be odd (since the block is centered on the current pixel).A larger block size means a smoother, but less accurate disparity map.A smaller block size gives a more detailed disparity map, but the algorithm has a higher chance of finding the wrong correspondence.Usually between 3 and 11.
  • P1: The first parameter that controls the smoothness of disparity, which is a penalty of 1 for disparity changes between adjacent pixels.The larger the value, the smoother the parallax.
  • P2: The second parameter that controls the smoothness of the parallax, which is the penalty for the parallax variation between adjacent pixels exceeding 1.The larger the value, the smoother the parallax.The algorithm requires P2>P1.
  • disp12MaxDiff: The maximum disparity (in integer pixels) allowed in left and right disparity checks.Set it to a non-positive value to disable checking.
  • preFilterCap: The cutoff value for pre-filtered image pixels.The algorithm first computes the x-direction derivative for each pixel and clips its value by the [-preFilterCap, preFilterCap] interval.The resulting value is passed to the Birchfield-Tomasi pixel cost function.
  • uniquenessRatio: The percentage by which the best (minimum) computational cost function value should exceed the second best value before a match found is considered correct.Usually, a value in the range of 5-15 is good enough.
  • speckleWindowSize: The maximum size of the smooth gap region whose noise speckle is considered and invalidated.Set it to 0 to disable blob filtering.Otherwise, set it somewhere in the 50-200 range.
  • specleRange: Maximum parallax change within each connected component.If blob filtering, set the parameter to a positive value, it will be implicitly multiplied by 16.Usually, 1 or 2 are sufficient.
  • mode: By default, it is set to false.If it is set to MODE_HH, the full two-pass dynamic programming algorithm will be run.

Code Example

import cv2def estimate_depth(left_path, right_path, show=True):# Read the left and right imagesimg_left = cv2.imread(left_path, 0)img_right = cv2.imread(right_path, 0)height, width = img_left.shape[:2]# Initialize the stereo block match objectstereo = cv2.StereoBM_create(numDisparities=16, blockSize=15)# Get the disparity mapdisparity = stereo.compute(img_left, img_right)if show:# Normalize the disparity map# min_val = disparity.min()# max_val = disparity.max()# disparity = np.uint8(6400 * (disparity - min_val) / (max_val - min_val))# show disparity mapcv2.imshow('disparity image', disparity)cv2.imwrite('disparity.jpg', disparity)cv2.waitKey(0)estimate_depth('images/view1.png', 'images/view5.png')

copyright notice
author[Qiao Qing],Please bring the original link to reprint, thank you.

Random recommended