SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
WAIMapStorage.cpp File Reference
#include <WAIMapStorage.h>
#include <Profiler.h>
Include dependency graph for WAIMapStorage.cpp:

Go to the source code of this file.

Functions

void buildMatching (std::vector< WAIKeyFrame * > &kfs, std::map< WAIKeyFrame *, std::map< size_t, size_t >> &KFmatching)
 
void saveKeyFrames (std::vector< WAIKeyFrame * > &kfs, std::map< WAIKeyFrame *, std::map< size_t, size_t >> &KFmatching, cv::FileStorage &fs, std::string imgDir, bool saveBOW)
 
void saveMapPoints (std::vector< WAIMapPoint * > mpts, std::map< WAIKeyFrame *, std::map< size_t, size_t >> &KFmatching, cv::FileStorage &fs)
 

Function Documentation

◆ buildMatching()

void buildMatching ( std::vector< WAIKeyFrame * > &  kfs,
std::map< WAIKeyFrame *, std::map< size_t, size_t >> &  KFmatching 
)

Definition at line 54 of file WAIMapStorage.cpp.

56 {
57  for (int i = 0; i < kfs.size(); ++i)
58  {
59  WAIKeyFrame* kf = kfs[i];
60  if (kf->isBad())
61  continue;
62  // if (kf->mBowVec.data.empty())
63  // continue;
64 
65  std::vector<WAIMapPoint*> mps = kf->GetMapPointMatches();
66  std::map<size_t, size_t> matching;
67 
68  size_t id = 0;
69  for (int j = 0; j < mps.size(); j++)
70  {
71  if (mps[j] != nullptr)
72  {
73  matching.insert(std::pair<size_t, size_t>(j, id));
74  id++;
75  }
76  }
77  KFmatching.insert(std::pair<WAIKeyFrame*, std::map<size_t, size_t>>(kf, matching));
78  }
79 }
AR Keyframe node class.
Definition: WAIKeyFrame.h:60
std::vector< WAIMapPoint * > GetMapPointMatches()

◆ saveKeyFrames()

void saveKeyFrames ( std::vector< WAIKeyFrame * > &  kfs,
std::map< WAIKeyFrame *, std::map< size_t, size_t >> &  KFmatching,
cv::FileStorage &  fs,
std::string  imgDir,
bool  saveBOW 
)

Definition at line 81 of file WAIMapStorage.cpp.

86 {
87  // start sequence keyframes
88  fs << "KeyFrames"
89  << "[";
90  for (int i = 0; i < kfs.size(); ++i)
91  {
92  WAIKeyFrame* kf = kfs[i];
93  if (kf->isBad())
94  continue;
95  if (kf->mBowVec.data.empty())
96  continue;
97 
98  fs << "{"; // new map keyFrame
99  // add id
100  fs << "id" << (int)kf->mnId;
101  if (kf->mnId != 0) // kf with id 0 has no parent
102  fs << "parentId" << (int)kf->GetParent()->mnId;
103  else
104  fs << "parentId" << -1;
105 
106  // loop edges: we store the id of the connected kf
107  auto loopEdges = kf->GetLoopEdges();
108  if (loopEdges.size())
109  {
110  std::vector<int> loopEdgeIds;
111  for (auto loopEdgeKf : loopEdges)
112  {
113  loopEdgeIds.push_back(loopEdgeKf->mnId);
114  }
115  fs << "loopEdges" << loopEdgeIds;
116  }
117 
118  // world w.r.t camera
119  fs << "Tcw" << kf->GetPose();
120 
121  if (KFmatching.size() > 0)
122  {
123  cv::Mat descriptors;
124  const std::map<size_t, size_t>& matching = KFmatching[kf];
125  descriptors.create((int)matching.size(), 32, CV_8U);
126  std::vector<cv::KeyPoint> keypoints(matching.size());
127  for (int j = 0; j < kf->mvKeysUn.size(); j++)
128  {
129  auto it = matching.find(j);
130  if (it != matching.end())
131  {
132  kf->mDescriptors.row(j).copyTo(descriptors.row(it->second));
133  keypoints[it->second] = kf->mvKeysUn[j];
134  }
135  }
136  fs << "featureDescriptors" << descriptors;
137  fs << "keyPtsUndist" << keypoints;
138  }
139  else
140  {
141  fs << "featureDescriptors" << kf->mDescriptors;
142  fs << "keyPtsUndist" << kf->mvKeysUn;
143  }
144 
145  if (saveBOW)
146  {
147  WAIBowVector& bowVec = kf->mBowVec;
148  std::vector<int> wordsId;
149  std::vector<float> tfIdf;
150  for (auto it = bowVec.getWordScoreMapping().begin(); it != bowVec.getWordScoreMapping().end(); it++)
151  {
152  wordsId.push_back(it->first);
153  tfIdf.push_back(it->second);
154  }
155 
156  fs << "BowVectorWordsId" << wordsId;
157  fs << "TfIdf" << tfIdf;
158  }
159 
160  // scale factor
161  fs << "scaleFactor" << kf->mfScaleFactor;
162  // number of pyriamid scale levels
163  fs << "nScaleLevels" << kf->mnScaleLevels;
164  fs << "K" << kf->mK;
165 
166  fs << "nMinX" << kf->mnMinX;
167  fs << "nMinY" << kf->mnMinY;
168  fs << "nMaxX" << kf->mnMaxX;
169  fs << "nMaxY" << kf->mnMaxY;
170 
171 #if 0
172  std::vector<int> bestCovisibleKeyFrameIds;
173  std::vector<int> bestCovisibleWeights;
174  std::vector<WAIKeyFrame*> bestCovisibles = kf->GetBestCovisibilityKeyFrames(20);
175  for (WAIKeyFrame* covisible : bestCovisibles)
176  {
177  if (covisible->isBad())
178  continue;
179  int weight = kf->GetWeight(covisible);
180  if (weight)
181  {
182  bestCovisibleKeyFrameIds.push_back(covisible->mnId);
183  bestCovisibleWeights.push_back(weight);
184  }
185  }
186 
187  fs << "bestCovisibleKeyFrameIds" << bestCovisibleKeyFrameIds;
188  fs << "bestCovisibleWeights" << bestCovisibleWeights;
189 #endif
190 
191  fs << "}"; // close map
192 
193  // save the original frame image for this keyframe
194  if (imgDir != "")
195  {
196  cv::Mat imgColor;
197  if (!kf->imgGray.empty())
198  {
199  std::stringstream ss;
200  ss << imgDir << "kf" << (int)kf->mnId << ".jpg";
201 
202  cv::cvtColor(kf->imgGray, imgColor, cv::COLOR_GRAY2BGR);
203  cv::imwrite(ss.str(), imgColor);
204 
205  // if this kf was never loaded, we still have to set the texture path
206  kf->setTexturePath(ss.str());
207  }
208  }
209  }
210  fs << "]"; // close sequence keyframes
211 }
const cv::Mat mK
Definition: WAIKeyFrame.h:245
WAIBowVector mBowVec
Definition: WAIKeyFrame.h:226
std::vector< WAIKeyFrame * > GetBestCovisibilityKeyFrames(const int &N)
const int mnMaxX
Definition: WAIKeyFrame.h:243
const int mnScaleLevels
Definition: WAIKeyFrame.h:233
void setTexturePath(const std::string &path)
Definition: WAIKeyFrame.h:291
const int mnMinY
Definition: WAIKeyFrame.h:242
WAIKeyFrame * GetParent()
long unsigned int mnId
Definition: WAIKeyFrame.h:175
const float mfScaleFactor
Definition: WAIKeyFrame.h:234
std::set< WAIKeyFrame * > GetLoopEdges()
cv::Mat GetPose()
const int mnMaxY
Definition: WAIKeyFrame.h:244
const int mnMinX
Definition: WAIKeyFrame.h:241
const std::vector< cv::KeyPoint > mvKeysUn
Definition: WAIKeyFrame.h:220
cv::Mat imgGray
Definition: WAIKeyFrame.h:248
const cv::Mat mDescriptors
Definition: WAIKeyFrame.h:223
int GetWeight(WAIKeyFrame *pKF)
fbow::fBow & getWordScoreMapping()
fbow::fBow data

◆ saveMapPoints()

void saveMapPoints ( std::vector< WAIMapPoint * >  mpts,
std::map< WAIKeyFrame *, std::map< size_t, size_t >> &  KFmatching,
cv::FileStorage &  fs 
)

Definition at line 213 of file WAIMapStorage.cpp.

216 {
217  // start map points sequence
218  fs << "MapPoints"
219  << "[";
220  for (int i = 0; i < mpts.size(); ++i)
221  {
222  WAIMapPoint* mpt = mpts[i];
223  // TODO: ghm1: check if it is necessary to removed points that have no reference keyframe OR can we somehow update the reference keyframe in the SLAM
224  if (mpt->isBad() || mpt->refKf()->isBad())
225  continue;
226 
227  fs << "{"; // new map for MapPoint
228  // add id
229  fs << "id" << (int)mpt->mnId;
230  // add position
231  fs << "mWorldPos" << mpt->GetWorldPos();
232  // save keyframe observations
233  auto observations = mpt->GetObservations();
234  vector<int> observingKfIds;
235  vector<int> corrKpIndices; // corresponding keypoint indices in observing keyframe
236 
237  if (!KFmatching.empty())
238  {
239  for (auto it : observations)
240  {
241  WAIKeyFrame* kf = it.first;
242  size_t kpIdx = it.second;
243  if (!kf || kf->isBad() || kf->mBowVec.data.empty())
244  continue;
245 
246  if (KFmatching.find(kf) == KFmatching.end())
247  {
248  std::cout << "observation not found in kfmatching" << std::endl;
249  continue;
250  }
251 
252  const std::map<size_t, size_t>& matching = KFmatching[kf];
253  auto mit = matching.find(kpIdx);
254  if (mit != matching.end())
255  {
256  observingKfIds.push_back(kf->mnId);
257  corrKpIndices.push_back(mit->second);
258  }
259  }
260  }
261  else
262  {
263  for (auto it : observations)
264  {
265  if (!it.first->isBad())
266  {
267  observingKfIds.push_back(it.first->mnId);
268  corrKpIndices.push_back(it.second);
269  }
270  }
271  }
272 
273  fs << "observingKfIds" << observingKfIds;
274  fs << "corrKpIndices" << corrKpIndices;
275 
276  fs << "mfMaxDistance" << mpt->GetMaxDistance();
277  fs << "mfMinDistance" << mpt->GetMinDistance();
278  fs << "mNormalVector" << mpt->GetNormal();
279  fs << "mDescriptor" << mpt->GetDescriptor();
280 
281  fs << "refKfId" << (int)mpt->refKf()->mnId;
282  fs << "}"; // close map
283  }
284  fs << "]";
285 }
long unsigned int mnId
Definition: WAIMapPoint.h:108
std::map< WAIKeyFrame *, size_t > GetObservations()
cv::Mat GetNormal()
cv::Mat GetDescriptor()
float GetMaxDistance()
float GetMinDistance()
cv::Mat GetWorldPos()
WAIKeyFrame * refKf() const
Definition: WAIMapPoint.h:90