Converter.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #include "Converter.h"
  19. namespace ORB_SLAM3
  20. {
  21. std::vector<cv::Mat> Converter::toDescriptorVector(const cv::Mat &Descriptors)
  22. {
  23. std::vector<cv::Mat> vDesc;
  24. vDesc.reserve(Descriptors.rows);
  25. for (int j=0;j<Descriptors.rows;j++)
  26. vDesc.push_back(Descriptors.row(j));
  27. return vDesc;
  28. }
  29. g2o::SE3Quat Converter::toSE3Quat(const cv::Mat &cvT)
  30. {
  31. Eigen::Matrix<double,3,3> R;
  32. R << cvT.at<float>(0,0), cvT.at<float>(0,1), cvT.at<float>(0,2),
  33. cvT.at<float>(1,0), cvT.at<float>(1,1), cvT.at<float>(1,2),
  34. cvT.at<float>(2,0), cvT.at<float>(2,1), cvT.at<float>(2,2);
  35. Eigen::Matrix<double,3,1> t(cvT.at<float>(0,3), cvT.at<float>(1,3), cvT.at<float>(2,3));
  36. return g2o::SE3Quat(R,t);
  37. }
  38. cv::Mat Converter::toCvMat(const g2o::SE3Quat &SE3)
  39. {
  40. Eigen::Matrix<double,4,4> eigMat = SE3.to_homogeneous_matrix();
  41. return toCvMat(eigMat);
  42. }
  43. cv::Mat Converter::toCvMat(const g2o::Sim3 &Sim3)
  44. {
  45. Eigen::Matrix3d eigR = Sim3.rotation().toRotationMatrix();
  46. Eigen::Vector3d eigt = Sim3.translation();
  47. double s = Sim3.scale();
  48. return toCvSE3(s*eigR,eigt);
  49. }
  50. cv::Mat Converter::toCvMat(const Eigen::Matrix<double,4,4> &m)
  51. {
  52. cv::Mat cvMat(4,4,CV_32F);
  53. for(int i=0;i<4;i++)
  54. for(int j=0; j<4; j++)
  55. cvMat.at<float>(i,j)=m(i,j);
  56. return cvMat.clone();
  57. }
  58. cv::Mat Converter::toCvMat(const Eigen::Matrix3d &m)
  59. {
  60. cv::Mat cvMat(3,3,CV_32F);
  61. for(int i=0;i<3;i++)
  62. for(int j=0; j<3; j++)
  63. cvMat.at<float>(i,j)=m(i,j);
  64. return cvMat.clone();
  65. }
  66. cv::Mat Converter::toCvMat(const Eigen::MatrixXd &m)
  67. {
  68. cv::Mat cvMat(m.rows(),m.cols(),CV_32F);
  69. for(int i=0;i<m.rows();i++)
  70. for(int j=0; j<m.cols(); j++)
  71. cvMat.at<float>(i,j)=m(i,j);
  72. return cvMat.clone();
  73. }
  74. cv::Mat Converter::toCvMat(const Eigen::Matrix<double,3,1> &m)
  75. {
  76. cv::Mat cvMat(3,1,CV_32F);
  77. for(int i=0;i<3;i++)
  78. cvMat.at<float>(i)=m(i);
  79. return cvMat.clone();
  80. }
  81. cv::Mat Converter::toCvSE3(const Eigen::Matrix<double,3,3> &R, const Eigen::Matrix<double,3,1> &t)
  82. {
  83. cv::Mat cvMat = cv::Mat::eye(4,4,CV_32F);
  84. for(int i=0;i<3;i++)
  85. {
  86. for(int j=0;j<3;j++)
  87. {
  88. cvMat.at<float>(i,j)=R(i,j);
  89. }
  90. }
  91. for(int i=0;i<3;i++)
  92. {
  93. cvMat.at<float>(i,3)=t(i);
  94. }
  95. return cvMat.clone();
  96. }
  97. Eigen::Matrix<double,3,1> Converter::toVector3d(const cv::Mat &cvVector)
  98. {
  99. Eigen::Matrix<double,3,1> v;
  100. v << cvVector.at<float>(0), cvVector.at<float>(1), cvVector.at<float>(2);
  101. return v;
  102. }
  103. Eigen::Matrix<double,3,1> Converter::toVector3d(const cv::Point3f &cvPoint)
  104. {
  105. Eigen::Matrix<double,3,1> v;
  106. v << cvPoint.x, cvPoint.y, cvPoint.z;
  107. return v;
  108. }
  109. Eigen::Matrix<double,3,3> Converter::toMatrix3d(const cv::Mat &cvMat3)
  110. {
  111. Eigen::Matrix<double,3,3> M;
  112. M << cvMat3.at<float>(0,0), cvMat3.at<float>(0,1), cvMat3.at<float>(0,2),
  113. cvMat3.at<float>(1,0), cvMat3.at<float>(1,1), cvMat3.at<float>(1,2),
  114. cvMat3.at<float>(2,0), cvMat3.at<float>(2,1), cvMat3.at<float>(2,2);
  115. return M;
  116. }
  117. Eigen::Matrix<double,4,4> Converter::toMatrix4d(const cv::Mat &cvMat4)
  118. {
  119. Eigen::Matrix<double,4,4> M;
  120. M << cvMat4.at<float>(0,0), cvMat4.at<float>(0,1), cvMat4.at<float>(0,2), cvMat4.at<float>(0,3),
  121. cvMat4.at<float>(1,0), cvMat4.at<float>(1,1), cvMat4.at<float>(1,2), cvMat4.at<float>(1,3),
  122. cvMat4.at<float>(2,0), cvMat4.at<float>(2,1), cvMat4.at<float>(2,2), cvMat4.at<float>(2,3),
  123. cvMat4.at<float>(3,0), cvMat4.at<float>(3,1), cvMat4.at<float>(3,2), cvMat4.at<float>(3,3);
  124. return M;
  125. }
  126. std::vector<float> Converter::toQuaternion(const cv::Mat &M)
  127. {
  128. Eigen::Matrix<double,3,3> eigMat = toMatrix3d(M);
  129. Eigen::Quaterniond q(eigMat);
  130. std::vector<float> v(4);
  131. v[0] = q.x();
  132. v[1] = q.y();
  133. v[2] = q.z();
  134. v[3] = q.w();
  135. return v;
  136. }
  137. cv::Mat Converter::tocvSkewMatrix(const cv::Mat &v)
  138. {
  139. return (cv::Mat_<float>(3,3) << 0, -v.at<float>(2), v.at<float>(1),
  140. v.at<float>(2), 0,-v.at<float>(0),
  141. -v.at<float>(1), v.at<float>(0), 0);
  142. }
  143. bool Converter::isRotationMatrix(const cv::Mat &R)
  144. {
  145. cv::Mat Rt;
  146. cv::transpose(R, Rt);
  147. cv::Mat shouldBeIdentity = Rt * R;
  148. cv::Mat I = cv::Mat::eye(3,3, shouldBeIdentity.type());
  149. return cv::norm(I, shouldBeIdentity) < 1e-6;
  150. }
  151. std::vector<float> Converter::toEuler(const cv::Mat &R)
  152. {
  153. assert(isRotationMatrix(R));
  154. float sy = sqrt(R.at<float>(0,0) * R.at<float>(0,0) + R.at<float>(1,0) * R.at<float>(1,0) );
  155. bool singular = sy < 1e-6; // If
  156. float x, y, z;
  157. if (!singular)
  158. {
  159. x = atan2(R.at<float>(2,1) , R.at<float>(2,2));
  160. y = atan2(-R.at<float>(2,0), sy);
  161. z = atan2(R.at<float>(1,0), R.at<float>(0,0));
  162. }
  163. else
  164. {
  165. x = atan2(-R.at<float>(1,2), R.at<float>(1,1));
  166. y = atan2(-R.at<float>(2,0), sy);
  167. z = 0;
  168. }
  169. std::vector<float> v_euler(3);
  170. v_euler[0] = x;
  171. v_euler[1] = y;
  172. v_euler[2] = z;
  173. return v_euler;
  174. }
  175. } //namespace ORB_SLAM