Search code examples
c++exceptionopencvstereo-3d

opencv gives exception with cvFindStereoCorrespondenceBM


I wrote a code to find disparity map from two images "left" and "right"; and saving the map in "stereo". The code seems okay to me but somehow I'm getting exception:

On the terminal window:

OpenCV Error: Bad argument (Unknown array type) in unknown function, file ......\src\opencv\modules\core\src\matrix.cpp, line 698

The code is

Mat left = imread( "files\\left.jpg" );
Mat right = imread( "files\\right.jpg" );
Size size = left.size();
namedWindow( "left", CV_WINDOW_AUTOSIZE );
imshow("left", left);
cvNamedWindow( "right", CV_WINDOW_AUTOSIZE );
imshow("right", right);

Mat left_C1;
cvtColor(left, left_C1, CV_RGB2GRAY);
Mat right_C1;
cvtColor(right, right_C1, CV_RGB2GRAY);

Mat stereo = Mat(size, CV_16SC1);
CvStereoBMState* state = cvCreateStereoBMState();

state->preFilterSize=5;
state->preFilterCap=1;
state->SADWindowSize=5;
state->minDisparity=0;
state->numberOfDisparities=64;
state->textureThreshold=0;
state->uniquenessRatio=0;

cvFindStereoCorrespondenceBM(&left_C1, &right_C1, &stereo, state);

namedWindow( "stereo", CV_WINDOW_AUTOSIZE );
imshow("stereo",stereo);
waitKey(0);

I'm using VS ultimate 2012, Windows 8, OpenCV 2.4.4. Please guide me how to fix this exception.


Solution

  • I think cvFindStereoCorrespondenceBM requires IplImage or cvMat. Since you are using Mat, you should use C++ API of OpenCV.

    StereoBM sbm;
    sbm.state->SADWindowSize = 23;
    sbm.state->numberOfDisparities = 96;
    sbm.state->preFilterSize = 25;
    sbm.state->preFilterCap = 63;
    sbm.state->minDisparity = 0;
    sbm.state->textureThreshold = 20;
    sbm.state->uniquenessRatio = 10;
    sbm.state->speckleWindowSize = 25;
    sbm.state->speckleRange = 8;
    sbm.state->disp12MaxDiff = 1;
    // change the parameters
    
    sbm(left_c1, right_c1, stereo);
    

    Since you are using Mat, you should try this and check if it works or not.

    cvFindStereoCorrespondenceBM(left_C1, right_C1, stereo, state);
    

    I don't know if this will work or not, but the C++ API StereoBM will surely work.

    Try this and see if it works.

    #define WIDTH 426
    #define HEIGHT 320
    

    You can define some size.

    CvSize imageSize = {WIDTH,HEIGHT};
    CvMat* frame1r = cvCreateMat( imageSize.height,imageSize.width, CV_8U );
    CvMat* frame2r = cvCreateMat( imageSize.height,imageSize.width, CV_8U );
    CvMat* disp = cvCreateMat( imageSize.height, imageSize.width, CV_16S );
    CvMat* vdisp = cvCreateMat( imageSize.height,imageSize.width, CV_8U );
    
    // Load image in frame1r and frame2r
    
    cvFindStereoCorrespondenceBM( frame1r, frame2r, disp, state);
    cvNormalize( disp, vdisp, 0, 256, CV_MINMAX );                   
    cvShowImage( "disparity", vdisp );