VWDictionary.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the Universite de Sherbrooke nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #pragma once
29 
30 #include "rtabmap/core/rtabmap_core_export.h" // DLL export/import defines
31 
32 #include <opencv2/highgui/highgui.hpp>
33 #include <opencv2/core/core.hpp>
34 #include <opencv2/features2d/features2d.hpp>
35 #include <list>
36 #include <set>
38 
39 namespace rtabmap
40 {
41 
42 class DBDriver;
43 class VisualWord;
44 class FlannIndex;
45 
46 class RTABMAP_CORE_EXPORT VWDictionary
47 {
48 public:
49  enum NNStrategy{
55  kNNUndef};
56  static const int ID_START;
57  static const int ID_INVALID;
58  static std::string nnStrategyName(NNStrategy strategy)
59  {
60  switch(strategy) {
61  case kNNFlannNaive:
62  return "FLANN NAIVE";
63  case kNNFlannKdTree:
64  return "FLANN KD-TREE";
65  case kNNFlannLSH:
66  return "FLANN LSH";
67  case kNNBruteForce:
68  return "BRUTE FORCE";
69  case kNNBruteForceGPU:
70  return "BRUTE FORCE GPU";
71  default:
72  return "Unknown";
73  }
74  }
75 
76 public:
77  VWDictionary(const ParametersMap & parameters = ParametersMap());
78  virtual ~VWDictionary();
79 
80  virtual void parseParameters(const ParametersMap & parameters);
81 
82  virtual void update();
83 
84  virtual std::list<int> addNewWords(
85  const cv::Mat & descriptors,
86  int signatureId);
87  virtual void addWord(VisualWord * vw);
88 
89  std::vector<int> findNN(const std::list<VisualWord *> & vws) const;
90  std::vector<int> findNN(const cv::Mat & descriptors) const;
91 
92  void addWordRef(int wordId, int signatureId);
93  void removeAllWordRef(int wordId, int signatureId);
94  const VisualWord * getWord(int id) const;
95  VisualWord * getUnusedWord(int id) const;
96  void setLastWordId(int id) {_lastWordId = id;}
97  const std::map<int, VisualWord *> & getVisualWords() const {return _visualWords;}
98  float getNndrRatio() const {return _nndrRatio;}
99  unsigned int getNotIndexedWordsCount() const {return (int)_notIndexedWords.size();}
100  int getLastIndexedWordId() const;
101  int getTotalActiveReferences() const {return _totalActiveReferences;}
102  unsigned int getIndexedWordsCount() const;
103  unsigned int getIndexMemoryUsed() const; // KB
104  unsigned long getMemoryUsed() const; //Bytes
105  bool setNNStrategy(NNStrategy strategy); // Return true if the search tree has been re-initialized
106  bool isIncremental() const {return _incrementalDictionary;}
107  bool isIncrementalFlann() const {return _incrementalFlann;}
108  void setIncrementalDictionary();
109  void setFixedDictionary(const std::string & dictionaryPath);
110 
111  void exportDictionary(const char * fileNameReferences, const char * fileNameDescriptors) const;
112 
113  void clear(bool printWarningsIfNotEmpty = true);
114  std::vector<VisualWord *> getUnusedWords() const;
115  std::vector<int> getUnusedWordIds() const;
116  unsigned int getUnusedWordsSize() const {return (int)_unusedWords.size();}
117  void removeWords(const std::vector<VisualWord*> & words); // caller must delete the words
118  void deleteUnusedWords();
119 
120 public:
121  static cv::Mat convertBinTo32F(const cv::Mat & descriptorsIn, bool byteToFloat = true);
122  static cv::Mat convert32FToBin(const cv::Mat & descriptorsIn, bool byteToFloat = true);
123 
124 protected:
125  int getNextId();
126 
127 protected:
128  std::map<int, VisualWord *> _visualWords; //<id,VisualWord*>
129  int _totalActiveReferences; // keep track of all references for updating the common signature
130 
131 private:
136  float _nndrRatio;
137  std::string _dictionaryPath; // a pre-computed dictionary (.txt or .db)
138  std::string _newDictionaryPath; // a pre-computed dictionary (.txt or .db)
143  cv::Mat _dataTree;
145  std::map<int ,int> _mapIndexId;
146  std::map<int ,int> _mapIdIndex;
147  std::map<int, VisualWord*> _unusedWords; //<id,VisualWord*>, note that these words stay in _visualWords
148  std::set<int> _notIndexedWords; // Words that are not indexed in the dictionary
149  std::set<int> _removedIndexedWords; // Words not anymore in the dictionary but still indexed in the dictionary
150 };
151 
152 } // namespace rtabmap
rtabmap::VWDictionary::ID_INVALID
static const int ID_INVALID
Definition: VWDictionary.h:57
update
def update(text)
rtabmap::VWDictionary::_strategy
NNStrategy _strategy
Definition: VWDictionary.h:144
rtabmap::VWDictionary::_visualWords
std::map< int, VisualWord * > _visualWords
Definition: VWDictionary.h:128
rtabmap::VWDictionary::kNNBruteForceGPU
@ kNNBruteForceGPU
Definition: VWDictionary.h:54
rtabmap::VWDictionary::getVisualWords
const std::map< int, VisualWord * > & getVisualWords() const
Definition: VWDictionary.h:97
rtabmap::VWDictionary::_mapIdIndex
std::map< int,int > _mapIdIndex
Definition: VWDictionary.h:146
rtabmap::VWDictionary::useDistanceL1_
bool useDistanceL1_
Definition: VWDictionary.h:141
rtabmap::VWDictionary::getUnusedWordsSize
unsigned int getUnusedWordsSize() const
Definition: VWDictionary.h:116
rtabmap::ParametersMap
std::map< std::string, std::string > ParametersMap
Definition: Parameters.h:43
rtabmap::VisualWord
Definition: VisualWord.h:38
Parameters.h
rtabmap::VWDictionary::_dataTree
cv::Mat _dataTree
Definition: VWDictionary.h:143
rtabmap::VWDictionary::isIncremental
bool isIncremental() const
Definition: VWDictionary.h:106
rtabmap::VWDictionary::isIncrementalFlann
bool isIncrementalFlann() const
Definition: VWDictionary.h:107
rtabmap::VWDictionary::getNotIndexedWordsCount
unsigned int getNotIndexedWordsCount() const
Definition: VWDictionary.h:99
rtabmap::VWDictionary::_totalActiveReferences
int _totalActiveReferences
Definition: VWDictionary.h:129
rtabmap::VWDictionary::_mapIndexId
std::map< int,int > _mapIndexId
Definition: VWDictionary.h:145
rtabmap::VWDictionary::_incrementalFlann
bool _incrementalFlann
Definition: VWDictionary.h:133
rtabmap::VWDictionary::_incrementalDictionary
bool _incrementalDictionary
Definition: VWDictionary.h:132
rtabmap::VWDictionary::setLastWordId
void setLastWordId(int id)
Definition: VWDictionary.h:96
rtabmap::VWDictionary::getTotalActiveReferences
int getTotalActiveReferences() const
Definition: VWDictionary.h:101
rtabmap::FlannIndex
Definition: FlannIndex.h:37
rtabmap::VWDictionary::_unusedWords
std::map< int, VisualWord * > _unusedWords
Definition: VWDictionary.h:147
rtabmap::VWDictionary::kNNFlannLSH
@ kNNFlannLSH
Definition: VWDictionary.h:52
rtabmap::VWDictionary::_newWordsComparedTogether
bool _newWordsComparedTogether
Definition: VWDictionary.h:139
rtabmap::VWDictionary::_nndrRatio
float _nndrRatio
Definition: VWDictionary.h:136
rtabmap::VWDictionary::_rebalancingFactor
float _rebalancingFactor
Definition: VWDictionary.h:134
rtabmap::VWDictionary::nnStrategyName
static std::string nnStrategyName(NNStrategy strategy)
Definition: VWDictionary.h:58
rtabmap::VWDictionary::kNNFlannKdTree
@ kNNFlannKdTree
Definition: VWDictionary.h:51
rtabmap::VWDictionary
Definition: VWDictionary.h:46
rtabmap::VWDictionary::kNNBruteForce
@ kNNBruteForce
Definition: VWDictionary.h:53
rtabmap::VWDictionary::_removedIndexedWords
std::set< int > _removedIndexedWords
Definition: VWDictionary.h:149
id
id
rtabmap::VWDictionary::_byteToFloat
bool _byteToFloat
Definition: VWDictionary.h:135
rtabmap::VWDictionary::_flannIndex
FlannIndex * _flannIndex
Definition: VWDictionary.h:142
rtabmap::VWDictionary::NNStrategy
NNStrategy
Definition: VWDictionary.h:49
rtabmap::VWDictionary::_notIndexedWords
std::set< int > _notIndexedWords
Definition: VWDictionary.h:148
rtabmap
Definition: CameraARCore.cpp:35
rtabmap::VWDictionary::_newDictionaryPath
std::string _newDictionaryPath
Definition: VWDictionary.h:138
rtabmap::VWDictionary::kNNFlannNaive
@ kNNFlannNaive
Definition: VWDictionary.h:50
rtabmap::VWDictionary::ID_START
static const int ID_START
Definition: VWDictionary.h:56
rtabmap::VWDictionary::_lastWordId
int _lastWordId
Definition: VWDictionary.h:140
rtabmap::VWDictionary::getNndrRatio
float getNndrRatio() const
Definition: VWDictionary.h:98
rtabmap::VWDictionary::_dictionaryPath
std::string _dictionaryPath
Definition: VWDictionary.h:137


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jul 25 2024 02:50:24