OpenCV中StereoMatcher与StereoSGBM类compute函数解析求助
Hey there! Let’s dive into the compute() functions of StereoMatcher and StereoSGBM since you’re new to OpenCV stereo vision. I’ll also clarify how createRightMatcher fits into the mix based on your example code.
First, a Quick Base Class Overview
StereoMatcher is the abstract parent class for all stereo matching algorithms in OpenCV. Both StereoBM (Block Matching) and StereoSGBM (Semi-Global Block Matching) inherit from it, so they share the core compute() method signature—but their internal logic and performance characteristics are very different.
The base compute() is a pure virtual function (meaning it’s only implemented in child classes) with this signature:
virtual void compute(InputArray left, InputArray right, OutputArray disparity) = 0;
left: Grayscale left stereo image (8-bit single-channel; color images need conversion first!)right: Grayscale right stereo image (same size and type as the left)disparity: Output disparity map (typically a 16-bit signed integer matrix, where each value represents how far a pixel shifted between the left and right images)
StereoBM::compute() – Fast, Block-Based Matching
StereoBM uses a simple, fast block-matching approach: it compares small, fixed-size blocks of pixels between the left and right images to find the best match (using sum of squared differences, SSD, by default). It’s great for real-time applications but struggles with textureless regions, edges, and fine details.
Usage Example
Your code already initializes a StereoBM matcher—here’s how to use its compute() method:
// Initialize matcher (max_disp must be a multiple of 16; wsize is odd) Ptr<StereoBM> left_matcher = StereoBM::create(max_disp, wsize); // Compute left disparity map Mat disparity_left; left_matcher->compute(left_gray_img, right_gray_img, disparity_left);
StereoSGBM::compute() – Robust, Semi-Global Matching
StereoSGBM is a more advanced algorithm that adds global continuity constraints to block matching. Instead of only looking at local pixel blocks, it considers neighboring pixels to produce smoother, more accurate disparity maps—especially in textureless areas. It’s slower than StereoBM but far more reliable for most use cases.
Key Tweaks for StereoSGBM
It has extra parameters to control smoothness and accuracy:
P1: Penalty for small disparity changes (lower = more detail, higher = smoother)P2: Penalty for large disparity changes (should be 2-10xP1)MODE: Controls matching strategy (e.g.,MODE_SGBM_3WAYfor better accuracy)
Usage Example
// Initialize SGBM matcher Ptr<StereoSGBM> left_sgbm = StereoSGBM::create(0, max_disp, wsize); // Tweak advanced parameters (optional but recommended) left_sgbm->setP1(8 * 3 * wsize * wsize); left_sgbm->setP2(32 * 3 * wsize * wsize); left_sgbm->setMode(StereoSGBM::MODE_SGBM_3WAY); // Compute left disparity map Mat disparity_left_sgbm; left_sgbm->compute(left_gray_img, right_gray_img, disparity_left_sgbm);
What’s createRightMatcher() Doing?
As you noted, createRightMatcher() takes your left-facing matcher (either StereoBM or StereoSGBM) and returns a corresponding right-facing matcher. This right matcher is configured to compute a right disparity map (matching the right image to the left), which is required for the WLS filter in your example to refine the left disparity map.
Internally, it flips the image input order and adjusts disparity parameters to reverse the matching direction. Here’s how it fits into your workflow:
Ptr<StereoMatcher> right_matcher = createRightMatcher(left_matcher); Mat disparity_right; // Compute right disparity map (right image -> left image) right_matcher->compute(right_gray_img, left_gray_img, disparity_right); // Use both disparities with WLS filter Mat disparity_filtered; wls_filter->filter(disparity_left, left_gray_img, disparity_filtered, disparity_right);
Quick Tips for Newbies
- Always convert input images to grayscale first—
compute()won’t work with color images. - To visualize disparity maps, normalize the 16-bit output to 8-bit:
Mat disp_vis; normalize(disparity_left, disp_vis, 0, 255, NORM_MINMAX, CV_8U); imshow("Disparity Map", disp_vis); - Start with default parameters for
StereoSGBM, then tweakP1andP2to balance detail and smoothness.
内容的提问来源于stack exchange,提问作者vishnukumar

