4
votes

I am trying to make disparity map. I saw the sample code provided by opencv 'stereo_match.cpp' and I wrote the following code. But when I display the left and right image after rectification and remapping the image is black. Can anybody tell me where am I doing wrong?

int main(int argc, char* argv[])
{
    Mat img1, img2, g1, g2;
    Mat disp, disp8;
    char* method ="SGBM";
    float scale = 1.f; // don't know why
    //img1 = imread(argv[1]);
    //img2 = imread(argv[2]);
    img1=imread("l1.jpg");
    img2=imread("r1.jpg");
    cvtColor(img1, g1, CV_BGR2GRAY);
    cvtColor(img2, g2, CV_BGR2GRAY);
    Size img_size = img1.size();
    Rect roi1, roi2;
    Mat Q;

    /*reading parameters of ectrinssic & intrinssic file*/
    const char* intrinsic_filename="intrinsics";
    Mat img1r, img2r;
    if( intrinsic_filename )
    {
        FileStorage fs("intrinsics.yml", cv::FileStorage::READ);
        if(!fs.isOpened())
        {
            printf("Failed to open file %s\n");
            return -1;
        }

        Mat M1, D1, M2, D2;
        fs["M1"] >> M1;
        fs["D1"] >> D1;
        fs["M2"] >> M2;
        fs["D2"] >> D2;

        M1 *= scale;
        M2 *= scale;

        fs.open("extrinsics.yml", cv::FileStorage::READ);
        if(!fs.isOpened())
        {
            printf("Failed to open file %s\n");
            return -1;
        }

        Mat R, T, R1, P1, R2, P2;
        fs["R"] >> R;
        fs["T"] >> T;
        stereoRectify( M1, D1, M2, D2, img_size, R, T, R1, R2, P1, P2, Q, CALIB_ZERO_DISPARITY, -1, img_size, &roi1, &roi2 );

        Mat map11, map12, map21, map22;
        initUndistortRectifyMap(M1, D1, R1, P1, img_size, CV_16SC2, map11, map12);
        initUndistortRectifyMap(M2, D2, R2, P2, img_size, CV_16SC2, map21, map22);


        remap(img1, img1r, map11, map12, INTER_LINEAR);
        remap(img2, img2r, map21, map22, INTER_LINEAR);


        //  img1 = img1r;
        // img2 = img2r;

        imshow("left1", img1r);
        imshow("left2", img2r); 

    }
}

output left image} output right image ![original left image][3]

3

3 Answers

0
votes

Didn't you switch the input/output parameters on cvtColor(...) in:

cvtColor(img1, g1, CV_BGR2GRAY);
cvtColor(img2, g2, CV_BGR2GRAY);

g1 and g2 seem to be the grayscale version of the input images, but they are never used in the code that follows. Why don't you just try it on the other way round?:

g1=imread("l1.jpg");
g2=imread("r1.jpg");
cvtColor(g1, img1, CV_BGR2GRAY);
cvtColor(g2, img2, CV_BGR2GRAY);
0
votes

Disparity maps come out of OpenCV in a scale that doesn't show a lot of contrast on 8-bit displays. You need to re-map the image range to increase the contrast.

To improve this answer, here is the code I use in Python to do this:

disp = cv2.normalize(sgbm.compute(ri_l, ri_r), alpha=0, beta=255, \
    norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)

sgmb.compute() is the disparity map generation function.

0
votes

if remap created only gray images, that means, that your intrinsic/extrinsic values were bad.

you'll have to go back to the calibration stage, and do that again, until you come out with an error < 1 from stereoCalibrate.