Hello,
I try to compute the fundamental matrix given the following camera calibration parameters:
- camera Matrix 1/2 (camMat1, camMat2)
- rotation Vector 1/2 (rotVec1, rotVec2)
- translation Vector 1/2 (transVec1, transVec2)
According to the following formula the fundamental matrix F is computed by:
F = inverse(transpose(camMat1)) * R * S * inverse(camMat2)
Anyway, i am quite a bit lost how to compute R and S. I know that R is the rotation matrix which brings image 1 into image 2. Also i know that S is the translation vector to transform image 1 into image 2. My plan would be:
1) Rodrigues both rotation Vectors and substract rotation Matrix 1 from rotation Matrix 2
cv::Mat rotMat1, rotMat2;
cv::Rodrigues(rotVec1[0], rotMat1);
cv::Rodrigues(rotVec2[0], rotMat2);
cv::Mat R = rotMat2 - rotMat1;
2) Substract translation Vector 1 from translation Vector 2
T = transVec2 - transVec1
3) Compose S
S = [0,-T[3], T[2]; T[3], 0, -T[1]; -T[2], T[1], 0];
Would this be correct? Any help on this topic would be appreciated.
I hope this is not offtopic since it is not directly related to OpenCV.
Edit: I worked in the solution you provided. I cross checked it with cv::stereoCalib(). Unfortunatly the matrizes does not match. Any suggestions what i did wrong?
cv::Mat computeFundMat(cv::Mat camMat1, cv::Mat camMat2, vector<cv::Mat> rotVec1,
vector<cv::Mat> rotVec2, vector<cv::Mat> transVec1, vector<cv::Mat> transVec2)
{
cv::Mat rotMat1(3, 3, CV_64F), rotMat2(3, 3, CV_64F);
cv::Mat transVec2toWorld(3, 1, CV_64F);
cv::Mat R(3, 3, CV_64F), T(3, 1, CV_64F), S(cv::Mat::zeros(3, 3, CV_64F));
cv::Mat F(3, 3, CV_64F);
//Convert rotation vector into rotation matrix
cv::Rodrigues(rotVec1.at(0), rotMat1);
cv::Rodrigues(rotVec2.at(0), rotMat2);
//Transform prameters of camera 2 into world
rotMat2 = rotMat2.t();
transVec2toWorld = (-rotMat2 * transVec2.at(0));
//Compute R, T to rotate, translate image b into image a
R = rotMat2 * rotMat1; //or --> rotMat2 * rotMat1 ???
T = transVec1.at(0) + transVec2toWorld;
//Compose skew matrix (Format: S =
// [0,-T[3], T[2]; -> 1
S.at<double>(0, 1) = -T.at<double>(2); S.at<double>(0, 2) = T.at<double>(1); //-> 1
// T[3], 0, -T[1]; -> 2
S.at<double>(1, 0) = T.at<double>(2); S.at<double>(1, 2) = -T.at<double>(0); //-> 2
// -T[2], T[1], 0];) -> 3
S.at<double>(2, 0) = -T.at<double>(1); S.at<double>(2, 1) = T.at<double>(0); //-> 3
//Compute fundamental matrix (F = inverse(transpose(camMat2)) * R * S * inverse(camMat1))
return cv::Mat(camMat2.t().inv() * R * S * camMat1.inv());
}