MLPnPsolver.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. * Author: Steffen Urban *
  20. * Contact: urbste@gmail.com *
  21. * License: Copyright (c) 2016 Steffen Urban, ANU. All rights reserved. *
  22. * *
  23. * Redistribution and use in source and binary forms, with or without *
  24. * modification, are permitted provided that the following conditions *
  25. * are met: *
  26. * * Redistributions of source code must retain the above copyright *
  27. * notice, this list of conditions and the following disclaimer. *
  28. * * Redistributions in binary form must reproduce the above copyright *
  29. * notice, this list of conditions and the following disclaimer in the *
  30. * documentation and/or other materials provided with the distribution. *
  31. * * Neither the name of ANU nor the names of its contributors may be *
  32. * used to endorse or promote products derived from this software without *
  33. * specific prior written permission. *
  34. * *
  35. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"*
  36. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE *
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE *
  38. * ARE DISCLAIMED. IN NO EVENT SHALL ANU OR THE CONTRIBUTORS BE LIABLE *
  39. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL *
  40. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR *
  41. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER *
  42. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *
  43. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY *
  44. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF *
  45. * SUCH DAMAGE. *
  46. ******************************************************************************/
  47. #ifndef ORB_SLAM3_MLPNPSOLVER_H
  48. #define ORB_SLAM3_MLPNPSOLVER_H
  49. #include "MapPoint.h"
  50. #include "Frame.h"
  51. #include<Eigen/Dense>
  52. #include<Eigen/Sparse>
  53. namespace ORB_SLAM3{
  54. class MLPnPsolver {
  55. public:
  56. MLPnPsolver(const Frame &F, const vector<MapPoint*> &vpMapPointMatches);
  57. ~MLPnPsolver();
  58. void SetRansacParameters(double probability = 0.99, int minInliers = 8, int maxIterations = 300, int minSet = 6, float epsilon = 0.4,
  59. float th2 = 5.991);
  60. cv::Mat iterate(int nIterations, bool &bNoMore, vector<bool> &vbInliers, int &nInliers);
  61. //Type definitions needed by the original code
  62. /** A 3-vector of unit length used to describe landmark observations/bearings
  63. * in camera frames (always expressed in camera frames)
  64. */
  65. typedef Eigen::Vector3d bearingVector_t;
  66. /** An array of bearing-vectors */
  67. typedef std::vector<bearingVector_t, Eigen::aligned_allocator<bearingVector_t> >
  68. bearingVectors_t;
  69. /** A 2-matrix containing the 2D covariance information of a bearing vector
  70. */
  71. typedef Eigen::Matrix2d cov2_mat_t;
  72. /** A 3-matrix containing the 3D covariance information of a bearing vector */
  73. typedef Eigen::Matrix3d cov3_mat_t;
  74. /** An array of 3D covariance matrices */
  75. typedef std::vector<cov3_mat_t, Eigen::aligned_allocator<cov3_mat_t> >
  76. cov3_mats_t;
  77. /** A 3-vector describing a point in 3D-space */
  78. typedef Eigen::Vector3d point_t;
  79. /** An array of 3D-points */
  80. typedef std::vector<point_t, Eigen::aligned_allocator<point_t> >
  81. points_t;
  82. /** A homogeneous 3-vector describing a point in 3D-space */
  83. typedef Eigen::Vector4d point4_t;
  84. /** An array of homogeneous 3D-points */
  85. typedef std::vector<point4_t, Eigen::aligned_allocator<point4_t> >
  86. points4_t;
  87. /** A 3-vector containing the rodrigues parameters of a rotation matrix */
  88. typedef Eigen::Vector3d rodrigues_t;
  89. /** A rotation matrix */
  90. typedef Eigen::Matrix3d rotation_t;
  91. /** A 3x4 transformation matrix containing rotation \f$ \mathbf{R} \f$ and
  92. * translation \f$ \mathbf{t} \f$ as follows:
  93. * \f$ \left( \begin{array}{cc} \mathbf{R} & \mathbf{t} \end{array} \right) \f$
  94. */
  95. typedef Eigen::Matrix<double,3,4> transformation_t;
  96. /** A 3-vector describing a translation/camera position */
  97. typedef Eigen::Vector3d translation_t;
  98. private:
  99. void CheckInliers();
  100. bool Refine();
  101. //Functions from de original MLPnP code
  102. /*
  103. * Computes the camera pose given 3D points coordinates (in the camera reference
  104. * system), the camera rays and (optionally) the covariance matrix of those camera rays.
  105. * Result is stored in solution
  106. */
  107. void computePose(
  108. const bearingVectors_t & f,
  109. const points_t & p,
  110. const cov3_mats_t & covMats,
  111. const std::vector<int>& indices,
  112. transformation_t & result);
  113. void mlpnp_gn(Eigen::VectorXd& x,
  114. const points_t& pts,
  115. const std::vector<Eigen::MatrixXd>& nullspaces,
  116. const Eigen::SparseMatrix<double> Kll,
  117. bool use_cov);
  118. void mlpnp_residuals_and_jacs(
  119. const Eigen::VectorXd& x,
  120. const points_t& pts,
  121. const std::vector<Eigen::MatrixXd>& nullspaces,
  122. Eigen::VectorXd& r,
  123. Eigen::MatrixXd& fjac,
  124. bool getJacs);
  125. void mlpnpJacs(
  126. const point_t& pt,
  127. const Eigen::Vector3d& nullspace_r,
  128. const Eigen::Vector3d& nullspace_s,
  129. const rodrigues_t& w,
  130. const translation_t& t,
  131. Eigen::MatrixXd& jacs);
  132. //Auxiliar methods
  133. /**
  134. * \brief Compute a rotation matrix from Rodrigues axis angle.
  135. *
  136. * \param[in] omega The Rodrigues-parameters of a rotation.
  137. * \return The 3x3 rotation matrix.
  138. */
  139. Eigen::Matrix3d rodrigues2rot(const Eigen::Vector3d & omega);
  140. /**
  141. * \brief Compute the Rodrigues-parameters of a rotation matrix.
  142. *
  143. * \param[in] R The 3x3 rotation matrix.
  144. * \return The Rodrigues-parameters.
  145. */
  146. Eigen::Vector3d rot2rodrigues(const Eigen::Matrix3d & R);
  147. //----------------------------------------------------
  148. //Fields of the solver
  149. //----------------------------------------------------
  150. vector<MapPoint*> mvpMapPointMatches;
  151. // 2D Points
  152. vector<cv::Point2f> mvP2D;
  153. //Substitued by bearing vectors
  154. bearingVectors_t mvBearingVecs;
  155. vector<float> mvSigma2;
  156. // 3D Points
  157. //vector<cv::Point3f> mvP3Dw;
  158. points_t mvP3Dw;
  159. // Index in Frame
  160. vector<size_t> mvKeyPointIndices;
  161. // Current Estimation
  162. double mRi[3][3];
  163. double mti[3];
  164. cv::Mat mTcwi;
  165. vector<bool> mvbInliersi;
  166. int mnInliersi;
  167. // Current Ransac State
  168. int mnIterations;
  169. vector<bool> mvbBestInliers;
  170. int mnBestInliers;
  171. cv::Mat mBestTcw;
  172. // Refined
  173. cv::Mat mRefinedTcw;
  174. vector<bool> mvbRefinedInliers;
  175. int mnRefinedInliers;
  176. // Number of Correspondences
  177. int N;
  178. // Indices for random selection [0 .. N-1]
  179. vector<size_t> mvAllIndices;
  180. // RANSAC probability
  181. double mRansacProb;
  182. // RANSAC min inliers
  183. int mRansacMinInliers;
  184. // RANSAC max iterations
  185. int mRansacMaxIts;
  186. // RANSAC expected inliers/total ratio
  187. float mRansacEpsilon;
  188. // RANSAC Threshold inlier/outlier. Max error e = dist(P1,T_12*P2)^2
  189. float mRansacTh;
  190. // RANSAC Minimun Set used at each iteration
  191. int mRansacMinSet;
  192. // Max square error associated with scale level. Max error = th*th*sigma(level)*sigma(level)
  193. vector<float> mvMaxError;
  194. GeometricCamera* mpCamera;
  195. };
  196. }
  197. #endif //ORB_SLAM3_MLPNPSOLVER_H