Search code examples
opencv

Opencv StereoRectify Issue


Working with exaple, provided with opencv. After I calibrated stereo pair with RMS around 0.66, I tried to make a 3d point cloud. Unfortunatelly, after I do stereoRectify, my input images turn black. And no other work is possible. May it happen because I made bad calibration?

Mat img1 = imread(img1_filename, IMREAD_GRAYSCALE);
Mat img2 = imread(img2_filename, IMREAD_GRAYSCALE);

if (img1.empty())
{
    printf("Command-line parameter error: could not load the first input image file\n");
    return -1;
}
if (img2.empty())
{
    printf("Command-line parameter error: could not load the second input image file\n");
    return -1;
}
scale = 1;
if (scale != 1.f)
{
    Mat temp1, temp2;
    int method = scale < 1 ? INTER_AREA : INTER_CUBIC;
    resize(img1, temp1, Size(), scale, scale, method);
    img1 = temp1;
    resize(img2, temp2, Size(), scale, scale, method);
    img2 = temp2;
}

Size img_size = img1.size();

Rect roi1, roi2;

Mat Q;

if (!intrinsic_filename.empty())
{
    // reading intrinsic parameters
    FileStorage fs(intrinsic_filename, FileStorage::READ);
    if (!fs.isOpened())
    {
        printf("Failed to open file %s\n", intrinsic_filename.c_str());
        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(extrinsic_filename, FileStorage::READ);
    if (!fs.isOpened())
    {
        printf("Failed to open file %s\n", extrinsic_filename.c_str());
        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, CV_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);

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

    img1 = img1r;
    img2 = img2r;
    imshow("img1", img1);
    imshow("img2", img2);
    cv::waitKey(0);
}

Solution

  • according to : http://answers.opencv.org/question/93090/stereo-rectify-doesnt-rectify-even-with-correct-m-and-d/

    changing the stereoCalibrate flags in opencv calibration example solves this issue. For example using: cv::CALIB_RATIONAL_MODEL + cv::CALIB_FIX_K3 + cv::CALIB_FIX_K4 + cv::CALIB_FIX_K5 + cv::CALIB_FIX_K6

    Another thing that gave larger RMS error, but at least a correct remap was calibrating intrinsics and extrinsics together. So DO NOT USE cv::CALIB_USE_INTRINSIC_GUESS + cv::CALIB_FIX_INTRINSIC