Attention! This forum will be made read-only by Dec-20. Please migrate to Most of existing active users should've received invitation by e-mail.
Ask Your Question

How to use cv::StereoMatcher in c++

asked 2016-03-26 12:00:22 -0500

215 gravatar image

updated 2016-03-26 12:40:13 -0500

I struggling a bit understanding how i should apply StereoMatcher to create a simple disparity map?..

From the Api it seems simple but, but this simple piece of code does result in bad_memory access error.

What is going wrong?

#include <iostream>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/core/affine.hpp>

int main(int argc, const    char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    std::cout << CV_VERSION << std::endl;

    cv::Mat left = cv::imread("../left.png");
    cv::Mat right = cv::imread("../right.png");
    cv::Mat disparity_out;
    cv::StereoMatcher* map;
    map->compute(left, right, disparity_out);

    return 0;
edit retag flag offensive close merge delete


cv::StereoMatcher* map; -- this does not ever create a valid instance

berak gravatar imageberak ( 2016-03-26 12:05:00 -0500 )edit

How would you do so? cv::StereoMatcher map won't run as stereoMatcher is an abstract class.

215 gravatar image215 ( 2016-03-26 12:05:40 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-03-26 12:44:23 -0500

berak gravatar image

updated 2016-03-26 12:49:53 -0500

StereoMatcher is the (abstract) base class to StereoBM or StereoSGBM. please use one of the latter, not the former

also, you have to use cv::Ptr<>not a 'raw' pointer, like :

 Ptr< StereoBM > match = StereoBM::create();

(then, please never call anything mapusing c++, since there's already a container class with the same name in namespace std)

edit flag offensive delete link more


for the record, -- 3rd iteration ;)

berak gravatar imageberak ( 2016-03-26 13:07:30 -0500 )edit

I tried this code but the result is a black image? do you have any suggestion ?

Laurentiu gravatar imageLaurentiu ( 2017-05-19 06:58:18 -0500 )edit

the disparity is a 16U image, and your values might be fairly small. try something like:

imshow("test",disparity_out * 1000);

also, just print it out:

cout<< disparity_out;
berak gravatar imageberak ( 2017-05-19 07:04:11 -0500 )edit

the result is negative. Range between -8000 and -320000 value for my pixels

Laurentiu gravatar imageLaurentiu ( 2017-05-19 07:15:47 -0500 )edit

ah, sorry, 16S, then. maybe convertTo() 8U, using appropriate scale and offset ?

berak gravatar imageberak ( 2017-05-19 07:21:18 -0500 )edit

Now it's ok, but not very good. And I want to write as binary file, and if I read binary file the images is bad, very bad

Laurentiu gravatar imageLaurentiu ( 2017-05-19 07:25:58 -0500 )edit

After conversion the result is good. Thank you for your help :D

Laurentiu gravatar imageLaurentiu ( 2017-05-19 07:31:26 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2016-03-26 12:00:22 -0500

Seen: 1,384 times

Last updated: Mar 26 '16