2

I'm writing a function in OpenCV to compute v and u-disparities, so I need first the disparity image. I set sgbm.minDisparity = 0 and numberOfDisparities = 160.

The disparity image is CV_16SC1, and I need Unsigned values to go on programming my function.

I printed the whole Mat and there are negative values and values above 160. If I understood well the documentation, the disparity image represents the disparity values*16. Does that mean that the maximum value is 16*160 in my case?. If not, what could be wrong?. And anyway, why there are values less than zero when minDisparity is set to 0? Here's the code:

void Stereo_SGBM(){
    int numberOfDisparities;
    StereoSGBM sgbm;
    Mat img1, img2;

    img1=left_frame;   //left and right frames are global variables
    img2=right_frame;

    Size img_size = img1.size();

    //I make sure the number of disparities is divisible by 16
    numberOfDisparities = 160;  

    int cn=1;   //Grayscale

    sgbm.preFilterCap = 63;
    sgbm.SADWindowSize = 3;
    sgbm.P1 = 8*cn*sgbm.SADWindowSize*sgbm.SADWindowSize;
    sgbm.P2 = 32*cn*sgbm.SADWindowSize*sgbm.SADWindowSize;
    sgbm.minDisparity = 0;
    sgbm.numberOfDisparities = numberOfDisparities;
    sgbm.uniquenessRatio = 10;
    sgbm.speckleWindowSize = 100;
    sgbm.speckleRange = 2;
    sgbm.disp12MaxDiff = 1;
    sgbm.fullDP = false;  

    Mat disp;   // CV_16SC1
    Mat disp8;  //CV_8UC1 (used later in the code


    sgbm(img1, img2, disp); 
    //disp contains negative values and larger than 160!!!

    //img1 and img2 are left and right channels of size 1242x375 grayscale    
}
agregorio
  • 91
  • 1
  • 11

1 Answers1

1

The way I see it, the disparity is meant to be a float, and it reflects on the parameters. If you convert the result to float, and divide by 16, things makes a little more sense:

The algorithm apparently reports -1 (actually minDisparity - 1) where it could not match. And numberOfDisparities is more "max disparity - min disparity", rather than an actual number of values.

For example, if you give minDisparity=2 and numberOfDisparities=144, you will get results in the range: 1.0 - 145.0. The number of different values will actually be 144*16 because it goes in 1/16 increments.

So yes, in your case, using integers, this means you will get 16*160 max value.

Gnurfos
  • 980
  • 2
  • 10
  • 29