SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
WAIKeyFrame.h
Go to the documentation of this file.
1 /**
2  * \file WAIKeyframe.h
3  * \authors Michael Goettlicher
4  * \date October 2017
5 // Codestyle: https://github.com/cpvrlab/SLProject/wiki/Coding-Style-Guidelines
6  * \authors Marcus Hudritsch
7  * \copyright http://opensource.org/licenses/GPL-3.0
8 */
9 
10 /**
11  * This file is part of ORB-SLAM2.
12  *
13  * Copyright (C) 2014-2016 Raúl Mur-Artal <raulmur at unizar dot es> (University of Zaragoza)
14  * For more information see <https://github.com/raulmur/ORB_SLAM2>
15  *
16  * ORB-SLAM2 is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * ORB-SLAM2 is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with ORB-SLAM2. If not, see <http://www.gnu.org/licenses/>.
28  */
29 
30 #ifndef WAIKEYFRAME_H
31 #define WAIKEYFRAME_H
32 
33 #include <vector>
34 #include <mutex>
35 #include <string>
36 
37 #include <DBoW2/BowVector.h>
38 #include <DBoW2/FeatureVector.h>
39 #include <opencv2/core/core.hpp>
40 
41 #include <WAIHelper.h>
42 #include <WAIOrbVocabulary.h>
43 #include <WAIFrame.h>
44 #include <WAIMath.h>
45 
46 using namespace ORB_SLAM2;
47 
48 class WAIMapPoint;
49 class WAIKeyFrameDB;
50 class WAIMap;
51 
52 //-----------------------------------------------------------------------------
53 //! AR Keyframe node class
54 /*! A Keyframe is a camera with a position and additional information about key-
55 points that were found in this frame. It also contains descriptors for the found
56 keypoints.
57 */
58 
60 {
61 public:
62  //!keyframe generation during map loading
63  WAIKeyFrame(const cv::Mat& Tcw,
64  unsigned long id,
65  bool fixKF,
66  float fx,
67  float fy,
68  float cx,
69  float cy,
70  size_t N,
71  const std::vector<cv::KeyPoint>& vKeysUn,
72  const cv::Mat& descriptors,
73  WAIOrbVocabulary* vocabulary,
74  int nScaleLevels,
75  float fScaleFactor,
76  const std::vector<float>& vScaleFactors,
77  const std::vector<float>& vLevelSigma2,
78  const std::vector<float>& vInvLevelSigma2,
79  int nMinX,
80  int nMinY,
81  int nMaxX,
82  int nMaxY,
83  const cv::Mat& KB);
84 
85  //!keyframe generation from frame
86  WAIKeyFrame(WAIFrame& F, bool retainImg = true);
87 
88  // Pose functions
89  void SetPose(const cv::Mat& Tcw);
90  cv::Mat GetPose();
91  cv::Mat GetPoseInverse();
92  cv::Mat GetCameraCenter();
93  cv::Mat GetRotation();
94  cv::Mat GetTranslation();
95 
96  // Bag of Words Representation
97  void ComputeBoW(WAIOrbVocabulary* vocabulary);
98  void SetBowVector(WAIBowVector& bow);
99 
100  // Covisibility graph functions
101  void AddConnection(WAIKeyFrame* pKF, int weight);
102  void EraseConnection(WAIKeyFrame* pKF);
103  void FindAndUpdateConnections(bool buildSpanningTree = true);
104  void UpdateConnections(std::map<WAIKeyFrame*, int> KFcounter, bool buildSpanningTree);
105  void UpdateBestCovisibles();
106  std::set<WAIKeyFrame*> GetConnectedKeyFrames();
107  std::vector<WAIKeyFrame*> GetVectorCovisibleKeyFrames();
108  std::vector<WAIKeyFrame*> GetBestCovisibilityKeyFrames(const int& N);
109  std::vector<WAIKeyFrame*> GetCovisiblesByWeight(const int& w);
110  //get weight of covisibility connection to this keyframe
111  int GetWeight(WAIKeyFrame* pKF);
112  const std::map<WAIKeyFrame*, int>& GetConnectedKfWeights();
113 
114  // Spanning tree functions
115  void AddChild(WAIKeyFrame* pKF);
116  void EraseChild(WAIKeyFrame* pKF);
117  void ChangeParent(WAIKeyFrame* pKF);
118  std::set<WAIKeyFrame*> GetChilds();
119  WAIKeyFrame* GetParent();
120  bool hasChild(WAIKeyFrame* pKF);
121 
122  // Loop Edges
123  void AddLoopEdge(WAIKeyFrame* pKF);
124  std::set<WAIKeyFrame*> GetLoopEdges();
125 
126  // MapPoint observation functions
127  void AddMapPoint(WAIMapPoint* pMP, size_t idx);
128  void EraseMapPointMatch(WAIMapPoint* pMP);
129  void EraseMapPointMatch(const size_t& idx);
130  void ReplaceMapPointMatch(const size_t& idx, WAIMapPoint* pMP);
131  std::set<WAIMapPoint*> GetMapPoints();
132  std::vector<WAIMapPoint*> GetMapPointMatches();
133  int TrackedMapPoints(const int& minObs);
134  WAIMapPoint* GetMapPoint(const size_t& idx);
135  bool hasMapPoint(WAIMapPoint* mp);
136 
137  bool isFixed() const;
138 
139  // KeyPoint functions
140  std::vector<size_t> GetFeaturesInArea(const float& x, const float& y, const float& r) const;
141 
142  // Image
143  bool IsInImage(const float& x, const float& y) const;
144 
145  // Enable/Disable bad flag changes
146  void SetNotErase();
147  void SetErase();
148 
149  // Set/check bad flag
150  void SetBadFlag();
151  bool isBad();
152  // recursively check if kf is among children
153  bool findChildRecursive(WAIKeyFrame* kf);
154 
155  // Compute Scene Depth (q=2 median). Used in monocular.
156  float ComputeSceneMedianDepth(const int q);
157 
158  static bool weightComp(int a, int b)
159  {
160  return a > b;
161  }
162 
163  static bool lId(WAIKeyFrame* pKF1, WAIKeyFrame* pKF2)
164  {
165  return pKF1->mnId < pKF2->mnId;
166  }
167 
168  //get estimated size of this object
169  size_t getSizeOfCvMat(const cv::Mat& mat);
170  size_t getSizeOf();
171 
172  // The following variables are accesed from only 1 thread or never change (no mutex needed).
173 public:
174  static long unsigned int nNextId;
175  long unsigned int mnId;
176  const long unsigned int mnFrameId;
177 
178  const double mTimeStamp;
179 
180  //flags, if this keyframe is not to be culled and the pose of this keyframe is not to be optimized in local bundle adjustment
181  const bool _fixed = false;
182 
183  // Grid (to speed up feature matching)
184  const int mnGridCols;
185  const int mnGridRows;
188 
189  /*
190  // Variables used by the tracking
191  long unsigned int mnTrackReferenceForFrame = 0;
192  long unsigned int mnFuseTargetForKF;
193 
194  // Variables used by the local mapping
195  long unsigned int mnBALocalForKF;
196  long unsigned int mnBAFixedForKF;
197  */
198 
199  long unsigned int mnMarker[7];
200 
201  // Variables used by the keyframe database
202  long unsigned int mnLoopQuery = 0;
203  int mnLoopWords = 0;
204  float mLoopScore = -1.0;
205  long unsigned int mnRelocQuery = 0;
206  int mnRelocWords = 0;
207  float mRelocScore = -1.0f;
208 
209  // Variables used by loop closing
210  cv::Mat mTcwGBA;
211  cv::Mat mTcwRefGBA;
212 
213  // Calibration parameters
214  const float fx, fy, cx, cy, invfx, invfy; /*, mbf, mb, mThDepth;*/
215 
216  // Number of KeyPoints
217  const int N = 0;
218 
219  //undistorted keypoints
220  const std::vector<cv::KeyPoint> mvKeysUn;
221 
222  //image feature descriptors
223  const cv::Mat mDescriptors;
224 
225  //BoW
228 
229  // Pose relative to parent (this is computed when bad flag is activated)
230  cv::Mat mTcp;
231 
232  // Scale
233  const int mnScaleLevels;
234  const float mfScaleFactor;
235  const float mfLogScaleFactor;
236  const std::vector<float> mvScaleFactors;
237  const std::vector<float> mvLevelSigma2;
238  const std::vector<float> mvInvLevelSigma2;
239 
240  // Image bounds and calibration
241  const int mnMinX;
242  const int mnMinY;
243  const int mnMaxX;
244  const int mnMaxY;
245  const cv::Mat mK;
246 
247  //original image
248  cv::Mat imgGray;
249 
250  // The following variables need to be accessed trough a mutex to be thread safe.
251 protected:
252  // SE3 Pose and camera center
253  //! opencv coordinate representation: z-axis points to principlal point,
254  //! x-axis to the right and y-axis down
255  //! Infos about the pose: https://github.com/raulmur/ORB_SLAM2/issues/249
256  cv::Mat _Twc; //camera wrt world
257  cv::Mat _Tcw; //world wrt camera
258  //! camera center
259  cv::Mat Ow;
260 
261  // MapPoints associated to keypoints (this array contains NULL for every
262  //unassociated keypoint from original frame)
263  std::vector<WAIMapPoint*> mvpMapPoints;
264 
265  // Grid over the image to speed up feature matching
266  std::vector<std::size_t> mGrid[FRAME_GRID_COLS][FRAME_GRID_ROWS];
267 
268  //maps covisibility weights to this keyframe by keyframe pointer of the connected one
269  std::map<WAIKeyFrame*, int> mConnectedKeyFrameWeights;
270  //all covisibility keyframes ordered by weight (over min thres 15) (number of common keypoint observations)
271  std::vector<WAIKeyFrame*> mvpOrderedConnectedKeyFrames;
272  std::vector<int> mvOrderedWeights;
273 
274  // Spanning Tree and Loop Edges
275  bool mbFirstConnection = true;
276  WAIKeyFrame* mpParent = NULL;
277  std::set<WAIKeyFrame*> mspChildrens;
278  std::set<WAIKeyFrame*> mspLoopEdges;
279 
280  // Bad flags
283  bool mbBad;
284 
285 public:
286  std::mutex mMutexPose;
287  std::mutex mMutexConnections;
288  std::mutex mMutexFeatures;
289  //ghm1: added funtions
290  //set path to texture image
291  void setTexturePath(const std::string& path) { _pathToTexture = path; }
292  const std::string& getTexturePath() { return _pathToTexture; }
293 
294  //! get visual representation as SLPoints
295 #if 0
296  WAI::M4x4 getObjectMatrix();
297 #else
298  cv::Mat getObjectMatrix();
299 #endif
300 
301 private:
302  //! this is a function from Frame, but we need it here for map loading
303  void AssignFeaturesToGrid();
304  //! this is a function from Frame, but we need it here for map loading
305  bool PosInGrid(const cv::KeyPoint& kp, int& posX, int& posY);
306 
307  //path to background texture image
308  std::string _pathToTexture;
309 };
310 
311 #endif // !WAIKEYFRAME_H
#define FRAME_GRID_COLS
Definition: WAIFrame.h:44
#define FRAME_GRID_ROWS
Definition: WAIFrame.h:43
#define WAI_API
Definition: WAIHelper.h:36
AR Keyframe database class.
Definition: WAIKeyFrameDB.h:46
AR Keyframe node class.
Definition: WAIKeyFrame.h:60
std::vector< int > mvOrderedWeights
Definition: WAIKeyFrame.h:272
std::string _pathToTexture
Definition: WAIKeyFrame.h:308
const cv::Mat mK
Definition: WAIKeyFrame.h:245
std::mutex mMutexConnections
Definition: WAIKeyFrame.h:287
cv::Mat mTcwRefGBA
Definition: WAIKeyFrame.h:211
WAIBowVector mBowVec
Definition: WAIKeyFrame.h:226
const int mnMaxX
Definition: WAIKeyFrame.h:243
cv::Mat Ow
camera center
Definition: WAIKeyFrame.h:259
const std::vector< float > mvInvLevelSigma2
Definition: WAIKeyFrame.h:238
const float mfGridElementHeightInv
Definition: WAIKeyFrame.h:187
const int mnScaleLevels
Definition: WAIKeyFrame.h:233
std::vector< WAIMapPoint * > mvpMapPoints
Definition: WAIKeyFrame.h:263
const int mnGridCols
Definition: WAIKeyFrame.h:184
void setTexturePath(const std::string &path)
Definition: WAIKeyFrame.h:291
const int mnMinY
Definition: WAIKeyFrame.h:242
const double mTimeStamp
Definition: WAIKeyFrame.h:178
std::set< WAIKeyFrame * > mspLoopEdges
Definition: WAIKeyFrame.h:278
static bool weightComp(int a, int b)
Definition: WAIKeyFrame.h:158
cv::Mat mTcp
Definition: WAIKeyFrame.h:230
bool mbNotErase
Definition: WAIKeyFrame.h:281
const std::vector< float > mvScaleFactors
Definition: WAIKeyFrame.h:236
const long unsigned int mnFrameId
Definition: WAIKeyFrame.h:176
cv::Mat _Twc
Definition: WAIKeyFrame.h:256
std::map< WAIKeyFrame *, int > mConnectedKeyFrameWeights
Definition: WAIKeyFrame.h:269
long unsigned int mnId
Definition: WAIKeyFrame.h:175
const float mfScaleFactor
Definition: WAIKeyFrame.h:234
std::mutex mMutexPose
Definition: WAIKeyFrame.h:286
const int mnMaxY
Definition: WAIKeyFrame.h:244
const int mnMinX
Definition: WAIKeyFrame.h:241
const int mnGridRows
Definition: WAIKeyFrame.h:185
WAIFeatVector mFeatVec
Definition: WAIKeyFrame.h:227
const std::vector< cv::KeyPoint > mvKeysUn
Definition: WAIKeyFrame.h:220
const float mfGridElementWidthInv
Definition: WAIKeyFrame.h:186
cv::Mat _Tcw
Definition: WAIKeyFrame.h:257
cv::Mat imgGray
Definition: WAIKeyFrame.h:248
std::mutex mMutexFeatures
Definition: WAIKeyFrame.h:288
const float cx
Definition: WAIKeyFrame.h:214
bool mbToBeErased
Definition: WAIKeyFrame.h:282
cv::Mat mTcwGBA
Definition: WAIKeyFrame.h:210
std::vector< WAIKeyFrame * > mvpOrderedConnectedKeyFrames
Definition: WAIKeyFrame.h:271
const std::vector< float > mvLevelSigma2
Definition: WAIKeyFrame.h:237
const std::string & getTexturePath()
Definition: WAIKeyFrame.h:292
std::set< WAIKeyFrame * > mspChildrens
Definition: WAIKeyFrame.h:277
const cv::Mat mDescriptors
Definition: WAIKeyFrame.h:223
const float mfLogScaleFactor
Definition: WAIKeyFrame.h:235
static bool lId(WAIKeyFrame *pKF1, WAIKeyFrame *pKF2)
Definition: WAIKeyFrame.h:163
static long unsigned int nNextId
Definition: WAIKeyFrame.h:174
Definition: WAIMap.h:52