RTAB-Map 0.23.10
Real-Time Appearance-Based Mapping
Loading...
Searching...
No Matches
VWDictionary.h
1/*
2Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, 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
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23ON 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
25SOFTWARE, 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#if CV_MAJOR_VERSION < 5
35#include <opencv2/features2d/features2d.hpp>
36#else
37#include <opencv2/features.hpp>
38#endif
39#include <list>
40#include <set>
41#include "rtabmap/core/Parameters.h"
42
43namespace rtabmap
44{
45
46class DBDriver;
47class VisualWord;
48class FlannIndex;
49
65class RTABMAP_CORE_EXPORT VWDictionary
66{
67public:
80
84 static const int ID_START;
85
89 static const int ID_INVALID;
90
96 static std::string nnStrategyName(NNStrategy strategy)
97 {
98 switch(strategy) {
99 case kNNFlannNaive:
100 return "FLANN NAIVE";
101 case kNNFlannKdTree:
102 return "FLANN KD-TREE";
103 case kNNFlannLSH:
104 return "FLANN LSH";
105 case kNNBruteForce:
106 return "BRUTE FORCE";
107 case kNNBruteForceGPU:
108 return "BRUTE FORCE GPU";
109 default:
110 return "Unknown";
111 }
112 }
113
114public:
119 VWDictionary(const ParametersMap & parameters = ParametersMap());
120
126 virtual ~VWDictionary();
127
132 virtual void parseParameters(const ParametersMap & parameters);
133
140 virtual void update();
141
156 virtual std::list<int> addNewWords(
157 const cv::Mat & descriptors,
158 int signatureId);
159
166 virtual void addWord(VisualWord * vw);
167
177 std::vector<int> findNN(const std::list<VisualWord *> & vws) const;
178
188 std::vector<int> findNN(const cv::Mat & descriptors) const;
189
199 bool addWordRef(int wordId, int signatureId);
200
209 void removeAllWordRef(int wordId, int signatureId);
210
216 const VisualWord * getWord(int id) const;
217
226 VisualWord * getUnusedWord(int id) const;
227
232 void setLastWordId(int id) {_lastWordId = id;}
233
238 const std::map<int, VisualWord *> & getVisualWords() const {return _visualWords;}
239
251 float getNndrRatio() const {return _nndrRatio;}
252
257 unsigned int getNotIndexedWordsCount() const {return (int)_notIndexedWords.size();}
258
264
269 int getTotalActiveReferences() const {return _totalActiveReferences;}
270
275 unsigned int getIndexedWordsCount() const;
276
281 unsigned int getIndexMemoryUsed() const; // KB
282
287 unsigned long getMemoryUsed() const; //Bytes
288
296 bool setNNStrategy(NNStrategy strategy);
297
302 NNStrategy getNNStrategy() const {return _strategy;}
303
308 bool isIncremental() const {return _incrementalDictionary;}
309
314 bool isIncrementalFlann() const {return _incrementalFlann;}
315
323
332 void setFixedDictionary(const std::string & dictionaryPath);
333
338 bool isModified() const;
339 // Re-index all the words from scratch. The index then contains the words in
340 // the same order than the one built by update() on a dictionary freshly
341 // loaded from a database, which is required to serialize it (the serialized
342 // index is matched against the words in that order when deserialized).
343 void rebuildIndex();
344
349 std::vector<unsigned char> serializeIndex() const;
350
356 bool deserializeIndex(const std::vector<unsigned char> & data);
357
364 bool deserializeIndex(const unsigned char * data, size_t size);
365
373 void exportDictionary(const char * fileNameReferences, const char * fileNameDescriptors) const;
374
381 void clear(bool printWarningsIfNotEmpty = true);
382
392 std::vector<VisualWord *> getUnusedWords() const;
393
398 std::vector<int> getUnusedWordIds() const;
399
404 unsigned int getUnusedWordsSize() const {return (int)_unusedWords.size();}
405
412 void removeWords(const std::vector<VisualWord*> & words); // caller must delete the words
413
420
421public:
433 static cv::Mat convertBinTo32F(const cv::Mat & descriptorsIn, bool byteToFloat = true);
434
447 static cv::Mat convert32FToBin(const cv::Mat & descriptorsIn, bool byteToFloat = true);
448
449protected:
455
456protected:
461 std::map<int, VisualWord *> _visualWords; //<id,VisualWord*>
462
468
469private:
473 bool _incrementalDictionary;
474
478 bool _incrementalFlann;
479
483 float _rebalancingFactor;
484
488 bool _byteToFloat;
489
496 float _nndrRatio;
497
501 std::string _dictionaryPath; // a pre-computed dictionary (.txt or .db)
502
506 std::string _newDictionaryPath; // a pre-computed dictionary (.txt or .db)
507
511 bool _newWordsComparedTogether;
512
516 bool _serializeWithChecksum;
517
521 int _lastWordId;
522
526 bool useDistanceL1_;
527
531 FlannIndex * _flannIndex;
532
536 cv::Mat _dataTree;
537
541 bool _modified;
542
546 NNStrategy _strategy;
547
551 std::map<int ,int> _mapIndexId;
552
556 std::map<int ,int> _mapIdIndex;
557
562 std::map<int, VisualWord*> _unusedWords; //<id,VisualWord*>
566 std::set<int> _notIndexedWords;
567
571 std::set<int> _removedIndexedWords;
572};
573
574} // namespace rtabmap
Manages a dictionary of visual words for visual place recognition and loop closure detection.
int getNextId()
Get the next available visual word ID.
bool deserializeIndex(const unsigned char *data, size_t size)
Deserialize the search index from raw bytes.
unsigned int getUnusedWordsSize() const
Get the count of unused visual words.
void exportDictionary(const char *fileNameReferences, const char *fileNameDescriptors) const
Export the dictionary to files.
std::vector< unsigned char > serializeIndex() const
Serialize the search index to a byte vector.
unsigned int getNotIndexedWordsCount() const
Get the count of words not yet indexed in the search tree.
virtual std::list< int > addNewWords(const cv::Mat &descriptors, int signatureId)
Add new visual words from descriptors.
static const int ID_START
Starting ID for visual words (typically 1)
void deleteUnusedWords()
Delete all unused visual words.
bool deserializeIndex(const std::vector< unsigned char > &data)
Deserialize the search index from a byte vector.
float getNndrRatio() const
Get the Nearest Neighbor Distance Ratio (NNDR) threshold.
int _totalActiveReferences
Total count of active word-to-signature references.
void clear(bool printWarningsIfNotEmpty=true)
Clear all visual words and reset the dictionary.
const VisualWord * getWord(int id) const
Get a visual word by ID.
void setFixedDictionary(const std::string &dictionaryPath)
Set the dictionary to fixed mode and load from file.
unsigned int getIndexMemoryUsed() const
Get the memory used by the search index.
std::vector< int > getUnusedWordIds() const
Get IDs of all unused visual words.
int getLastIndexedWordId() const
Get the ID of the last indexed word.
unsigned int getIndexedWordsCount() const
Get the count of words currently indexed in the search tree.
const std::map< int, VisualWord * > & getVisualWords() const
Get all visual words.
bool isIncremental() const
Check if the dictionary is in incremental mode.
static std::string nnStrategyName(NNStrategy strategy)
Get the name of a nearest neighbor strategy.
static cv::Mat convertBinTo32F(const cv::Mat &descriptorsIn, bool byteToFloat=true)
Convert binary descriptors to 32-bit float format.
void removeWords(const std::vector< VisualWord * > &words)
Remove words from the dictionary.
static const int ID_INVALID
Invalid visual word ID (typically 0)
virtual void update()
Update the search index with newly added words.
virtual void parseParameters(const ParametersMap &parameters)
Parse and apply parameters from a parameters map.
NNStrategy
Nearest neighbor search strategies for descriptor matching.
@ kNNBruteForce
Brute force CPU search.
@ kNNBruteForceGPU
Brute force GPU-accelerated search (requires CUDA)
@ kNNFlannLSH
FLANN Locality-Sensitive Hashing (ideal for binary descriptors)
@ kNNFlannKdTree
FLANN kd-tree index (fast for high-dimensional descriptors)
@ kNNFlannNaive
FLANN naive search (exhaustive)
int getTotalActiveReferences() const
Get the total number of active word-to-signature references.
bool isIncrementalFlann() const
Check if FLANN index is updated incrementally.
bool isModified() const
Check if the dictionary has been modified since last save.
std::vector< int > findNN(const cv::Mat &descriptors) const
Find nearest neighbor visual word IDs for descriptors.
void setIncrementalDictionary()
Set the dictionary to incremental mode.
NNStrategy getNNStrategy() const
Get the current nearest neighbor search strategy.
std::vector< VisualWord * > getUnusedWords() const
Get all unused visual words.
VisualWord * getUnusedWord(int id) const
Get an unused visual word by ID.
std::vector< int > findNN(const std::list< VisualWord * > &vws) const
Find nearest neighbor visual word IDs for a list of visual words.
void removeAllWordRef(int wordId, int signatureId)
Remove all references from a visual word to a signature.
unsigned long getMemoryUsed() const
Get the total memory used by the dictionary.
static cv::Mat convert32FToBin(const cv::Mat &descriptorsIn, bool byteToFloat=true)
Convert 32-bit float descriptors to binary format.
virtual void addWord(VisualWord *vw)
Add an existing visual word to the dictionary.
VWDictionary(const ParametersMap &parameters=ParametersMap())
Constructor.
std::map< int, VisualWord * > _visualWords
Map of visual word ID to VisualWord pointer.
bool addWordRef(int wordId, int signatureId)
Add a reference from a visual word to a signature.
void setLastWordId(int id)
Set the last word ID (used when loading from database)
bool setNNStrategy(NNStrategy strategy)
Set the nearest neighbor search strategy.
virtual ~VWDictionary()
Destructor.
Represents a visual word (feature descriptor) used in the bag-of-words (BoW) model.
Definition VisualWord.h:50
std::map< std::string, std::string > ParametersMap
Parameter keys mapped to their values, as used by every configurable class (see Parameters).
Definition Parameters.h:44