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/RtabmapExp.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 
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
std::map< std::string, std::string > ParametersMap
Definition: Parameters.h:43
const std::map< int, VisualWord * > & getVisualWords() const
Definition: VWDictionary.h:97
unsigned int getUnusedWordsSize() const
Definition: VWDictionary.h:116
bool isIncremental() const
Definition: VWDictionary.h:106
std::map< int,int > _mapIdIndex
Definition: VWDictionary.h:146
bool isIncrementalFlann() const
Definition: VWDictionary.h:107
#define RTABMAP_EXP
Definition: RtabmapExp.h:38
void setLastWordId(int id)
Definition: VWDictionary.h:96
static std::string nnStrategyName(NNStrategy strategy)
Definition: VWDictionary.h:58
std::map< int,int > _mapIndexId
Definition: VWDictionary.h:145
int getTotalActiveReferences() const
Definition: VWDictionary.h:101
unsigned int getNotIndexedWordsCount() const
Definition: VWDictionary.h:99
std::set< int > _notIndexedWords
Definition: VWDictionary.h:148
std::map< int, VisualWord * > _unusedWords
Definition: VWDictionary.h:147
std::string _newDictionaryPath
Definition: VWDictionary.h:138
std::set< int > _removedIndexedWords
Definition: VWDictionary.h:149
static const int ID_START
Definition: VWDictionary.h:56
float getNndrRatio() const
Definition: VWDictionary.h:98
static const int ID_INVALID
Definition: VWDictionary.h:57
std::string _dictionaryPath
Definition: VWDictionary.h:137
FlannIndex * _flannIndex
Definition: VWDictionary.h:142
std::map< int, VisualWord * > _visualWords
Definition: VWDictionary.h:128


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Jan 23 2023 03:38:58