SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
WAISlam.h
Go to the documentation of this file.
1 #ifndef WAISLAM_H
2 #define WAISLAM_H
3 
4 #include <WAIHelper.h>
5 #include <WAIKeyFrameDB.h>
6 #include <WAIMap.h>
7 #include <orb_slam/LocalMapping.h>
8 #include <orb_slam/LoopClosing.h>
9 #include <orb_slam/Initializer.h>
10 #include <orb_slam/ORBmatcher.h>
11 #include <orb_slam/Optimizer.h>
12 #include <orb_slam/PnPsolver.h>
13 #include <LocalMap.h>
14 #include <opencv2/core.hpp>
15 #include <WAISlamTools.h>
16 #include <WAIMap.h>
17 #include <WAIMapPoint.h>
18 #include <WAIKeyFrame.h>
19 #include <memory>
20 
21 //-----------------------------------------------------------------------------
22 /*
23  * This class should not be instanciated. It contains only pure virtual methods
24  * and some variables with getter that are useful for slam in a subclass.
25  */
26 class WAISlam : public WAISlamTools
27 {
28 public:
29  struct Params
30  {
31  // ensure all new keyframe have enough in common with loaded map
32  bool ensureKFIntegration = false;
33  // wait for localmapping
34  bool serial = false;
35  // retain the images in the keyframes, so we can store them later
36  bool retainImg = false;
37  // in onlyTracking mode we do not use local mapping and loop closing
38  bool onlyTracking = false;
39  // If true, keyframes loaded from a map will not be culled and the pose will not be changed. Local bundle adjustment is applied only on newly added kfs.
40  // Also, the loop closing will be disabled so that there will be no optimization of the essential graph and no global bundle adjustment.
41  bool fixOldKfs = false;
42  // use lucas canade optical flow tracking
43  bool trackOptFlow = false;
44 
45  // keyframe culling strategy params:
46  // A keyframe is considered redundant if _cullRedundantPerc of the MapPoints it sees, are seen
47  // in at least other 3 keyframes (in the same or finer scale)
48  float cullRedundantPerc = 0.95f; // originally it was 0.9
49 
50  // Min common words as a factor of max common words within candidates
51  // for relocalization and loop closing
52  float minCommonWordFactor = 0.8f;
53 
54  // Min acceleration score filter in detectRelocalizationCandidates
55  bool minAccScoreFilter = false;
56  };
57 
58  WAISlam(const cv::Mat& intrinsic,
59  const cv::Mat& distortion,
60  WAIOrbVocabulary* voc,
61  KPextractor* iniExtractor,
62  KPextractor* relocExtractor,
63  KPextractor* extractor,
64  std::unique_ptr<WAIMap> globalMap,
65  WAISlam::Params params);
66 
67  virtual ~WAISlam();
68 
69  virtual void reset();
70 
71  void changeIntrinsic(cv::Mat intrinsic, cv::Mat distortion);
72  void createFrame(WAIFrame& frame, cv::Mat& imageGray);
73 
74  virtual void updatePose(WAIFrame& frame);
75  virtual bool update(cv::Mat& imageGray);
76  virtual void updatePoseKFIntegration(WAIFrame& frame);
77  virtual void resume();
78 
79  virtual bool isTracking();
80  virtual bool hasStateIdle();
81  virtual void requestStateIdle();
82 
83  virtual bool retainImage();
84 
85  void transformCoords(cv::Mat transform);
86 
87  std::vector<WAIMapPoint*> getMatchedMapPoints(WAIFrame* frame);
89  std::pair<std::vector<cv::Point2f>,
90  std::vector<cv::Point3f>>& matching);
91 
92  virtual bool isInitialized() { return _initialized; }
93  virtual WAIMap* getMap() { return _globalMap.get(); }
94  virtual WAIFrame getLastFrame();
95  virtual WAIFrame* getLastFramePtr();
96  virtual std::vector<WAIMapPoint*> getLocalMapPoints() { return _localMap.mapPoints; }
97  virtual int getNumKeyFrames() { return (int)_globalMap->KeyFramesInMap(); }
98 
99  virtual std::vector<WAIMapPoint*> getMapPoints()
100  {
101  if (_globalMap != nullptr)
102  return _globalMap->GetAllMapPoints();
103  return std::vector<WAIMapPoint*>();
104  }
105 
106  virtual std::vector<WAIKeyFrame*> getKeyFrames()
107  {
108  if (_globalMap != nullptr)
109  return _globalMap->GetAllKeyFrames();
110  return std::vector<WAIKeyFrame*>();
111  }
112 
113  virtual std::string getPrintableState()
114  {
115  switch (_state)
116  {
118  return std::string("TrackingState_Idle\n");
119  break;
121  return std::string("Initializing");
122  break;
124  return std::string("None");
125  break;
127  return std::string("TrackingLost");
128  break;
130  return std::string("TrackingOK");
131  break;
133  return std::string("TrackingStart");
134  break;
135  default:
136  return std::string("");
137  }
138  }
139 
140  virtual int getKeyPointCount() { return _lastFrame.N; }
141  virtual int getKeyFrameCount() { return (int)_globalMap->KeyFramesInMap(); }
142  virtual int getMapPointCount() { return (int)_globalMap->MapPointsInMap(); }
143  // get camera extrinsic
144  virtual cv::Mat getPose();
145  // set camera extrinsic guess
146  virtual void setCamExrinsicGuess(cv::Mat extrinsicGuess);
147  virtual void setMap(std::unique_ptr<WAIMap> globalMap);
148 
150 
151  virtual void drawInfo(cv::Mat& imageBGR,
152  float scale,
153  bool showInitLine,
154  bool showKeyPoints,
155  bool showKeyPointsMatched);
156 
157  KPextractor* getKPextractor()
158  {
159  return _extractor;
160  };
161 
162  int getMapPointMatchesCount() const;
163 
164  std::string getLoopCloseStatus();
165 
166  int getLoopCloseCount();
167 
169 
170 protected:
171  void updateState(WAITrackingState state);
172 
174  bool _isFinish;
175  bool _isStop;
176  std::mutex _stateMutex;
177  bool finishRequested();
178  void requestFinish();
179  bool isStop();
180  bool isFinished();
181  void flushQueue();
182  int getNextFrame(WAIFrame& frame);
183  static void updatePoseThread(WAISlam* ptr);
184 
188  std::mutex _mutexStates;
189  std::mutex _lastFrameMutex;
190 
192 
193  unsigned int _relocFrameCounter = 0;
194  unsigned long _lastRelocFrameId = 0;
195  unsigned long _lastKeyFrameFrameId = 0;
196  KPextractor* _extractor = nullptr;
197  KPextractor* _relocExtractor = nullptr;
198  KPextractor* _iniExtractor = nullptr;
200  std::thread* _poseUpdateThread;
201  std::queue<WAIFrame> _framesQueue;
202  std::mutex _frameQueueMutex;
203 };
204 //-----------------------------------------------------------------------------
205 #endif
WAITrackingState
Definition: WAISlamTools.h:27
@ TrackingOK
Definition: WAISlamTools.h:31
@ TrackingStart
Definition: WAISlamTools.h:33
@ TrackingLost
Definition: WAISlamTools.h:32
@ None
Definition: WAISlamTools.h:28
@ Initializing
Definition: WAISlamTools.h:30
@ Idle
Definition: WAISlamTools.h:29
int N
Definition: WAIFrame.h:123
Definition: WAIMap.h:52
KPextractor * _iniExtractor
Definition: WAISlam.h:198
std::queue< WAIFrame > _framesQueue
Definition: WAISlam.h:201
std::mutex _cameraExtrinsicGuessMutex
Definition: WAISlam.h:187
unsigned long _lastRelocFrameId
Definition: WAISlam.h:194
int getMatchedCorrespondances(WAIFrame *frame, std::pair< std::vector< cv::Point2f >, std::vector< cv::Point3f >> &matching)
Definition: WAISlam.cpp:531
virtual int getNumKeyFrames()
Definition: WAISlam.h:97
std::mutex _mutexStates
Definition: WAISlam.h:188
std::vector< WAIMapPoint * > getMatchedMapPoints(WAIFrame *frame)
Definition: WAISlam.cpp:515
bool isStop()
Definition: WAISlam.cpp:243
virtual void reset()
Definition: WAISlam.cpp:121
WAISlam::Params _params
Definition: WAISlam.h:191
bool _isFinish
Definition: WAISlam.h:174
bool _requestFinish
Definition: WAISlam.h:173
virtual bool isInitialized()
Definition: WAISlam.h:92
void flushQueue()
Definition: WAISlam.cpp:197
virtual void resume()
Definition: WAISlam.cpp:249
KPextractor * _extractor
Definition: WAISlam.h:196
int _infoMatchedInliners
Definition: WAISlam.h:199
void transformCoords(cv::Mat transform)
Definition: WAISlam.cpp:596
virtual void setCamExrinsicGuess(cv::Mat extrinsicGuess)
Definition: WAISlam.cpp:557
unsigned long _lastKeyFrameFrameId
Definition: WAISlam.h:195
std::mutex _stateMutex
Definition: WAISlam.h:176
std::string getLoopCloseStatus()
Definition: WAISlam.cpp:629
virtual bool update(cv::Mat &imageGray)
Definition: WAISlam.cpp:471
virtual bool isTracking()
Definition: WAISlam.cpp:585
std::mutex _lastFrameMutex
Definition: WAISlam.h:189
virtual ~WAISlam()
Definition: WAISlam.cpp:93
virtual WAITrackingState getTrackingState()
Definition: WAISlam.h:149
WAITrackingState _state
Definition: WAISlam.h:185
virtual std::vector< WAIMapPoint * > getMapPoints()
Definition: WAISlam.h:99
virtual std::string getPrintableState()
Definition: WAISlam.h:113
virtual cv::Mat getPose()
Definition: WAISlam.cpp:551
virtual std::vector< WAIMapPoint * > getLocalMapPoints()
Definition: WAISlam.h:96
virtual WAIFrame * getLastFramePtr()
Definition: WAISlam.cpp:465
void updateState(WAITrackingState state)
Definition: WAISlam.cpp:206
int getMapPointMatchesCount() const
Definition: WAISlam.cpp:624
void createFrame(WAIFrame &frame, cv::Mat &imageGray)
Definition: WAISlam.cpp:162
bool finishRequested()
Definition: WAISlam.cpp:231
virtual void setMap(std::unique_ptr< WAIMap > globalMap)
Definition: WAISlam.cpp:615
void requestFinish()
Definition: WAISlam.cpp:225
virtual int getKeyFrameCount()
Definition: WAISlam.h:141
virtual bool hasStateIdle()
Definition: WAISlam.cpp:579
void changeIntrinsic(cv::Mat intrinsic, cv::Mat distortion)
Definition: WAISlam.cpp:156
KPextractor * getKPextractor()
Definition: WAISlam.h:157
int getNextFrame(WAIFrame &frame)
Definition: WAISlam.cpp:212
bool isFinished()
Definition: WAISlam.cpp:237
virtual void updatePoseKFIntegration(WAIFrame &frame)
Definition: WAISlam.cpp:395
virtual bool retainImage()
Definition: WAISlam.cpp:591
int getKeyFramesInLoopCloseQueueCount()
Definition: WAISlam.cpp:639
bool _isStop
Definition: WAISlam.h:175
virtual int getKeyPointCount()
Definition: WAISlam.h:140
virtual void requestStateIdle()
Definition: WAISlam.cpp:563
static void updatePoseThread(WAISlam *ptr)
Definition: WAISlam.cpp:257
KPextractor * _relocExtractor
Definition: WAISlam.h:197
virtual void drawInfo(cv::Mat &imageBGR, float scale, bool showInitLine, bool showKeyPoints, bool showKeyPointsMatched)
Definition: WAISlam.cpp:488
virtual WAIFrame getLastFrame()
Definition: WAISlam.cpp:459
std::thread * _poseUpdateThread
Definition: WAISlam.h:200
virtual std::vector< WAIKeyFrame * > getKeyFrames()
Definition: WAISlam.h:106
virtual void updatePose(WAIFrame &frame)
Definition: WAISlam.cpp:291
int getLoopCloseCount()
Definition: WAISlam.cpp:634
std::mutex _frameQueueMutex
Definition: WAISlam.h:202
virtual WAIMap * getMap()
Definition: WAISlam.h:93
std::mutex _cameraExtrinsicMutex
Definition: WAISlam.h:186
WAISlam(const cv::Mat &intrinsic, const cv::Mat &distortion, WAIOrbVocabulary *voc, KPextractor *iniExtractor, KPextractor *relocExtractor, KPextractor *extractor, std::unique_ptr< WAIMap > globalMap, WAISlam::Params params)
Definition: WAISlam.cpp:15
unsigned int _relocFrameCounter
Definition: WAISlam.h:193
virtual int getMapPointCount()
Definition: WAISlam.h:142
std::unique_ptr< WAIMap > _globalMap
Definition: WAISlamTools.h:208
WAIFrame _lastFrame
Definition: WAISlamTools.h:206
LocalMap _localMap
Definition: WAISlamTools.h:209
bool ensureKFIntegration
Definition: WAISlam.h:32
bool fixOldKfs
Definition: WAISlam.h:41
bool minAccScoreFilter
Definition: WAISlam.h:55
bool trackOptFlow
Definition: WAISlam.h:43
bool retainImg
Definition: WAISlam.h:36
float cullRedundantPerc
Definition: WAISlam.h:48
float minCommonWordFactor
Definition: WAISlam.h:52
bool onlyTracking
Definition: WAISlam.h:38