Frame.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /**
  2. * This file is part of ORB-SLAM3
  3. *
  4. * Copyright (C) 2017-2021 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. #ifndef FRAME_H
  19. #define FRAME_H
  20. #include<vector>
  21. #include "Thirdparty/DBoW2/DBoW2/BowVector.h"
  22. #include "Thirdparty/DBoW2/DBoW2/FeatureVector.h"
  23. #include "Thirdparty/Sophus/sophus/geometry.hpp"
  24. #include "ImuTypes.h"
  25. #include "ORBVocabulary.h"
  26. #include "Converter.h"
  27. #include "Settings.h"
  28. #include <mutex>
  29. #include <opencv2/opencv.hpp>
  30. #include "Eigen/Core"
  31. #include "sophus/se3.hpp"
  32. namespace ORB_SLAM3
  33. {
  34. #define FRAME_GRID_ROWS 48
  35. #define FRAME_GRID_COLS 64
  36. class MapPoint;
  37. class KeyFrame;
  38. class ConstraintPoseImu;
  39. class GeometricCamera;
  40. class ORBextractor;
  41. class Frame
  42. {
  43. public:
  44. Frame();
  45. // Copy constructor.
  46. Frame(const Frame &frame);
  47. // Constructor for stereo cameras.
  48. Frame(const cv::Mat &imLeft, const cv::Mat &imRight, const double &timeStamp, ORBextractor* extractorLeft, ORBextractor* extractorRight, ORBVocabulary* voc, cv::Mat &K, cv::Mat &distCoef, const float &bf, const float &thDepth, GeometricCamera* pCamera,Frame* pPrevF = static_cast<Frame*>(NULL), const IMU::Calib &ImuCalib = IMU::Calib());
  49. // Constructor for RGB-D cameras.
  50. Frame(const cv::Mat &imGray, const cv::Mat &imDepth, const double &timeStamp, ORBextractor* extractor,ORBVocabulary* voc, cv::Mat &K, cv::Mat &distCoef, const float &bf, const float &thDepth, GeometricCamera* pCamera,Frame* pPrevF = static_cast<Frame*>(NULL), const IMU::Calib &ImuCalib = IMU::Calib());
  51. // Constructor for Monocular cameras.
  52. Frame(const cv::Mat &imGray, const double &timeStamp, ORBextractor* extractor,ORBVocabulary* voc, GeometricCamera* pCamera, cv::Mat &distCoef, const float &bf, const float &thDepth, Frame* pPrevF = static_cast<Frame*>(NULL), const IMU::Calib &ImuCalib = IMU::Calib());
  53. // Destructor
  54. // ~Frame();
  55. // Extract ORB on the image. 0 for left image and 1 for right image.
  56. void ExtractORB(int flag, const cv::Mat &im, const int x0, const int x1);
  57. // Compute Bag of Words representation.
  58. void ComputeBoW();
  59. // Set the camera pose. (Imu pose is not modified!)
  60. void SetPose(const Sophus::SE3<float> &Tcw);
  61. // Set IMU velocity
  62. void SetVelocity(Eigen::Vector3f Vw);
  63. Eigen::Vector3f GetVelocity() const;
  64. // Set IMU pose and velocity (implicitly changes camera pose)
  65. void SetImuPoseVelocity(const Eigen::Matrix3f &Rwb, const Eigen::Vector3f &twb, const Eigen::Vector3f &Vwb);
  66. Eigen::Matrix<float,3,1> GetImuPosition() const;
  67. Eigen::Matrix<float,3,3> GetImuRotation();
  68. Sophus::SE3<float> GetImuPose();
  69. Sophus::SE3f GetRelativePoseTrl();
  70. Sophus::SE3f GetRelativePoseTlr();
  71. Eigen::Matrix3f GetRelativePoseTlr_rotation();
  72. Eigen::Vector3f GetRelativePoseTlr_translation();
  73. void SetNewBias(const IMU::Bias &b);
  74. // Check if a MapPoint is in the frustum of the camera
  75. // and fill variables of the MapPoint to be used by the tracking
  76. bool isInFrustum(MapPoint* pMP, float viewingCosLimit);
  77. bool ProjectPointDistort(MapPoint* pMP, cv::Point2f &kp, float &u, float &v);
  78. Eigen::Vector3f inRefCoordinates(Eigen::Vector3f pCw);
  79. // Compute the cell of a keypoint (return false if outside the grid)
  80. bool PosInGrid(const cv::KeyPoint &kp, int &posX, int &posY);
  81. vector<size_t> GetFeaturesInArea(const float &x, const float &y, const float &r, const int minLevel=-1, const int maxLevel=-1, const bool bRight = false) const;
  82. // Search a match for each keypoint in the left image to a keypoint in the right image.
  83. // If there is a match, depth is computed and the right coordinate associated to the left keypoint is stored.
  84. void ComputeStereoMatches();
  85. // Associate a "right" coordinate to a keypoint if there is valid depth in the depthmap.
  86. void ComputeStereoFromRGBD(const cv::Mat &imDepth);
  87. // Backprojects a keypoint (if stereo/depth info available) into 3D world coordinates.
  88. bool UnprojectStereo(const int &i, Eigen::Vector3f &x3D);
  89. ConstraintPoseImu* mpcpi;
  90. bool imuIsPreintegrated();
  91. void setIntegrated();
  92. bool isSet() const;
  93. // Computes rotation, translation and camera center matrices from the camera pose.
  94. void UpdatePoseMatrices();
  95. // Returns the camera center.
  96. inline Eigen::Vector3f GetCameraCenter(){
  97. return mOw;
  98. }
  99. // Returns inverse of rotation
  100. inline Eigen::Matrix3f GetRotationInverse(){
  101. return mRwc;
  102. }
  103. inline Sophus::SE3<float> GetPose() const {
  104. //TODO: can the Frame pose be accsessed from several threads? should this be protected somehow?
  105. return mTcw;
  106. }
  107. inline Eigen::Matrix3f GetRwc() const {
  108. return mRwc;
  109. }
  110. inline Eigen::Vector3f GetOw() const {
  111. return mOw;
  112. }
  113. inline bool HasPose() const {
  114. return mbHasPose;
  115. }
  116. inline bool HasVelocity() const {
  117. return mbHasVelocity;
  118. }
  119. private:
  120. //Sophus/Eigen migration
  121. Sophus::SE3<float> mTcw;
  122. Eigen::Matrix<float,3,3> mRwc;
  123. Eigen::Matrix<float,3,1> mOw;
  124. Eigen::Matrix<float,3,3> mRcw;
  125. Eigen::Matrix<float,3,1> mtcw;
  126. bool mbHasPose;
  127. //Rcw_ not necessary as Sophus has a method for extracting the rotation matrix: Tcw_.rotationMatrix()
  128. //tcw_ not necessary as Sophus has a method for extracting the translation vector: Tcw_.translation()
  129. //Twc_ not necessary as Sophus has a method for easily computing the inverse pose: Tcw_.inverse()
  130. Sophus::SE3<float> mTlr, mTrl;
  131. Eigen::Matrix<float,3,3> mRlr;
  132. Eigen::Vector3f mtlr;
  133. // IMU linear velocity
  134. Eigen::Vector3f mVw;
  135. bool mbHasVelocity;
  136. public:
  137. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  138. // Vocabulary used for relocalization.
  139. ORBVocabulary* mpORBvocabulary;
  140. // Feature extractor. The right is used only in the stereo case.
  141. ORBextractor* mpORBextractorLeft, *mpORBextractorRight;
  142. // Frame timestamp.
  143. double mTimeStamp;
  144. // Calibration matrix and OpenCV distortion parameters.
  145. cv::Mat mK;
  146. Eigen::Matrix3f mK_;
  147. static float fx;
  148. static float fy;
  149. static float cx;
  150. static float cy;
  151. static float invfx;
  152. static float invfy;
  153. cv::Mat mDistCoef;
  154. // Stereo baseline multiplied by fx.
  155. float mbf;
  156. // Stereo baseline in meters.
  157. float mb;
  158. // Threshold close/far points. Close points are inserted from 1 view.
  159. // Far points are inserted as in the monocular case from 2 views.
  160. float mThDepth;
  161. // Number of KeyPoints.
  162. int N;
  163. // Vector of keypoints (original for visualization) and undistorted (actually used by the system).
  164. // In the stereo case, mvKeysUn is redundant as images must be rectified.
  165. // In the RGB-D case, RGB images can be distorted.
  166. std::vector<cv::KeyPoint> mvKeys, mvKeysRight;
  167. std::vector<cv::KeyPoint> mvKeysUn;
  168. // Corresponding stereo coordinate and depth for each keypoint.
  169. std::vector<MapPoint*> mvpMapPoints;
  170. // "Monocular" keypoints have a negative value.
  171. std::vector<float> mvuRight;
  172. std::vector<float> mvDepth;
  173. // Bag of Words Vector structures.
  174. DBoW2::BowVector mBowVec;
  175. DBoW2::FeatureVector mFeatVec;
  176. // ORB descriptor, each row associated to a keypoint.
  177. cv::Mat mDescriptors, mDescriptorsRight;
  178. // MapPoints associated to keypoints, NULL pointer if no association.
  179. // Flag to identify outlier associations.
  180. std::vector<bool> mvbOutlier;
  181. int mnCloseMPs;
  182. // Keypoints are assigned to cells in a grid to reduce matching complexity when projecting MapPoints.
  183. static float mfGridElementWidthInv;
  184. static float mfGridElementHeightInv;
  185. std::vector<std::size_t> mGrid[FRAME_GRID_COLS][FRAME_GRID_ROWS];
  186. IMU::Bias mPredBias;
  187. // IMU bias
  188. IMU::Bias mImuBias;
  189. // Imu calibration
  190. IMU::Calib mImuCalib;
  191. // Imu preintegration from last keyframe
  192. IMU::Preintegrated* mpImuPreintegrated;
  193. KeyFrame* mpLastKeyFrame;
  194. // Pointer to previous frame
  195. Frame* mpPrevFrame;
  196. IMU::Preintegrated* mpImuPreintegratedFrame;
  197. // Current and Next Frame id.
  198. static long unsigned int nNextId;
  199. long unsigned int mnId;
  200. // Reference Keyframe.
  201. KeyFrame* mpReferenceKF;
  202. // Scale pyramid info.
  203. int mnScaleLevels;
  204. float mfScaleFactor;
  205. float mfLogScaleFactor;
  206. vector<float> mvScaleFactors;
  207. vector<float> mvInvScaleFactors;
  208. vector<float> mvLevelSigma2;
  209. vector<float> mvInvLevelSigma2;
  210. // Undistorted Image Bounds (computed once).
  211. static float mnMinX;
  212. static float mnMaxX;
  213. static float mnMinY;
  214. static float mnMaxY;
  215. static bool mbInitialComputations;
  216. map<long unsigned int, cv::Point2f> mmProjectPoints;
  217. map<long unsigned int, cv::Point2f> mmMatchedInImage;
  218. string mNameFile;
  219. int mnDataset;
  220. #ifdef REGISTER_TIMES
  221. double mTimeORB_Ext;
  222. double mTimeStereoMatch;
  223. #endif
  224. private:
  225. // Undistort keypoints given OpenCV distortion parameters.
  226. // Only for the RGB-D case. Stereo must be already rectified!
  227. // (called in the constructor).
  228. void UndistortKeyPoints();
  229. // Computes image bounds for the undistorted image (called in the constructor).
  230. void ComputeImageBounds(const cv::Mat &imLeft);
  231. // Assign keypoints to the grid for speed up feature matching (called in the constructor).
  232. void AssignFeaturesToGrid();
  233. bool mbIsSet;
  234. bool mbImuPreintegrated;
  235. std::mutex *mpMutexImu;
  236. public:
  237. GeometricCamera* mpCamera, *mpCamera2;
  238. //Number of KeyPoints extracted in the left and right images
  239. int Nleft, Nright;
  240. //Number of Non Lapping Keypoints
  241. int monoLeft, monoRight;
  242. //For stereo matching
  243. std::vector<int> mvLeftToRightMatch, mvRightToLeftMatch;
  244. //For stereo fisheye matching
  245. static cv::BFMatcher BFmatcher;
  246. //Triangulated stereo observations using as reference the left camera. These are
  247. //computed during ComputeStereoFishEyeMatches
  248. std::vector<Eigen::Vector3f> mvStereo3Dpoints;
  249. //Grid for the right image
  250. std::vector<std::size_t> mGridRight[FRAME_GRID_COLS][FRAME_GRID_ROWS];
  251. Frame(const cv::Mat &imLeft, const cv::Mat &imRight, const double &timeStamp, ORBextractor* extractorLeft, ORBextractor* extractorRight, ORBVocabulary* voc, cv::Mat &K, cv::Mat &distCoef, const float &bf, const float &thDepth, GeometricCamera* pCamera, GeometricCamera* pCamera2, Sophus::SE3f& Tlr,Frame* pPrevF = static_cast<Frame*>(NULL), const IMU::Calib &ImuCalib = IMU::Calib());
  252. //Stereo fisheye
  253. void ComputeStereoFishEyeMatches();
  254. bool isInFrustumChecks(MapPoint* pMP, float viewingCosLimit, bool bRight = false);
  255. Eigen::Vector3f UnprojectStereoFishEye(const int &i);
  256. cv::Mat imgLeft, imgRight;
  257. void PrintPointDistribution(){
  258. int left = 0, right = 0;
  259. int Nlim = (Nleft != -1) ? Nleft : N;
  260. for(int i = 0; i < N; i++){
  261. if(mvpMapPoints[i] && !mvbOutlier[i]){
  262. if(i < Nlim) left++;
  263. else right++;
  264. }
  265. }
  266. cout << "Point distribution in Frame: left-> " << left << " --- right-> " << right << endl;
  267. }
  268. Sophus::SE3<double> T_test;
  269. };
  270. }// namespace ORB_SLAM
  271. #endif // FRAME_H