Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/calib3d/src/upnp.h
16354 views
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 "opencv2/core/core_c.h"
53
#include <iostream>
54
55
class upnp
56
{
57
public:
58
upnp(const cv::Mat& cameraMatrix, const cv::Mat& opoints, const cv::Mat& ipoints);
59
~upnp();
60
61
double compute_pose(cv::Mat& R, cv::Mat& t);
62
private:
63
upnp(const upnp &); // copy disabled
64
upnp& operator=(const upnp &); // assign disabled
65
template <typename T>
66
void init_camera_parameters(const cv::Mat& cameraMatrix)
67
{
68
uc = cameraMatrix.at<T> (0, 2);
69
vc = cameraMatrix.at<T> (1, 2);
70
fu = 1;
71
fv = 1;
72
}
73
template <typename OpointType, typename IpointType>
74
void init_points(const cv::Mat& opoints, const cv::Mat& ipoints)
75
{
76
for(int i = 0; i < number_of_correspondences; i++)
77
{
78
pws[3 * i ] = opoints.at<OpointType>(i).x;
79
pws[3 * i + 1] = opoints.at<OpointType>(i).y;
80
pws[3 * i + 2] = opoints.at<OpointType>(i).z;
81
82
us[2 * i ] = ipoints.at<IpointType>(i).x;
83
us[2 * i + 1] = ipoints.at<IpointType>(i).y;
84
}
85
}
86
87
double reprojection_error(const double R[3][3], const double t[3]);
88
void choose_control_points();
89
void compute_alphas();
90
void fill_M(cv::Mat * M, const int row, const double * alphas, const double u, const double v);
91
void compute_ccs(const double * betas, const double * ut);
92
void compute_pcs(void);
93
94
void solve_for_sign(void);
95
96
void find_betas_and_focal_approx_1(cv::Mat * Ut, cv::Mat * Rho, double * betas, double * efs);
97
void find_betas_and_focal_approx_2(cv::Mat * Ut, cv::Mat * Rho, double * betas, double * efs);
98
void qr_solve(cv::Mat * A, cv::Mat * b, cv::Mat * X);
99
100
cv::Mat compute_constraint_distance_2param_6eq_2unk_f_unk(const cv::Mat& M1);
101
cv::Mat compute_constraint_distance_3param_6eq_6unk_f_unk(const cv::Mat& M1, const cv::Mat& M2);
102
void generate_all_possible_solutions_for_f_unk(const double betas[5], double solutions[18][3]);
103
104
double sign(const double v);
105
double dot(const double * v1, const double * v2);
106
double dotXY(const double * v1, const double * v2);
107
double dotZ(const double * v1, const double * v2);
108
double dist2(const double * p1, const double * p2);
109
110
void compute_rho(double * rho);
111
void compute_L_6x12(const double * ut, double * l_6x12);
112
113
void gauss_newton(const cv::Mat * L_6x12, const cv::Mat * Rho, double current_betas[4], double * efs);
114
void compute_A_and_b_gauss_newton(const double * l_6x12, const double * rho,
115
const double cb[4], cv::Mat * A, cv::Mat * b, double const f);
116
117
double compute_R_and_t(const double * ut, const double * betas,
118
double R[3][3], double t[3]);
119
120
void estimate_R_and_t(double R[3][3], double t[3]);
121
122
void copy_R_and_t(const double R_dst[3][3], const double t_dst[3],
123
double R_src[3][3], double t_src[3]);
124
125
126
double uc, vc, fu, fv;
127
128
std::vector<double> pws, us, alphas, pcs;
129
int number_of_correspondences;
130
131
double cws[4][3], ccs[4][3];
132
int max_nr;
133
double * A1, * A2;
134
};
135
136
#endif // OPENCV_CALIB3D_UPNP_H_
137
138