Tracking.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 TRACKING_H
  19. #define TRACKING_H
  20. #include <opencv2/core/core.hpp>
  21. #include <opencv2/features2d/features2d.hpp>
  22. #include "Viewer.h"
  23. #include "FrameDrawer.h"
  24. #include "Atlas.h"
  25. #include "LocalMapping.h"
  26. #include "LoopClosing.h"
  27. #include "Frame.h"
  28. #include "ORBVocabulary.h"
  29. #include "KeyFrameDatabase.h"
  30. #include "ORBextractor.h"
  31. #include "MapDrawer.h"
  32. #include "System.h"
  33. #include "ImuTypes.h"
  34. #include "Settings.h"
  35. #include "GeometricCamera.h"
  36. #include <mutex>
  37. #include <unordered_set>
  38. namespace ORB_SLAM3
  39. {
  40. class Viewer;
  41. class FrameDrawer;
  42. class Atlas;
  43. class LocalMapping;
  44. class LoopClosing;
  45. class System;
  46. class Settings;
  47. class Tracking
  48. {
  49. public:
  50. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  51. Tracking(System* pSys, ORBVocabulary* pVoc, FrameDrawer* pFrameDrawer, MapDrawer* pMapDrawer, Atlas* pAtlas,
  52. KeyFrameDatabase* pKFDB, const string &strSettingPath, const int sensor, Settings* settings, const string &_nameSeq=std::string());
  53. ~Tracking();
  54. // Parse the config file
  55. bool ParseCamParamFile(cv::FileStorage &fSettings);
  56. bool ParseORBParamFile(cv::FileStorage &fSettings);
  57. bool ParseIMUParamFile(cv::FileStorage &fSettings);
  58. // Preprocess the input and call Track(). Extract features and performs stereo matching.
  59. Sophus::SE3f GrabImageStereo(const cv::Mat &imRectLeft,const cv::Mat &imRectRight, const double &timestamp, string filename);
  60. Sophus::SE3f GrabImageRGBD(const cv::Mat &imRGB,const cv::Mat &imD, const double &timestamp, string filename);
  61. Sophus::SE3f GrabImageMonocular(const cv::Mat &im, const double &timestamp, string filename);
  62. void GrabImuData(const IMU::Point &imuMeasurement);
  63. void SetLocalMapper(LocalMapping* pLocalMapper);
  64. void SetLoopClosing(LoopClosing* pLoopClosing);
  65. void SetViewer(Viewer* pViewer);
  66. void SetStepByStep(bool bSet);
  67. bool GetStepByStep();
  68. // Load new settings
  69. // The focal lenght should be similar or scale prediction will fail when projecting points
  70. void ChangeCalibration(const string &strSettingPath);
  71. // Use this function if you have deactivated local mapping and you only want to localize the camera.
  72. void InformOnlyTracking(const bool &flag);
  73. void UpdateFrameIMU(const float s, const IMU::Bias &b, KeyFrame* pCurrentKeyFrame);
  74. KeyFrame* GetLastKeyFrame()
  75. {
  76. return mpLastKeyFrame;
  77. }
  78. void CreateMapInAtlas();
  79. //std::mutex mMutexTracks;
  80. //--
  81. void NewDataset();
  82. int GetNumberDataset();
  83. int GetMatchesInliers();
  84. //DEBUG
  85. void SaveSubTrajectory(string strNameFile_frames, string strNameFile_kf, string strFolder="");
  86. void SaveSubTrajectory(string strNameFile_frames, string strNameFile_kf, Map* pMap);
  87. float GetImageScale();
  88. #ifdef REGISTER_LOOP
  89. void RequestStop();
  90. bool isStopped();
  91. void Release();
  92. bool stopRequested();
  93. #endif
  94. public:
  95. // Tracking states
  96. enum eTrackingState{
  97. SYSTEM_NOT_READY=-1,
  98. NO_IMAGES_YET=0,
  99. NOT_INITIALIZED=1,
  100. OK=2,
  101. RECENTLY_LOST=3,
  102. LOST=4,
  103. OK_KLT=5
  104. };
  105. eTrackingState mState;
  106. eTrackingState mLastProcessedState;
  107. // Input sensor
  108. int mSensor;
  109. // Current Frame
  110. Frame mCurrentFrame;
  111. Frame mLastFrame;
  112. cv::Mat mImGray;
  113. // Initialization Variables (Monocular)
  114. std::vector<int> mvIniLastMatches;
  115. std::vector<int> mvIniMatches;
  116. std::vector<cv::Point2f> mvbPrevMatched;
  117. std::vector<cv::Point3f> mvIniP3D;
  118. Frame mInitialFrame;
  119. // Lists used to recover the full camera trajectory at the end of the execution.
  120. // Basically we store the reference keyframe for each frame and its relative transformation
  121. list<Sophus::SE3f> mlRelativeFramePoses;
  122. list<KeyFrame*> mlpReferences;
  123. list<double> mlFrameTimes;
  124. list<bool> mlbLost;
  125. // frames with estimated pose
  126. int mTrackedFr;
  127. bool mbStep;
  128. // True if local mapping is deactivated and we are performing only localization
  129. bool mbOnlyTracking;
  130. void Reset(bool bLocMap = false);
  131. void ResetActiveMap(bool bLocMap = false);
  132. float mMeanTrack;
  133. bool mbInitWith3KFs;
  134. double t0; // time-stamp of first read frame
  135. double t0vis; // time-stamp of first inserted keyframe
  136. double t0IMU; // time-stamp of IMU initialization
  137. bool mFastInit = false;
  138. vector<MapPoint*> GetLocalMapMPS();
  139. bool mbWriteStats;
  140. #ifdef REGISTER_TIMES
  141. void LocalMapStats2File();
  142. void TrackStats2File();
  143. void PrintTimeStats();
  144. vector<double> vdRectStereo_ms;
  145. vector<double> vdResizeImage_ms;
  146. vector<double> vdORBExtract_ms;
  147. vector<double> vdStereoMatch_ms;
  148. vector<double> vdIMUInteg_ms;
  149. vector<double> vdPosePred_ms;
  150. vector<double> vdLMTrack_ms;
  151. vector<double> vdNewKF_ms;
  152. vector<double> vdTrackTotal_ms;
  153. #endif
  154. protected:
  155. // Main tracking function. It is independent of the input sensor.
  156. void Track();
  157. // Map initialization for stereo and RGB-D
  158. void StereoInitialization();
  159. // Map initialization for monocular
  160. void MonocularInitialization();
  161. //void CreateNewMapPoints();
  162. void CreateInitialMapMonocular();
  163. void CheckReplacedInLastFrame();
  164. bool TrackReferenceKeyFrame();
  165. void UpdateLastFrame();
  166. bool TrackWithMotionModel();
  167. bool PredictStateIMU();
  168. bool Relocalization();
  169. void UpdateLocalMap();
  170. void UpdateLocalPoints();
  171. void UpdateLocalKeyFrames();
  172. bool TrackLocalMap();
  173. void SearchLocalPoints();
  174. bool NeedNewKeyFrame();
  175. void CreateNewKeyFrame();
  176. // Perform preintegration from last frame
  177. void PreintegrateIMU();
  178. // Reset IMU biases and compute frame velocity
  179. void ResetFrameIMU();
  180. bool mbMapUpdated;
  181. // Imu preintegration from last frame
  182. IMU::Preintegrated *mpImuPreintegratedFromLastKF;
  183. // Queue of IMU measurements between frames
  184. std::list<IMU::Point> mlQueueImuData;
  185. // Vector of IMU measurements from previous to current frame (to be filled by PreintegrateIMU)
  186. std::vector<IMU::Point> mvImuFromLastFrame;
  187. std::mutex mMutexImuQueue;
  188. // Imu calibration parameters
  189. IMU::Calib *mpImuCalib;
  190. // Last Bias Estimation (at keyframe creation)
  191. IMU::Bias mLastBias;
  192. // In case of performing only localization, this flag is true when there are no matches to
  193. // points in the map. Still tracking will continue if there are enough matches with temporal points.
  194. // In that case we are doing visual odometry. The system will try to do relocalization to recover
  195. // "zero-drift" localization to the map.
  196. bool mbVO;
  197. //Other Thread Pointers
  198. LocalMapping* mpLocalMapper;
  199. LoopClosing* mpLoopClosing;
  200. //ORB
  201. ORBextractor* mpORBextractorLeft, *mpORBextractorRight;
  202. ORBextractor* mpIniORBextractor;
  203. //BoW
  204. ORBVocabulary* mpORBVocabulary;
  205. KeyFrameDatabase* mpKeyFrameDB;
  206. // Initalization (only for monocular)
  207. bool mbReadyToInitializate;
  208. bool mbSetInit;
  209. //Local Map
  210. KeyFrame* mpReferenceKF;
  211. std::vector<KeyFrame*> mvpLocalKeyFrames;
  212. std::vector<MapPoint*> mvpLocalMapPoints;
  213. // System
  214. System* mpSystem;
  215. //Drawers
  216. Viewer* mpViewer;
  217. FrameDrawer* mpFrameDrawer;
  218. MapDrawer* mpMapDrawer;
  219. bool bStepByStep;
  220. //Atlas
  221. Atlas* mpAtlas;
  222. //Calibration matrix
  223. cv::Mat mK;
  224. Eigen::Matrix3f mK_;
  225. cv::Mat mDistCoef;
  226. float mbf;
  227. float mImageScale;
  228. float mImuFreq;
  229. double mImuPer;
  230. bool mInsertKFsLost;
  231. //New KeyFrame rules (according to fps)
  232. int mMinFrames;
  233. int mMaxFrames;
  234. int mnFirstImuFrameId;
  235. int mnFramesToResetIMU;
  236. // Threshold close/far points
  237. // Points seen as close by the stereo/RGBD sensor are considered reliable
  238. // and inserted from just one frame. Far points requiere a match in two keyframes.
  239. float mThDepth;
  240. // For RGB-D inputs only. For some datasets (e.g. TUM) the depthmap values are scaled.
  241. float mDepthMapFactor;
  242. //Current matches in frame
  243. int mnMatchesInliers;
  244. //Last Frame, KeyFrame and Relocalisation Info
  245. KeyFrame* mpLastKeyFrame;
  246. unsigned int mnLastKeyFrameId;
  247. unsigned int mnLastRelocFrameId;
  248. double mTimeStampLost;
  249. double time_recently_lost;
  250. unsigned int mnFirstFrameId;
  251. unsigned int mnInitialFrameId;
  252. unsigned int mnLastInitFrameId;
  253. bool mbCreatedMap;
  254. //Motion Model
  255. bool mbVelocity{false};
  256. Sophus::SE3f mVelocity;
  257. //Color order (true RGB, false BGR, ignored if grayscale)
  258. bool mbRGB;
  259. list<MapPoint*> mlpTemporalPoints;
  260. //int nMapChangeIndex;
  261. int mnNumDataset;
  262. ofstream f_track_stats;
  263. ofstream f_track_times;
  264. double mTime_PreIntIMU;
  265. double mTime_PosePred;
  266. double mTime_LocalMapTrack;
  267. double mTime_NewKF_Dec;
  268. GeometricCamera* mpCamera, *mpCamera2;
  269. int initID, lastID;
  270. Sophus::SE3f mTlr;
  271. void newParameterLoader(Settings* settings);
  272. #ifdef REGISTER_LOOP
  273. bool Stop();
  274. bool mbStopped;
  275. bool mbStopRequested;
  276. bool mbNotStop;
  277. std::mutex mMutexStop;
  278. #endif
  279. public:
  280. cv::Mat mImRight;
  281. };
  282. } //namespace ORB_SLAM
  283. #endif // TRACKING_H