c++opencvcamera-calibrationdisparity-mapping

Disparity map's color is reversed


The result of my disparity map is good. As I know, close items should be light and far items are dark. The opposite happens with me.

This is my code:

CvStereoBMState *BMState = cvCreateStereoBMState();
    assert(BMState != 0);
    BMState->preFilterSize=31/*41*/;
    BMState->preFilterCap=41/*31*/;
    BMState->SADWindowSize=31/*41*/;
    BMState->minDisparity=-0/*-64*/;
    BMState->numberOfDisparities=112/*128*/;

    try
    {
    while(true)
    {
        if(true)
        {

        if( frameL && frameR )
        {
            CvMat part;
            cvRemap( gFrameL, img1r, mx1, my1 );
            cvRemap( gFrameR, img2r, mx2, my2 );
            if( true/*!isVerticalStereo || useUncalibrated != 0 */)
            {

                cvFindStereoCorrespondenceBM( img1r, img2r, disp,BMState);

                cvNormalize( disp, vdisp, 0, 256, CV_MINMAX );
                cvNamedWindow( "disparity" );
                cvShowImage( "disparity", vdisp );
            }
        }
      }
frameL = cvQueryFrame(capLeft);
frameR= cvQueryFrame(capRight);
cvCvtColor(frameL,gFrameL,CV_BGR2GRAY);
cvCvtColor(frameR,gFrameR,CV_BGR2GRAY);
    } 

How can I fix this problem?


Solution

  • Disparity is the offset in (horizontal) pixel units between a point in one stereo image to the other, where both points are images of the same 3D world point. If your images are switched the direction of the disparity would be flipped as well. Try switching the input images.

    If your left and right images are switched, then the disparity might be negative.
    After the cvNormalize() you will get proper positive values with smaller values representing close objects.