Contenido principal

disparitySGM

Compute disparity map through semi-global matching

Description

disparityMap = disparitySGM(I1,I2) computes disparity map from a pair of rectified stereo images I1 and I2, by using semi-global matching (SGM) method. To know more about rectifying stereo images, see Image Rectification.

example

[disparityMap,disparityRangeUsed] = disparitySGM(I1,I2) additionally returns the actual values of the disparity range used to estimate disparity, which may differ from those provided using the DisparityRange name-value argument.

example

___ = disparitySGM(I1,I2,Name,Value) specifies additional options using one or more name-value pair arguments along with any of the previous syntaxes.

Examples

collapse all

Load a rectified stereo pair image.

I1 = imread("rectified_left.png");
I2 = imread("rectified_right.png");

Create the stereo anaglyph of the rectified stereo pair image and display it. You can view the image in 3-D by using red-cyan stereo glasses.

A = stereoAnaglyph(I1,I2);
figure
imshow(A)
title("Red-Cyan composite view of the rectified stereo pair image")

Figure contains an axes object. The hidden axes object with title Red-Cyan composite view of the rectified stereo pair image contains an object of type image.

Convert the rectified input color images to grayscale images.

J1 = im2gray(I1);
J2 = im2gray(I2);

Compute the disparity map through semi-global matching. Specify the range of disparity as [0, 48], and the minimum value of uniqueness as 20.

disparityRange = [0 48];
disparityMap = disparitySGM(J1,J2,"DisparityRange",disparityRange,"UniquenessThreshold",20);

Display the disparity map. Set the display range to the same value as the disparity range.

figure
imshow(disparityMap,disparityRange)
title("Disparity Map")
colormap jet
colorbar

Figure contains an axes object. The hidden axes object with title Disparity Map contains an object of type image.

Input Arguments

collapse all

Input image referenced as I1 corresponding to camera 1, specified as a 2-D grayscale image or a gpuArray (Parallel Computing Toolbox) object. The function uses this image as the reference image for computing the disparity map. The input images I1 and I2 must be real, finite, and nonsparse. Also, I1 and I2 must be of the same size and same data type.

Data Types: single | double | int16 | uint8 | uint16

Input image referenced as I2 corresponding to camera 2, specified as a 2-D grayscale image or a gpuArray (Parallel Computing Toolbox) object. The input images I1 and I2 must be real, finite, and nonsparse. I1 and I2 must be of the same size and same data type.

Data Types: single | double | int16 | uint8 | uint16

Name-Value Arguments

collapse all

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: disparitySGM(I1,I2,'DisparityRange',[0 64])

Range of disparity, specified as the comma-separated pair consisting of 'DisparityRange' and a two-element vector of the form [MinDisparity MaxDisparity]. MinDisparity is the minimum disparity and MaxDisparity is the maximum disparity.

For input images of width N, the MinDisparity and MaxDisparity must be integers in the range (–N, N). The difference between the MaxDisparity and MinDisparity values must be less than or equal to 128. If the difference is not divisible by 8, the function internally rounds up the disparity range to the nearest multiple of 8. The function returns the actual value used in the disparity map computation in the disparityRangeUsed output argument.

The default value for the range of disparity is [0 128]. For more information on choosing the range of disparity, see Choosing Range of Disparity.

Data Types: integers

Minimum value of uniqueness, specified as the comma-separated pair consisting of 'UniquenessThreshold' and a nonnegative integer.

The function marks the estimated disparity value K for a pixel as unreliable, if:

v < V×(1+0.01×UniquenessThreshold),

where V is the Hamming distance corresponding to the disparity value K. v is the smallest Hamming distance value over the whole disparity range, excluding K, K–1, and K+1.

Increasing the value of UniquenessThreshold results in disparity values for more pixels being marked as unreliable. To disable the use of uniqueness threshold, set this value to 0.

Output Arguments

collapse all

Disparity map for rectified stereo pair image, returned as a 2-D grayscale image or a gpuArray object. The function returns the disparity map with the same size as input images I1 and I2. Each value in this output refers to the displacement between conjugate pixels in the stereo pair image. For details about computing the disparity map, see Computing Disparity Map Using Semi-Global Matching.

Data Types: single

Range of disparity actually used in the disparity map computation, returned as a two-element vector of the form [MinDisparity MaxDisparity]. MinDisparity is the minimum disparity and MaxDisparity is the maximum disparity. This value can differ from the value specified using the DisparityRange name-value argument in order to satisfy internal numerical constraints, where the difference between the MaxDisparity and MinDisparity values must be divisible by 8.

Data Types: double

More About

collapse all

Tips

  • Ensure accurate rectification — Imperfect rectification can significantly degrade the quality of the disparitySGM function results. Use the rectifyStereoImages function to rectify the input stereo image pair I1 and I2, so that the corresponding points are on the same rows. Use the stereoAnaglyph function to inspect the rectification results by creating an anaglyph image.

  • Apply post-processing — For better results, improve disparity maps by applying image processing operations such as median filtering (medfilt2), hole filling (imfill), or bilateral filtering (imbilatfilt).

  • Explore deep learning-based alternatives — For well-rectified, narrow-baseline stereo setups where classical SGM struggles with texture-less regions or repeated patterns, try opticalFlowRAFT as a deep learning-based alternative. For more information on comparison between classical SGM and RAFT deep learning model, see Compare RAFT Optical Flow and Semi-Global Matching for Stereo Reconstruction.

Algorithms

collapse all

References

[1] Hirschmuller, H. "Accurate and Efficient Stereo Processing by Semi-Global Matching and Mutual Information." In Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition (CVPR), pp. 807-814. San Diego, CA: IEEE, 2005.

Extended Capabilities

expand all

Version History

Introduced in R2019a

expand all