eos 1.4.0
Loading...
Searching...
No Matches
nonlinear_camera_estimation.hpp
1/*
2 * eos - A 3D Morphable Model fitting library written in modern C++11/14.
3 *
4 * File: include/eos/fitting/nonlinear_camera_estimation.hpp
5 *
6 * Copyright 2015, 2023 Patrik Huber
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20#pragma once
21
22#ifndef EOS_NONLINEAR_CAMERA_ESTIMATION_HPP
23#define EOS_NONLINEAR_CAMERA_ESTIMATION_HPP
24
25#include "eos/fitting/detail/nonlinear_camera_estimation_detail.hpp"
26#include "eos/fitting/RenderingParameters.hpp"
27
28#include "Eigen/Geometry"
29#include "unsupported/Eigen/NonLinearOptimization"
30
31#include <vector>
32#include <cassert>
33
34namespace eos {
35namespace fitting {
36
63inline RenderingParameters estimate_orthographic_camera(std::vector<Eigen::Vector2f> image_points,
64 std::vector<Eigen::Vector4f> model_points, int width,
65 int height)
66{
67 assert(image_points.size() == model_points.size());
68 assert(image_points.size() >= 6); // Number of correspondence points given needs to be equal to or larger than 6
69
70 const float aspect = static_cast<float>(width) / height;
71
72 // Set up the initial parameter vector and the cost function:
73 int num_params = 6;
74 Eigen::VectorXd parameters; // [rot_x_pitch, rot_y_yaw, rot_z_roll, t_x, t_y, frustum_scale]
75 parameters.setConstant(num_params, 0.0); // Set all 6 values to zero (except frustum_scale, see next line)
76 parameters[5] = 110.0; // This is just a rough hand-chosen scaling estimate - we could do a lot better. But it works.
77 detail::OrthographicParameterProjection cost_function(image_points, model_points, width, height);
78
79 // Note: we have analytical derivatives, so we should use them!
80 Eigen::NumericalDiff<detail::OrthographicParameterProjection> cost_function_with_derivative(cost_function,
81 0.0001);
82 // I had to change the default value of epsfcn, it works well around 0.0001. It couldn't produce the
83 // derivative with the default, I guess the changes in the gradient were too small.
84
85 Eigen::LevenbergMarquardt<Eigen::NumericalDiff<detail::OrthographicParameterProjection>> lm(
86 cost_function_with_derivative);
87 const auto info = lm.minimize(parameters); // we could or should use the return value
88 // 'parameters' contains the solution now.
89
90 Frustum camera_frustum{
91 -1.0f * aspect * static_cast<float>(parameters[5]), 1.0f * aspect * static_cast<float>(parameters[5]),
92 -1.0f * static_cast<float>(parameters[5]), 1.0f * static_cast<float>(parameters[5])};
93 return RenderingParameters(CameraType::Orthographic, camera_frustum, static_cast<float>(parameters[0]),
94 static_cast<float>(parameters[1]), static_cast<float>(parameters[2]),
95 static_cast<float>(parameters[3]), static_cast<float>(parameters[4]), width,
96 height);
97};
98
99} /* namespace fitting */
100} /* namespace eos */
101
102#endif /* EOS_NONLINEAR_CAMERA_ESTIMATION_HPP */
Represents a set of estimated model parameters (rotation, translation) and camera parameters (viewing...
Definition: RenderingParameters.hpp:102
RenderingParameters estimate_orthographic_camera(std::vector< Eigen::Vector2f > image_points, std::vector< Eigen::Vector4f > model_points, int width, int height)
This algorithm estimates the rotation angles and translation of the model, as well as the viewing fru...
Definition: nonlinear_camera_estimation.hpp:63
Namespace containing all of eos's 3D model fitting functionality.
A class representing a camera viewing frustum. At the moment only fully tested with orthographic came...
Definition: RenderingParameters.hpp:49