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!