Khairuzzaman Mamun

  • About
  • Expeience
  • Education
  • Skills
  • Publications
  • w/R interest
  • SWOT Analysis
  • Hobbies
  • Connect me
June 25, 2025

cv::StereoSGBM::create(…) in OpenCV

The StereoSGBM (Semi-Global Block Matching) algorithm computes a disparity map from a pair of stereo images. This disparity map represents the pixel shift between matching points in the left and right images, which in turn relates to depth. The cv::StereoSGBM::create(…) function initializes the algorithm with the following tuning parameters:

Parameter Explanations:

1. minDisparity (int)

o Purpose: Sets the minimum disparity value that the algorithm will consider.

o Interpretation: If objects are closer or further, their pixel shift (disparity) changes. A value of 0 means the search for matching areas starts at a 0-pixel offset.

o Impact: Increasing it might be necessary if you know the scene doesn’t contain objects with zero or negative disparity, effectively narrowing the search window.

2. numDisparities (int)

o Purpose: Determines the total range, in pixel values, that the algorithm searches over to find corresponding points between the stereo images.

o Interpretation: This value must be a multiple of 16. Here, 16 * 10 = 160 means the algorithm will search across 160 disparity levels (from 0 up to 159, given minDisparity = 0).

o Impact: A larger value allows the algorithm to handle scenes with objects at varied depths; however, it will increase computation time.

3. blockSize (int)

o Purpose: Specifies the linear size of the block (or window) used in matching.

o Interpretation: Here, a block size of 3 means that a 3×3 region around each pixel is used to compute similarity.

o Impact: Smaller block sizes give better detail but are more sensitive to noise. Larger windows provide more robust estimates in texture-less regions but may blur fine details.

4. P1 (int)

o Purpose: This is the penalty for a disparity change of 1 between neighboring pixels.

o Interpretation: Calculated as 8 × (number of image channels) × blockSize². For a color image (with 3 channels) and a 3×3 block, it becomes 8 * 3 * 3 * 3 = 216.

o Impact: A lower P1 allows more rapid changes in disparity, while a higher value enforces smoother transitions. This helps to reduce noise while maintaining depth discontinuities.

5. P2 (int)

o Purpose: A larger penalty applied when the disparity difference between neighboring pixels is greater than 1.

o Interpretation: Calculated similarly to P1 but with a larger multiplier: 32 × (number of channels) × blockSize². For our case, it becomes 32 * 3 * 3 * 3 = 864.

o Impact: This higher cost penalizes abrupt changes in disparity more heavily, promoting overall smoothness in the computed disparity map, while still allowing fine changes where necessary.

6. disp12MaxDiff (int)

o Purpose: Specifies the maximum allowed difference in matching between the left-to-right and right-to-left disparity maps.

o Interpretation: With a value of 1, it enforces that the corresponding disparities computed from both images should differ by no more than one pixel.

o Impact: This consistency check helps filter out unreliable matches and reduces errors in the final disparity map.

7. preFilterCap (int)

o Purpose: Limits the range of pixel values after a pre-filtering step that reduces the effects of variations in illumination.

o Interpretation: Here, a value of 4 caps pixel intensity differences.

o Impact: It normalizes pixel intensities and curtails extreme values that could distort the matching process. Choosing the right cap can enhance matching performance in scenes with high contrast.

8. uniquenessRatio (int)

o Purpose: Acts as a confidence measure to decide whether a match is distinct enough from the next best alternative.

o Interpretation: With a value of 10, the best match’s cost should be at least 10% better than that of the second-best match.

o Impact: This parameter filters out ambiguous matches, helping to ensure that only distinctive and reliable correspondences contribute to the final disparity map.

9. speckleWindowSize (int)

o Purpose: Used in the speckle filtering post-process to eliminate small regions (or “speckles”) that are likely noise.

o Interpretation: A 100-pixel window means that connected components with fewer than 100 pixels will be considered noise and removed.

o Impact: This filtering step cleans up the disparity map, removing spurious, isolated regions that do not correspond to genuine depth features.

10. speckleRange (int)

o Purpose: Sets the maximum allowed difference in disparity within each connected component during speckle filtering.

o Interpretation: With a range of 32, disparities within a “speckle” must vary by less than this amount.

o Impact: It further refines the filtered regions, ensuring that valid regions of similar disparity are preserved while noise is suppressed.

Practical Considerations

Ø Tuning:
Experimentation is often necessary to balance between accuracy and smoothness. For instance, scenes with a lot of texture or varied lighting may require adjustments in preFilterCap and uniquenessRatio to ensure robust matching.

Ø Performance vs. Accuracy:

Increasing numDisparities allows the algorithm to handle larger depth variations but increases computational complexity. Similarly, adjusting blockSize affects the balance between sensitivity to details and robustness to noise.

Ø Real-World Scenarios:

o In automotive applications, ensuring high accuracy in disparity is critical for estimating distances to objects reliably.

o In robotics or augmented reality, real-time performance may dictate using smaller blocks or reducing numDisparities.

Share this:

  • Share on Facebook (Opens in new window) Facebook
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Share on Telegram (Opens in new window) Telegram
  • Share on WhatsApp (Opens in new window) WhatsApp
  • Share on X (Opens in new window) X
  • Print (Opens in new window) Print
  • Email a link to a friend (Opens in new window) Email
Like Loading…
ai
ai

Leave a comment Cancel reply

Menu

  • About
  • Expeience
  • Education
  • Skills
  • Publications
  • w/R interest
  • SWOT Analysis
  • Hobbies
  • Connect me

Contact

020-0127, Morioka, Iwate

Zenkunen, 3-9-3-305

presidentmamun@gmail.com

+81-80-9632-6849

Khairuzzaman Mamun

A multidisciplinary research engineer. To discover more about him—click here.

Designed by Khairuzzaman Mamun

  • Comment
  • Reblog
  • Subscribe Subscribed
    • Khairuzzaman Mamun
    • Already have a WordPress.com account? Log in now.
    • Khairuzzaman Mamun
    • Subscribe Subscribed
    • Sign up
    • Log in
    • Copy shortlink
    • Report this content
    • View post in Reader
    • Manage subscriptions
    • Collapse this bar
%d