Ask Your Question
0

cv::fisheye::stereoRectify giving black image.

asked 2019-12-04 10:52:18 -0600

antithing gravatar image

updated 2019-12-04 11:07:29 -0600

I am trying to create a depth map from a fisheye stereo camera. I have intrinsic and extrinsic data from the camera SDK, so it can be trusted.

When I undistort the image using cv::fisheye::stereoRectify and then cv::fisheye::initUndistortRectifyMap and cv::remap, I get a black image.

When I print the P1 matrix after cv::fisheye::stereoRectify, it looks odd:

//before [284.49261, 0, 425.80771; 0, 285.6832, 395.49429; 0, 0, 1]

//after [108.90381630035, 0, 530.2634673508817, 0; 0, 108.90381630035, -8615.97446825623, 0; 0, 0, 1, 0]

Am I wrong in thinking that these two matrices should be the same? P1 = K1 * T, is that not correct?

If I use:

    cv::Mat lmapx, lmapy, rmapx, rmapy;
    cv::fisheye::initUndistortRectifyMap(K1, D1, cv::NoArray(), K1, imgSize, CV_16SC2, lmapx, lmapy);

the undistortion looks good. But I am stuck on the Rectify.

My code is:

leftImg = imread(leftFile_ss, cv::IMREAD_GRAYSCALE);
        rightImg = imread(rightFile_ss, cv::IMREAD_GRAYSCALE);

        //load cal data
        cv::Size imgSize(leftImg.cols, leftImg.rows);

        FileStorage fs("T265.xml", FileStorage::READ);
        cv::Mat K1, K2, D1, D2, R, T;
        fs["K1"] >> K1;
        fs["K2"] >> K2;
        fs["D1"] >> D1;
        fs["D2"] >> D2;
        fs["R"] >> R;
        fs["T"] >> T;

        std::cout << K1 << std::endl;

        double balance = 0.0, fov_scale = 1.0;
        cv::Mat R1, R2, P1, P2, Q;
        cv::fisheye::stereoRectify(K1, D1, K2, D2, imgSize, R, T, R1, R2, P1, P2, Q,
        cv::CALIB_ZERO_DISPARITY);

        std::cout << P1 << std::endl;


        cv::Mat lmapx, lmapy, rmapx, rmapy;
        cv::fisheye::initUndistortRectifyMap(K1, D1, R1, P1, imgSize, CV_16SC2, lmapx, lmapy);
        cv::Mat undist_left;
        cv::remap(leftImg, undist_left, lmapx, lmapy, cv::INTER_LINEAR);


        cv::imshow("undL", undist_left);
        cv::waitKey(0);

What am i doing wrong? How can i rectify these two images?

Thank you!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2019-12-05 08:23:36 -0600

antithing gravatar image

Setting the cx and cy values manually to the real middle of the image size has helped me here.

Creating the Projection matrices like this helped me get a pretty good result.

 //LEFT
        float fx = camera_matrixL.at<float>(0, 0);
        float fy = camera_matrixL.at<float>(1, 1);

        //Set mid point to actual pixel center
        cv::Mat_<float> P_left(3, 4);
        P_left << fx, 0, 424, 0,
            0, fy, 400, 0,
            0, 0, 1, 0;

//RIGHT

                float fx = camera_matrixR.at<float>(0, 0);
        float fy = camera_matrixR.at<float>(1, 1);

        //P2 gets the baseline added
        cv::Mat_<float> P_right(3, 4);
        P_right << fx, 0, 424, T.at<float>(0, 0) * 10,
            0, fy, 400, 0,
            0, 0, 1, 0;
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-12-04 10:52:18 -0600

Seen: 1,740 times

Last updated: Dec 05 '19