39 Mat2X points(2, markers.size());
40 for (
int i = 0;
i < markers.size(); ++
i) {
41 points(0,
i) = markers[
i].x;
42 points(1,
i) = markers[
i].y;
54struct EuclideanResectCostFunction {
56 typedef Vec FMatrixType;
57 typedef Vec6 XMatrixType;
60 const EuclideanReconstruction& reconstruction,
61 const Mat3& initial_R)
63 reconstruction(reconstruction),
64 initial_R(initial_R) {}
71 Vec3 t = dRt.tail<3>();
74 Vec residuals(2 * markers.size());
76 for (
int i = 0;
i < markers.size(); ++
i) {
77 const EuclideanPoint& point =
78 *reconstruction.PointForTrack(markers[
i].track);
79 Vec3 projected =
R * point.
X + t;
80 projected /= projected(2);
81 residuals[2 *
i + 0] = projected(0) - markers[
i].x;
82 residuals[2 *
i + 1] = projected(1) - markers[
i].y;
88 const EuclideanReconstruction& reconstruction;
89 const Mat3& initial_R;
97 if (markers.size() < 5) {
100 Mat2X points_2d = PointMatrixFromMarkers(markers);
101 Mat3X points_3d(3, markers.size());
102 for (
int i = 0;
i < markers.size();
i++) {
105 LG <<
"Points for resect:\n" << points_2d;
114 LG <<
"Resection for image " << markers[0].image <<
" failed;"
115 <<
" trying fallback projective resection.";
117 LG <<
"No fallback; failing resection for " << markers[0].image;
126 Mat4X points_3d_homogeneous(4, markers.size());
127 for (
int i = 0;
i < markers.size();
i++) {
128 points_3d_homogeneous.col(
i).head<3>() = points_3d.col(
i);
129 points_3d_homogeneous(3,
i) = 1.0;
132 if ((
P * points_3d_homogeneous.col(0))(2) < 0) {
133 LG <<
"Point behind camera; switch sign.";
141 Eigen::JacobiSVD<Mat3> svd(
R, Eigen::ComputeFullU | Eigen::ComputeFullV);
143 LG <<
"Resection rotation is: " << svd.singularValues().transpose();
144 LG <<
"Determinant is: " <<
R.determinant();
147 R = svd.matrixU() * svd.matrixV().transpose();
149 Vec3 xx =
R * points_3d.col(0) + t;
151 LG <<
"Final point is still behind camera...";
160 EuclideanResectCostFunction resect_cost(markers, *reconstruction,
R);
164 Vec6 dRt = Vec6::Zero();
167 Solver solver(resect_cost);
169 Solver::SolverParameters
params;
170 solver.minimize(
params, &dRt);
171 LG <<
"LM found incremental rotation: " << dRt.head<3>().
transpose();
178 LG <<
"Resection for image " << markers[0].image <<
" got:\n"
192struct ProjectiveResectCostFunction {
194 typedef Vec FMatrixType;
195 typedef Vec12 XMatrixType;
198 const ProjectiveReconstruction& reconstruction)
199 : markers(markers), reconstruction(reconstruction) {}
203 Map<const Mat34>
P(vector_P.data(), 3, 4);
206 Vec residuals(2 * markers.size());
208 for (
int i = 0;
i < markers.size(); ++
i) {
209 const ProjectivePoint& point =
210 *reconstruction.PointForTrack(markers[
i].track);
211 Vec3 projected =
P * point.X;
212 projected /= projected(2);
213 residuals[2 *
i + 0] = projected(0) - markers[
i].x;
214 residuals[2 *
i + 1] = projected(1) - markers[
i].y;
220 const ProjectiveReconstruction& reconstruction;
227 if (markers.size() < 5) {
232 Mat2X points_2d = PointMatrixFromMarkers(markers);
233 Mat4X points_3d_homogeneous(4, markers.size());
234 for (
int i = 0;
i < markers.size();
i++) {
235 points_3d_homogeneous.col(
i) =
238 LG <<
"Points for resect:\n" << points_2d;
245 if ((
P * points_3d_homogeneous.col(0))(2) < 0) {
246 LG <<
"Point behind camera; switch sign.";
255 ProjectiveResectCostFunction resect_cost(markers, *reconstruction);
260 Solver solver(resect_cost);
262 Solver::SolverParameters
params;
263 solver.minimize(
params, &vector_P);
269 LG <<
"Resection for image " << markers[0].image <<
" got:\n"
EuclideanPoint * PointForTrack(int track)
Returns a pointer to the point corresponding to track.
void InsertCamera(int image, const Mat3 &R, const Vec3 &t)
ProjectivePoint * PointForTrack(int track)
Returns a pointer to the point corresponding to track.
void InsertCamera(int image, const Mat34 &P)
MatBase< R, C > transpose(MatBase< C, R >) RET
bool EuclideanResection(const Mat2X &x_camera, const Mat3X &X_world, Mat3 *R, Vec3 *t, ResectionMethod method)
void Resection(const Matrix< T, 2, Dynamic > &x, const Matrix< T, 4, Dynamic > &X, Matrix< T, 3, 4 > *P)
Eigen::Matrix< double, 6, 1 > Vec6
bool ProjectiveResect(const vector< Marker > &markers, ProjectiveReconstruction *reconstruction)
Mat3 RotationFromEulerVector(Vec3 euler_vector)
Returns the rotaiton matrix built from given vector of euler angles.
Eigen::Matrix< double, 3, 3 > Mat3
bool EuclideanResect(const vector< Marker > &markers, EuclideanReconstruction *reconstruction, bool final_pass)
void KRt_From_P(const Mat34 &P, Mat3 *Kp, Mat3 *Rp, Vec3 *tp)
Eigen::Matrix< double, 3, 4 > Mat34
std::vector< ElementType, Eigen::aligned_allocator< ElementType > > vector
Eigen::Matrix< double, 4, Eigen::Dynamic > Mat4X
Eigen::Matrix< double, 3, Eigen::Dynamic > Mat3X
Eigen::Matrix< double, 12, 1 > Vec12
Eigen::Matrix< double, 2, Eigen::Dynamic > Mat2X