//M*//////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000, Intel Corporation, all rights reserved.13// Copyright (C) 2013, OpenCV Foundation, all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142/****************************************************************************************\43* Exhaustive Linearization for Robust Camera Pose and Focal Length Estimation.44* Contributed by Edgar Riba45\****************************************************************************************/4647#ifndef OPENCV_CALIB3D_UPNP_H_48#define OPENCV_CALIB3D_UPNP_H_4950#include "precomp.hpp"51#include "opencv2/core/core_c.h"52#include <iostream>5354class upnp55{56public:57upnp(const cv::Mat& cameraMatrix, const cv::Mat& opoints, const cv::Mat& ipoints);58~upnp();5960double compute_pose(cv::Mat& R, cv::Mat& t);61private:62upnp(const upnp &); // copy disabled63upnp& operator=(const upnp &); // assign disabled64template <typename T>65void init_camera_parameters(const cv::Mat& cameraMatrix)66{67uc = cameraMatrix.at<T> (0, 2);68vc = cameraMatrix.at<T> (1, 2);69fu = 1;70fv = 1;71}72template <typename OpointType, typename IpointType>73void init_points(const cv::Mat& opoints, const cv::Mat& ipoints)74{75for(int i = 0; i < number_of_correspondences; i++)76{77pws[3 * i ] = opoints.at<OpointType>(i).x;78pws[3 * i + 1] = opoints.at<OpointType>(i).y;79pws[3 * i + 2] = opoints.at<OpointType>(i).z;8081us[2 * i ] = ipoints.at<IpointType>(i).x;82us[2 * i + 1] = ipoints.at<IpointType>(i).y;83}84}8586double reprojection_error(const double R[3][3], const double t[3]);87void choose_control_points();88void compute_alphas();89void fill_M(cv::Mat * M, const int row, const double * alphas, const double u, const double v);90void compute_ccs(const double * betas, const double * ut);91void compute_pcs(void);9293void solve_for_sign(void);9495void find_betas_and_focal_approx_1(cv::Mat * Ut, cv::Mat * Rho, double * betas, double * efs);96void find_betas_and_focal_approx_2(cv::Mat * Ut, cv::Mat * Rho, double * betas, double * efs);97void qr_solve(cv::Mat * A, cv::Mat * b, cv::Mat * X);9899cv::Mat compute_constraint_distance_2param_6eq_2unk_f_unk(const cv::Mat& M1);100cv::Mat compute_constraint_distance_3param_6eq_6unk_f_unk(const cv::Mat& M1, const cv::Mat& M2);101void generate_all_possible_solutions_for_f_unk(const double betas[5], double solutions[18][3]);102103double sign(const double v);104double dot(const double * v1, const double * v2);105double dotXY(const double * v1, const double * v2);106double dotZ(const double * v1, const double * v2);107double dist2(const double * p1, const double * p2);108109void compute_rho(double * rho);110void compute_L_6x12(const double * ut, double * l_6x12);111112void gauss_newton(const cv::Mat * L_6x12, const cv::Mat * Rho, double current_betas[4], double * efs);113void compute_A_and_b_gauss_newton(const double * l_6x12, const double * rho,114const double cb[4], cv::Mat * A, cv::Mat * b, double const f);115116double compute_R_and_t(const double * ut, const double * betas,117double R[3][3], double t[3]);118119void estimate_R_and_t(double R[3][3], double t[3]);120121void copy_R_and_t(const double R_dst[3][3], const double t_dst[3],122double R_src[3][3], double t_src[3]);123124125double uc, vc, fu, fv;126127std::vector<double> pws, us, alphas, pcs;128int number_of_correspondences;129130double cws[4][3], ccs[4][3];131int max_nr;132double * A1, * A2;133};134135#endif // OPENCV_CALIB3D_UPNP_H_136137138