Skip to content

Instantly share code, notes, and snippets.

@chiral
Last active February 1, 2016 11:46
Show Gist options
  • Save chiral/24125e7f83bf18eff07e to your computer and use it in GitHub Desktop.
Save chiral/24125e7f83bf18eff07e to your computer and use it in GitHub Desktop.
convert from fundamental matrix to camera pose (R1, R2, T) in OpenCV2 (similar to decomposeEssentialMat() in OpenCV 3)
/*
you can use the result matrix (rot1,rot2,tvec) as:
candidate1: rot1, tvec
candidate2: rot1, -tvec
candidate3: rot2, tvec
candidate4: rot2, -tvec
*/
bool convertF2RT(Mat &F,Mat &K,Mat &rot1,Mat &rot2,Mat &tvec) {
if (F.empty() || K.empty()) return false;
Mat Kt;
transpose(K,Kt);
SVD svd(Kt*F*K,SVD::MODIFY_A);
Mat W = (Mat_<double>(3,3) << 0,-1,0,1,0,0,0,0,1);
Mat Wt = (Mat_<double>(3,3) << 0,1,0,-1,0,0,0,0,1);
rot1 = svd.u * W * svd.vt;
rot2 = svd.u * Wt * svd.vt;
svd.u.col(2).copyTo(tvec);
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment