22#ifndef EOS_ORTHOGRAPHIC_CAMERA_ESTIMATION_LINEAR_HPP
23#define EOS_ORTHOGRAPHIC_CAMERA_ESTIMATION_LINEAR_HPP
25#include "eos/cpp17/optional.hpp"
73 std::vector<Eigen::Vector2f> image_points, std::vector<Eigen::Vector4f> model_points,
74 bool is_viewport_upsidedown, cpp17::optional<int> viewport_height = cpp17::nullopt)
77 assert(image_points.size() == model_points.size());
78 assert(image_points.size() >= 4);
80 const int num_correspondences =
static_cast<int>(image_points.size());
82 if (is_viewport_upsidedown)
84 if (viewport_height == cpp17::nullopt)
86 throw std::runtime_error(
87 "Error: If is_viewport_upsidedown is set to true, viewport_height needs to be given.");
89 for (
auto&& ip : image_points)
91 ip[1] = viewport_height.value() - ip[1];
95 Matrix<float, Eigen::Dynamic, 8> A = Matrix<float, Eigen::Dynamic, 8>::Zero(2 * num_correspondences, 8);
96 for (
int i = 0; i < model_points.size(); ++i)
98 A.block<1, 4>(2 * i, 0) = model_points[i];
99 A.block<1, 4>((2 * i) + 1, 4) = model_points[i];
102 Matrix<float, Eigen::Dynamic, 1> b(2 * num_correspondences, 1);
103 for (
int i = 0; i < image_points.size(); ++i)
105 b.segment<2>(2 * i) = image_points[i];
110 const Matrix<float, 8, 1> k = A.colPivHouseholderQr().solve(b);
113 const Eigen::Vector3f R1 = k.segment<3>(0);
114 const Eigen::Vector3f R2 = k.segment<3>(4);
115 const float sTx = k(3);
116 const float sTy = k(7);
117 const auto s = (R1.norm() + R2.norm()) / 2.0f;
119 Eigen::Vector3f r1 = R1.normalized();
120 Eigen::Vector3f r2 = R2.normalized();
121 R.block<1, 3>(0, 0) = r1;
122 R.block<1, 3>(1, 0) = r2;
123 R.block<1, 3>(2, 0) = r1.cross(r2);
126 Eigen::JacobiSVD<Eigen::Matrix3f, Eigen::NoQRPreconditioner> svd(R, Eigen::ComputeFullU | Eigen::ComputeFullV);
127 Eigen::Matrix3f U = svd.matrixU();
128 const Eigen::Matrix3f V = svd.matrixV();
129 Eigen::Matrix3f R_ortho = U * V.transpose();
132 if (R_ortho.determinant() < 0)
134 U.block<1, 3>(2, 0) = -U.block<1, 3>(2, 0);
135 R_ortho = U * V.transpose();
139 const auto t1 = sTx / s;
140 const auto t2 = sTy / s;
ScaledOrthoProjectionParameters estimate_orthographic_projection_linear(std::vector< Eigen::Vector2f > image_points, std::vector< Eigen::Vector4f > model_points, bool is_viewport_upsidedown, cpp17::optional< int > viewport_height=cpp17::nullopt)
Definition: orthographic_camera_estimation_linear.hpp:72
Namespace containing all of eos's 3D model fitting functionality.
Definition: orthographic_camera_estimation_linear.hpp:42
Eigen::Matrix3f R
3x3 rotation matrix
Definition: orthographic_camera_estimation_linear.hpp:43
float ty
x and y translation
Definition: orthographic_camera_estimation_linear.hpp:44
float s
Scaling.
Definition: orthographic_camera_estimation_linear.hpp:45