SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
WAIOrbVocabulary.cpp
Go to the documentation of this file.
1 /**
2  * \file WAIOrbVocabulary.cpp
3  * \authors Michael Goettlicher
4  * \date April 2018
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 #include <orb_slam/Converter.h>
11 #include <WAIOrbVocabulary.h>
12 #include <Utils.h>
13 
15 {
16 #if USE_FBOW
17  _vocabulary = new fbow::Vocabulary();
18 #else
19  _vocabulary = new ORB_SLAM2::ORBVocabulary();
20 #endif
21  _layer = layer;
22 }
23 
25 {
26  if (_vocabulary)
27  {
28 #if USE_FBOW
29  _vocabulary->clear();
30 #endif
31  delete _vocabulary;
32  }
33 
34  _vocabulary = nullptr;
35 }
36 
37 //-----------------------------------------------------------------------------
38 void WAIOrbVocabulary::loadFromFile(std::string strVocFile)
39 {
40  if (!_vocabulary)
41  {
42 #if USE_FBOW
43  _vocabulary = new fbow::Vocabulary();
44 #else
45  _vocabulary = new ORB_SLAM2::ORBVocabulary();
46 #endif
47  }
48 
49 #if USE_FBOW
50  try
51  {
52  _vocabulary->readFromFile(strVocFile);
53  }
54  catch (std::exception& e)
55  {
56  std::string err = "WAIOrbVocabulary::loadFromFile: failed to load vocabulary " +
57  strVocFile + ", exception:" + e.what();
58  throw std::runtime_error(err);
59  }
60 #else
61  bool bVocLoad = _vocabulary->loadFromBinaryFile(strVocFile);
62 
63  if (!bVocLoad)
64  {
65  std::string err = "WAIOrbVocabulary::loadFromFile: failed to load vocabulary " + strVocFile;
66  Utils::log("WAI", err.c_str());
67  throw std::runtime_error(err);
68  }
69 #endif
70 }
71 
72 WAIBowVector::WAIBowVector(std::vector<int> wid, std::vector<float> values)
73 {
74  for (int i = 0; i < wid.size(); i++)
75  {
76 #if USE_FBOW
77  fbow::_float v;
78  v.var = values[i];
79  data.insert(std::pair<uint32_t, fbow::_float>(wid[i], v));
80 #else
81  data.insert(std::pair<DBoW2::WordId, DBoW2::WordValue>(wid[i], values[i]));
82 #endif
83  }
84 }
85 
86 void WAIOrbVocabulary::transform(const cv::Mat& descriptors, WAIBowVector& bow, WAIFeatVector& feat)
87 {
88  bow.isFill = true;
89  feat.isFill = true;
90 
91  if (descriptors.rows == 0)
92  return;
93 
94 #if USE_FBOW
95  _vocabulary->transform(descriptors, _layer, bow.data, feat.data);
96 #else
97  vector<cv::Mat> vCurrentDesc = ORB_SLAM2::Converter::toDescriptorVector(descriptors);
98  _vocabulary->transform(vCurrentDesc, bow.data, feat.data, _vocabulary->getDepthLevels() - _layer);
99 #endif
100 }
101 
103 {
104 #if USE_FBOW
105  return fbow::fBow::score(bow1.data, bow2.data);
106 #else
107  return _vocabulary->score(bow1.data, bow2.data);
108 #endif
109 }
110 
112 {
113 #if USE_FBOW
114  return _vocabulary->size() * _vocabulary->getK();
115 #else
116  return _vocabulary->size();
117 #endif
118 }
119 
120 void WAIOrbVocabulary::create(std::vector<cv::Mat>& features, int k, int l)
121 {
122 #if USE_FBOW
123  fbow::VocabularyCreator vc;
124  fbow::VocabularyCreator::Params p;
125  p.k = k;
126  p.L = l;
127  p.nthreads = 100;
128  p.maxIters = 11;
129  p.verbose = true;
130 
131  std::cout << "Creating a " << p.k << "^" << p.L << " vocabulary..." << std::endl;
132  _vocabulary = new fbow::Vocabulary();
133 
134  vc.create(*_vocabulary, features, "slamvoc", p);
135  std::cout << "... done!" << std::endl;
136 #else
137  const DBoW2::WeightingType weight = DBoW2::TF_IDF;
138  const DBoW2::ScoringType score = DBoW2::L1_NORM;
139 
140  std::vector<std::vector<cv::Mat>> feats;
141  feats.resize(features.size());
142 
143  cout << "Creating a " << k << "^" << l << " vocabulary..." << endl;
144  for (int i = 0; i < features.size(); i++)
145  {
146  feats[i].resize(features[i].rows);
147  for (int j = 0; j < features[i].rows; j++)
148  feats[i].push_back(features[i].row(j));
149  }
150 
151  _vocabulary = new ORB_SLAM2::ORBVocabulary(k, l, weight, score);
152  _vocabulary->create(feats);
153 
154  cout << "... done!" << endl;
155 #endif
156 }
157 
158 void WAIOrbVocabulary::save(std::string path)
159 {
160 #if USE_FBOW
161  _vocabulary->saveToFile(path);
162 #else
163  _vocabulary->saveToBinaryFile(path);
164 #endif
165 }
fbow::Vocabulary * _vocabulary
void transform(const cv::Mat &descriptors, WAIBowVector &bow, WAIFeatVector &feat)
void create(std::vector< cv::Mat > &features, int k, int l)
void loadFromFile(std::string strVocFile)
void save(std::string path)
WAIOrbVocabulary(int layer=2)
double score(WAIBowVector &bow1, WAIBowVector &bow2)
void log(const char *tag, const char *format,...)
logs a formatted string platform independently
Definition: Utils.cpp:1103
fbow::fBow data
fbow::fBow2 data