RTAB-Map 0.24.5
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/imgcodecs.hpp>
33#include <opencv2/videoio.hpp>
34#include <opencv2/core/core.hpp>
35#if CV_MAJOR_VERSION < 5
36#include <opencv2/features2d/features2d.hpp>
37#else
38#include <opencv2/features.hpp>
39#endif
40#include <list>
41#include <set>
42#include "rtabmap/core/Parameters.h"
43
44namespace rtabmap
45{
46
47class DBDriver;
48class VisualWord;
49class FlannIndex;
50
66class RTABMAP_CORE_EXPORT VWDictionary
67{
68public:
88
92 static const int ID_START;
93
97 static const int ID_INVALID;
98
104 static std::string nnStrategyName(NNStrategy strategy)
105 {
106 switch(strategy) {
107 case kNNFlannNaive:
108 return "FLANN NAIVE";
109 case kNNFlannKdTree:
110 return "FLANN KD-TREE";
111 case kNNFlannLSH:
112 return "FLANN LSH";
113 case kNNBruteForce:
114 return "BRUTE FORCE";
115 case kNNBruteForceGPU:
116 return "BRUTE FORCE GPU";
117 case kNNNanoFlannKdTree:
118 return "NANOFLANN KD-TREE";
119 case kNNFlannKdTreeSingle:
120 return "FLANN KD-TREE SINGLE";
121 default:
122 return "Unknown";
123 }
124 }
125
126
127
128public:
133 VWDictionary(const ParametersMap & parameters = ParametersMap());
134
140 virtual ~VWDictionary();
141
146 virtual void parseParameters(const ParametersMap & parameters);
147
154 virtual void update();
155
170 virtual std::list<int> addNewWords(
171 const cv::Mat & descriptors,
172 int signatureId);
173
180 virtual void addWord(VisualWord * vw);
181
191 std::vector<int> findNN(const std::list<VisualWord *> & vws) const;
192
202 std::vector<int> findNN(const cv::Mat & descriptors) const;
203
213 bool addWordRef(int wordId, int signatureId);
214
223 void removeAllWordRef(int wordId, int signatureId);
224
230 const VisualWord * getWord(int id) const;
231
240 VisualWord * getUnusedWord(int id) const;
241
246 void setLastWordId(int id) {_lastWordId = id;}
247
252 int getLastWordId() const {return _lastWordId;}
253
258 const std::map<int, VisualWord *> & getVisualWords() const {return _visualWords;}
259
271 float getNndrRatio() const {return _nndrRatio;}
272
277 unsigned int getNotIndexedWordsCount() const {return (int)_notIndexedWords.size();}
278
284
289 int getTotalActiveReferences() const {return _totalActiveReferences;}
290
295 unsigned int getIndexedWordsCount() const;
296
301 unsigned int getIndexMemoryUsed() const; // KB
302
307 unsigned long getMemoryUsed() const; //Bytes
308
316 bool setNNStrategy(NNStrategy strategy);
317
322 NNStrategy getNNStrategy() const {return _strategy;}
323
328 bool isIncremental() const {return _incrementalDictionary;}
329
334 bool isIncrementalFlann() const {return _incrementalFlann;}
335
343
352 void setFixedDictionary(const std::string & dictionaryPath);
353
358 bool isModified() const;
359 // Re-index all the words from scratch. The index then contains the words in
360 // the same order than the one built by update() on a dictionary freshly
361 // loaded from a database, which is required to serialize it (the serialized
362 // index is matched against the words in that order when deserialized).
363 void rebuildIndex();
364
369 std::vector<unsigned char> serializeIndex() const;
370
376 bool deserializeIndex(const std::vector<unsigned char> & data);
377
384 bool deserializeIndex(const unsigned char * data, size_t size);
385
393 void exportDictionary(const char * fileNameReferences, const char * fileNameDescriptors) const;
394
401 void clear(bool printWarningsIfNotEmpty = true);
402
412 std::vector<VisualWord *> getUnusedWords() const;
413
418 std::vector<int> getUnusedWordIds() const;
419
424 unsigned int getUnusedWordsSize() const {return (int)_unusedWords.size();}
425
432 void removeWords(const std::vector<VisualWord*> & words); // caller must delete the words
433
440
441public:
453 static cv::Mat convertBinTo32F(const cv::Mat & descriptorsIn, bool byteToFloat = true);
454
467 static cv::Mat convert32FToBin(const cv::Mat & descriptorsIn, bool byteToFloat = true);
468
469protected:
475
476protected:
481 std::map<int, VisualWord *> _visualWords; //<id,VisualWord*>
482
488
489private:
493 bool _incrementalDictionary;
494
498 bool _incrementalFlann;
499
503 float _rebalancingFactor;
504
508 int _flannThreads;
509
513 bool _byteToFloat;
514
521 float _nndrRatio;
522
526 std::string _dictionaryPath; // a pre-computed dictionary (.txt or .db)
527
531 std::string _newDictionaryPath; // a pre-computed dictionary (.txt or .db)
532
536 bool _newWordsComparedTogether;
537
541 bool _serializeWithChecksum;
542
546 int _lastWordId;
547
551 bool useDistanceL1_;
552
556 FlannIndex * _flannIndex;
557
561 cv::Mat _dataTree;
562
566 bool _modified;
567
571 NNStrategy _strategy;
572
576 std::map<int ,int> _mapIndexId;
577
581 std::map<int ,int> _mapIdIndex;
582
587 std::map<int, VisualWord*> _unusedWords; //<id,VisualWord*>
591 std::set<int> _notIndexedWords;
592
596 std::set<int> _removedIndexedWords;
597};
598
599} // namespace rtabmap
Nearest neighbor index over a set of features.
Definition FlannIndex.h:56
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.
@ kNNNanoFlannKdTree
nanoflann kd-tree index (float descriptors only, incremental)
@ kNNBruteForceGPU
Brute force GPU-accelerated search (requires CUDA)
@ kNNFlannLSH
FLANN Locality-Sensitive Hashing (ideal for binary descriptors)
@ kNNFlannKdTreeSingle
FLANN single exact kd-tree index (rebuilt whenever a word is added, for an index built once)
@ 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.
int getLastWordId() const
Get the last word ID: new words are created with the IDs following it.
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