Ask Your Question

Plane Gray Disparity Map by StereoSGBM

asked 2018-04-13 05:12:50 -0500

mfischer-gundlach gravatar image

updated 2018-04-13 06:24:20 -0500

Hey there,

I am moving my code from python to C++ but run into troubles when computing a disparity map with StereoSGBM. The map has the same values everywhere -from my python version I do know its not that smooth at all.

Here is some code:

const std::string filename_1 = "i1.png";
const std::string filename_2 = "i2.png";
cv::Mat image_1_grayscale;
cv::cvtColor(cv::imread(filename_1), image_1_grayscale, cv::COLOR_BGR2GRAY);
cv::Mat image_2_grayscale;
cv::cvtColor(cv::imread(filename_2), image_2_grayscale, cv::COLOR_BGR2GRAY);

cv::Ptr<cv::StereoSGBM> sgbm = cv::StereoSGBM::create();

cv::Mat disp;
sgbm->compute(image_1_grayscale, image_2_grayscale, disp);

if (!disp.empty()){
    cv::imshow("normal", disp);

I checked size of disp after computation and it does change to the size of the images, is of type CV_16SC1 and every value is -16.

Whats wrong in this snippet?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-04-13 05:17:49 -0500

berak gravatar image

updated 2018-04-13 05:19:42 -0500

Whats wrong in this snippet? -- the type. please check disp.type().

if it's CV_16S, use:<short>(0, 0));

if it's CV_16U:<ushort>(0, 0));

maybe just print out some small region:

cout  << disp(Rect(100,100,10,10)) << endl;
edit flag offensive delete link more


Hey, Thanks for the reply. I edited my question as it was not really clear what the problems was. The problem is not the value itself but the fact that every entry in the disparity map has the same value.

mfischer-gundlach gravatar imagemfischer-gundlach ( 2018-04-13 06:22:26 -0500 )edit

there are params to configure here , the default values probablydid not work in your case.

berak gravatar imageberak ( 2018-04-13 06:28:30 -0500 )edit

The values are fine (I know the expected results from my python scripts). The problem appears to be with imshow() for the disparity map. If I first save the map to an PNG and than use imshow() on the PNG everything is fine.

I will attach an image as answer to my question as soon as the 2 day rule does not apply anymore.

mfischer-gundlach gravatar imagemfischer-gundlach ( 2018-04-13 07:02:27 -0500 )edit

Disparity map is a 2D matrix of CV_16SC1 stereo disparity match values, as you said.

Non-negative values, ex. 0 or larger, are the disparity of the stereo match at that point, in units of 1/DISP_SCALE.

Negative values, ex. -16 = -DISP_SCALE, mean no disparity is available for that point - there was no match calculated at that point. Either the disparity is outside the search volume (horopter), filtered out as noise, or otherwise not found.

opalmirror gravatar imageopalmirror ( 2018-04-13 17:32:33 -0500 )edit

you probably want to convert the image to 8U before using imshow, also. See

opalmirror gravatar imageopalmirror ( 2018-04-13 17:37:18 -0500 )edit

you probably want to convert the image to 8U before using imshow, also. See

That's actually the solution the the problem I was looking for as a saved image was fine. Thanks.

mfischer-gundlach gravatar imagemfischer-gundlach ( 2018-04-16 01:34:14 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-04-13 05:12:50 -0500

Seen: 163 times

Last updated: Apr 13 '18