Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Manually set up stereo projection matrices?

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;
    vector<float> vec{ fx,0,cx,0,fy,cy,0,0,1 };

    //intrinsics
    cv::Mat K(vec);

    float k1 = 0.031989690067704447;
    float k2 = -0.11373776380163705;
    float p1 = 0.0;
    float p2 = 0.0;
    float k3 = 0.11337076724792615;
    vector<float> dist{ k1,k2,p1,p2,k3 };

    //distortion
    cv::Mat D(dist);


    //extrinsics

//rotation ( images are rectified, so this is zero)
cv::Mat R = cv::Mat::zeros(3, 3, CV_32F); 

    //translation. (stereo camera, rectified images, 12 cm baseline)
    vector<float> trans{ 0,0,12};
    cv::Mat t(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;

    cv::Mat O1 = cv::Mat::zeros(3, 1, CV_32F);

    // 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;
}

which gives me the following error:

OpenCV Error: Assertion failed (!fixedSize() || ((Mat*)obj)->size.operator()() == Size(_cols, _rows)) in cv::_OutputArray::create, file D:\opencv-master2017\opencv-master\modules\core\src\matrix.cpp, line 2287

Where am i going wrong here?

thank you!

Manually set up stereo projection matrices?

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;
    vector<float> vec{ 
    float data[9] = { fx,0,cx,0,fy,cy,0,0,1 };

    //intrinsics
    cv::Mat K(vec);
//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;
    vector<float> dist{ float dist[5] ={ k1,k2,p1,p2,k3 };

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


    //extrinsics
  //rotation ( images are rectified, so this is zero)
 cv::Mat R = cv::Mat::zeros(3, 3, CV_32F); CV_32F);


    //translation. (stereo camera, rectified images, 12 cm baseline)
    vector<float> trans{ float trans[3] = { 0,0,12};
    cv::Mat t(trans);
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;

    cv::Mat O1 = cv::Mat::zeros(3, 1, CV_32F);

    // 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 error:following:

OpenCV Error: Assertion failed (!fixedSize() || ((Mat*)obj)->size.operator()() == Size(_cols, _rows)) in cv::_OutputArray::create, file D:\opencv-master2017\opencv-master\modules\core\src\matrix.cpp, line 2287
matrix P1
[333.02081, 0, 318.51651, 0;
 0, 333.02081, 171.93558, 0;
 0, 0, 1, 0]
matrix P2
[0, 0, 0, 3822.1982;
 0, 0, 0, 2063.2271;
 0, 0, 0, 12]

this seems incorrect.

Where am i going wrong here?

thank you!

Manually set up stereo projection matrices?

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
 //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::zeros(3, 3, CV_32F);

cv::Mat(3,3, CV_32F, rots);

    //translation. (stereo camera, rectified images, 12 cm baseline)
    float trans[3] = { 0,0,12};
    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
[0, 0, 0, [333.02081, 0, 318.51651, 3822.1982;
 0, 0, 0, 333.02081, 171.93558, 2063.2271;
 0, 0, 0, 1, 12]

this seems incorrect.

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

thank you!

Manually set up stereo projection matrices?

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] = { 0,0,12};
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, 3822.1982;
39.962498;
 0, 333.02081, 171.93558, 2063.2271;
0;
 0, 0, 1, 12]
0]

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

thank you!