Manually set up stereo projection matrices?

asked 2017-06-25 14:27:27 -0500

stillNovice gravatar image

updated 2017-06-25 14:45:49 -0500

I have a stereo camera system, and need to get my Projection matrices in order to triangulate points. As i already have the intrinsic, extrinsics and distortion values, I would like to just manually plug these in to get the P1 and P2 matrices.

My images are rectified, so the rotation matrix is zero, and the translation is just the 12cm baseline. I have the following code:

void main()
{
    float fx = 333.0208275693896;
    float fy = 333.0208275693896;
    float cx = 318.51652340332424;
    float cy = 171.93557987330718;

    float data[9] = { fx,0,cx,0,fy,cy,0,0,1 };

    //intrinsics    
    cv::Mat K = cv::Mat(3, 3, CV_32F, data);

    float k1 = 0.031989690067704447;
    float k2 = -0.11373776380163705;
    float p1 = 0.0;
    float p2 = 0.0;
    float k3 = 0.11337076724792615;
    float dist[5] ={ k1,k2,p1,p2,k3 };

    //distortion
    cv::Mat D = cv::Mat(5, 1, CV_32F, dist);


    //extrinsics
    float tx = 0.12;
    float ty = 0;
    float tz = 0;

    //rotation ( images are rectified, so this is zero)
    float rots[9] = { 1,0,0,0,1,0,0,0,1 };
    cv::Mat R = cv::Mat(3,3, CV_32F, rots);


    //translation. (stereo camera, rectified images, 12 cm baseline)
    float trans[3] = { tx,ty,tz};
    cv::Mat t = cv::Mat(3, 1, CV_32F, trans);


    // Camera 1 Projection Matrix K[I|0]
    cv::Mat P1(3, 4, CV_32F, cv::Scalar(0));
    K.copyTo(P1.rowRange(0, 3).colRange(0, 3));

    std::cout << "matrix P1" << std::endl;
    std::cout << P1 << std::endl;

    // Camera 2 Projection Matrix K[R|t]
    cv::Mat P2(3, 4, CV_32F);
    R.copyTo(P2.rowRange(0, 3).colRange(0, 3));
    t.copyTo(P2.rowRange(0, 3).col(3));
    P2 = K*P2;

    std::cout << "matrix P2" << std::endl;
    std::cout << P2 << std::endl;

    cin.get();

}

which gives me the following:

matrix P1
[333.02081, 0, 318.51651, 0;
 0, 333.02081, 171.93558, 0;
 0, 0, 1, 0]
matrix P2
[333.02081, 0, 318.51651, 39.962498;
 0, 333.02081, 171.93558, 0;
 0, 0, 1, 0]

Before i go any further i want to check, am i doing this correctly? Do the returned matrices look correct?

thank you!

edit retag flag offensive close merge delete