PnPsolver.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * This file is part of ORB-SLAM3
  3. *
  4. * Copyright (C) 2017-2020 Carlos Campos, Richard Elvira, Juan J. Gómez Rodríguez, José M.M. Montiel and Juan D. Tardós, University of Zaragoza.
  5. * Copyright (C) 2014-2016 Raúl Mur-Artal, José M.M. Montiel and Juan D. Tardós, University of Zaragoza.
  6. *
  7. * ORB-SLAM3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * ORB-SLAM3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
  12. * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with ORB-SLAM3.
  16. * If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * Copyright (c) 2009, V. Lepetit, EPFL
  20. * All rights reserved.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions are met:
  24. *
  25. * 1. Redistributions of source code must retain the above copyright notice, this
  26. * list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright notice,
  28. * this list of conditions and the following disclaimer in the documentation
  29. * and/or other materials provided with the distribution.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  32. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  33. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  34. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  35. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  36. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  38. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  39. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  40. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  41. *
  42. * The views and conclusions contained in the software and documentation are those
  43. * of the authors and should not be interpreted as representing official policies,
  44. * either expressed or implied, of the FreeBSD Project
  45. */
  46. #ifndef PNPSOLVER_H
  47. #define PNPSOLVER_H
  48. #include <opencv2/core/core.hpp>
  49. #include "MapPoint.h"
  50. #include "Frame.h"
  51. namespace ORB_SLAM3
  52. {
  53. class PnPsolver {
  54. public:
  55. PnPsolver(const Frame &F, const vector<MapPoint*> &vpMapPointMatches);
  56. ~PnPsolver();
  57. void SetRansacParameters(double probability = 0.99, int minInliers = 8 , int maxIterations = 300, int minSet = 4, float epsilon = 0.4,
  58. float th2 = 5.991);
  59. cv::Mat find(vector<bool> &vbInliers, int &nInliers);
  60. cv::Mat iterate(int nIterations, bool &bNoMore, vector<bool> &vbInliers, int &nInliers);
  61. private:
  62. void CheckInliers();
  63. bool Refine();
  64. // Functions from the original EPnP code
  65. void set_maximum_number_of_correspondences(const int n);
  66. void reset_correspondences(void);
  67. void add_correspondence(const double X, const double Y, const double Z,
  68. const double u, const double v);
  69. double compute_pose(double R[3][3], double T[3]);
  70. void relative_error(double & rot_err, double & transl_err,
  71. const double Rtrue[3][3], const double ttrue[3],
  72. const double Rest[3][3], const double test[3]);
  73. void print_pose(const double R[3][3], const double t[3]);
  74. double reprojection_error(const double R[3][3], const double t[3]);
  75. void choose_control_points(void);
  76. void compute_barycentric_coordinates(void);
  77. void fill_M(CvMat * M, const int row, const double * alphas, const double u, const double v);
  78. void compute_ccs(const double * betas, const double * ut);
  79. void compute_pcs(void);
  80. void solve_for_sign(void);
  81. void find_betas_approx_1(const CvMat * L_6x10, const CvMat * Rho, double * betas);
  82. void find_betas_approx_2(const CvMat * L_6x10, const CvMat * Rho, double * betas);
  83. void find_betas_approx_3(const CvMat * L_6x10, const CvMat * Rho, double * betas);
  84. void qr_solve(CvMat * A, CvMat * b, CvMat * X);
  85. double dot(const double * v1, const double * v2);
  86. double dist2(const double * p1, const double * p2);
  87. void compute_rho(double * rho);
  88. void compute_L_6x10(const double * ut, double * l_6x10);
  89. void gauss_newton(const CvMat * L_6x10, const CvMat * Rho, double current_betas[4]);
  90. void compute_A_and_b_gauss_newton(const double * l_6x10, const double * rho,
  91. double cb[4], CvMat * A, CvMat * b);
  92. double compute_R_and_t(const double * ut, const double * betas,
  93. double R[3][3], double t[3]);
  94. void estimate_R_and_t(double R[3][3], double t[3]);
  95. void copy_R_and_t(const double R_dst[3][3], const double t_dst[3],
  96. double R_src[3][3], double t_src[3]);
  97. void mat_to_quat(const double R[3][3], double q[4]);
  98. double uc, vc, fu, fv;
  99. double * pws, * us, * alphas, * pcs;
  100. int maximum_number_of_correspondences;
  101. int number_of_correspondences;
  102. double cws[4][3], ccs[4][3];
  103. double cws_determinant;
  104. vector<MapPoint*> mvpMapPointMatches;
  105. // 2D Points
  106. vector<cv::Point2f> mvP2D;
  107. vector<float> mvSigma2;
  108. // 3D Points
  109. vector<cv::Point3f> mvP3Dw;
  110. // Index in Frame
  111. vector<size_t> mvKeyPointIndices;
  112. // Current Estimation
  113. double mRi[3][3];
  114. double mti[3];
  115. cv::Mat mTcwi;
  116. vector<bool> mvbInliersi;
  117. int mnInliersi;
  118. // Current Ransac State
  119. int mnIterations;
  120. vector<bool> mvbBestInliers;
  121. int mnBestInliers;
  122. cv::Mat mBestTcw;
  123. // Refined
  124. cv::Mat mRefinedTcw;
  125. vector<bool> mvbRefinedInliers;
  126. int mnRefinedInliers;
  127. // Number of Correspondences
  128. int N;
  129. // Indices for random selection [0 .. N-1]
  130. vector<size_t> mvAllIndices;
  131. // RANSAC probability
  132. double mRansacProb;
  133. // RANSAC min inliers
  134. int mRansacMinInliers;
  135. // RANSAC max iterations
  136. int mRansacMaxIts;
  137. // RANSAC expected inliers/total ratio
  138. float mRansacEpsilon;
  139. // RANSAC Threshold inlier/outlier. Max error e = dist(P1,T_12*P2)^2
  140. float mRansacTh;
  141. // RANSAC Minimun Set used at each iteration
  142. int mRansacMinSet;
  143. // Max square error associated with scale level. Max error = th*th*sigma(level)*sigma(level)
  144. vector<float> mvMaxError;
  145. };
  146. } //namespace ORB_SLAM
  147. #endif //PNPSOLVER_H