| 1 | //M*////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // |
| 3 | // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. |
| 4 | // |
| 5 | // By downloading, copying, installing or using the software you agree to this license. |
| 6 | // If you do not agree to this license, do not download, install, |
| 7 | // copy or use the software. |
| 8 | // |
| 9 | // |
| 10 | // License Agreement |
| 11 | // For Open Source Computer Vision Library |
| 12 | // |
| 13 | // Copyright (C) 2000, Intel Corporation, all rights reserved. |
| 14 | // Copyright (C) 2013, OpenCV Foundation, all rights reserved. |
| 15 | // Third party copyrights are property of their respective owners. |
| 16 | // |
| 17 | // Redistribution and use in source and binary forms, with or without modification, |
| 18 | // are permitted provided that the following conditions are met: |
| 19 | // |
| 20 | // * Redistribution's of source code must retain the above copyright notice, |
| 21 | // this list of conditions and the following disclaimer. |
| 22 | // |
| 23 | // * Redistribution's in binary form must reproduce the above copyright notice, |
| 24 | // this list of conditions and the following disclaimer in the documentation |
| 25 | // and/or other materials provided with the distribution. |
| 26 | // |
| 27 | // * The name of the copyright holders may not be used to endorse or promote products |
| 28 | // derived from this software without specific prior written permission. |
| 29 | // |
| 30 | // This software is provided by the copyright holders and contributors "as is" and |
| 31 | // any express or implied warranties, including, but not limited to, the implied |
| 32 | // warranties of merchantability and fitness for a particular purpose are disclaimed. |
| 33 | // In no event shall the Intel Corporation or contributors be liable for any direct, |
| 34 | // indirect, incidental, special, exemplary, or consequential damages |
| 35 | // (including, but not limited to, procurement of substitute goods or services; |
| 36 | // loss of use, data, or profits; or business interruption) however caused |
| 37 | // and on any theory of liability, whether in contract, strict liability, |
| 38 | // or tort (including negligence or otherwise) arising in any way out of |
| 39 | // the use of this software, even if advised of the possibility of such damage. |
| 40 | // |
| 41 | //M*/ |
| 42 | |
| 43 | /****************************************************************************************\ |
| 44 | * Exhaustive Linearization for Robust Camera Pose and Focal Length Estimation. |
| 45 | * Contributed by Edgar Riba |
| 46 | \****************************************************************************************/ |
| 47 | |
| 48 | #ifndef OPENCV_CALIB3D_UPNP_H_ |
| 49 | #define OPENCV_CALIB3D_UPNP_H_ |
| 50 | |
| 51 | #include "precomp.hpp" |
| 52 | #include <iostream> |
| 53 | |
| 54 | #if 0 // fix buffer overflow first (FIXIT mark in .cpp file) |
| 55 | |
| 56 | class upnp |
| 57 | { |
| 58 | public: |
| 59 | upnp(const cv::Mat& cameraMatrix, const cv::Mat& opoints, const cv::Mat& ipoints); |
| 60 | ~upnp(); |
| 61 | |
| 62 | double compute_pose(cv::Mat& R, cv::Mat& t); |
| 63 | private: |
| 64 | upnp(const upnp &); // copy disabled |
| 65 | upnp& operator=(const upnp &); // assign disabled |
| 66 | template <typename T> |
| 67 | void init_camera_parameters(const cv::Mat& cameraMatrix) |
| 68 | { |
| 69 | uc = cameraMatrix.at<T> (0, 2); |
| 70 | vc = cameraMatrix.at<T> (1, 2); |
| 71 | fu = 1; |
| 72 | fv = 1; |
| 73 | } |
| 74 | template <typename OpointType, typename IpointType> |
| 75 | void init_points(const cv::Mat& opoints, const cv::Mat& ipoints) |
| 76 | { |
| 77 | for(int i = 0; i < number_of_correspondences; i++) |
| 78 | { |
| 79 | pws[3 * i ] = opoints.at<OpointType>(i).x; |
| 80 | pws[3 * i + 1] = opoints.at<OpointType>(i).y; |
| 81 | pws[3 * i + 2] = opoints.at<OpointType>(i).z; |
| 82 | |
| 83 | us[2 * i ] = ipoints.at<IpointType>(i).x; |
| 84 | us[2 * i + 1] = ipoints.at<IpointType>(i).y; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | double reprojection_error(const double R[3][3], const double t[3]); |
| 89 | void choose_control_points(); |
| 90 | void compute_alphas(); |
| 91 | void fill_M(cv::Mat * M, const int row, const double * alphas, const double u, const double v); |
| 92 | void compute_ccs(const double * betas, const double * ut); |
| 93 | void compute_pcs(void); |
| 94 | |
| 95 | void solve_for_sign(void); |
| 96 | |
| 97 | void find_betas_and_focal_approx_1(cv::Mat * Ut, cv::Mat * Rho, double * betas, double * efs); |
| 98 | void find_betas_and_focal_approx_2(cv::Mat * Ut, cv::Mat * Rho, double * betas, double * efs); |
| 99 | void qr_solve(cv::Mat * A, cv::Mat * b, cv::Mat * X); |
| 100 | |
| 101 | cv::Mat compute_constraint_distance_2param_6eq_2unk_f_unk(const cv::Mat& M1); |
| 102 | cv::Mat compute_constraint_distance_3param_6eq_6unk_f_unk(const cv::Mat& M1, const cv::Mat& M2); |
| 103 | void generate_all_possible_solutions_for_f_unk(const double betas[5], double solutions[18][3]); |
| 104 | |
| 105 | double sign(const double v); |
| 106 | double dot(const double * v1, const double * v2); |
| 107 | double dotXY(const double * v1, const double * v2); |
| 108 | double dotZ(const double * v1, const double * v2); |
| 109 | double dist2(const double * p1, const double * p2); |
| 110 | |
| 111 | void compute_rho(double * rho); |
| 112 | void compute_L_6x12(const double * ut, double * l_6x12); |
| 113 | |
| 114 | void gauss_newton(const cv::Mat * L_6x12, const cv::Mat * Rho, double current_betas[4], double * efs); |
| 115 | void compute_A_and_b_gauss_newton(const double * l_6x12, const double * rho, |
| 116 | const double cb[4], cv::Mat * A, cv::Mat * b, double const f); |
| 117 | |
| 118 | double compute_R_and_t(const double * ut, const double * betas, |
| 119 | double R[3][3], double t[3]); |
| 120 | |
| 121 | void estimate_R_and_t(double R[3][3], double t[3]); |
| 122 | |
| 123 | void copy_R_and_t(const double R_dst[3][3], const double t_dst[3], |
| 124 | double R_src[3][3], double t_src[3]); |
| 125 | |
| 126 | |
| 127 | double uc, vc, fu, fv; |
| 128 | |
| 129 | std::vector<double> pws, us, alphas, pcs; |
| 130 | int number_of_correspondences; |
| 131 | |
| 132 | double cws[4][3], ccs[4][3]; |
| 133 | int max_nr; |
| 134 | double * A1, * A2; |
| 135 | }; |
| 136 | |
| 137 | #endif |
| 138 | |
| 139 | #endif // OPENCV_CALIB3D_UPNP_H_ |
| 140 | |