RTAB-Map 0.23.11
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:
87
91 static const int ID_START;
92
96 static const int ID_INVALID;
97
103 static std::string nnStrategyName(NNStrategy strategy)
104 {
105 switch(strategy) {
106 case kNNFlannNaive:
107 return "FLANN NAIVE";
108 case kNNFlannKdTree:
109 return "FLANN KD-TREE";
110 case kNNFlannLSH:
111 return "FLANN LSH";
112 case kNNBruteForce:
113 return "BRUTE FORCE";
114 case kNNBruteForceGPU:
115 return "BRUTE FORCE GPU";
116 case kNNNanoFlannKdTree:
117 return "NANOFLANN KD-TREE";
118 case kNNFlannKdTreeSingle:
119 return "FLANN KD-TREE SINGLE";
120 default:
121 return "Unknown";
122 }
123 }
124
125
126
127public:
132 VWDictionary(const ParametersMap & parameters = ParametersMap());
133
139 virtual ~VWDictionary();
140
145 virtual void parseParameters(const ParametersMap & parameters);
146
153 virtual void update();
154
169 virtual std::list<int> addNewWords(
170 const cv::Mat & descriptors,
171 int signatureId);
172
179 virtual void addWord(VisualWord * vw);
180
190 std::vector<int> findNN(const std::list<VisualWord *> & vws) const;
191
201 std::vector<int> findNN(const cv::Mat & descriptors) const;
202
212 bool addWordRef(int wordId, int signatureId);
213
222 void removeAllWordRef(int wordId, int signatureId);
223
229 const VisualWord * getWord(int id) const;
230
239 VisualWord * getUnusedWord(int id) const;
240
245 void setLastWordId(int id) {_lastWordId = id;}
246
251 const std::map<int, VisualWord *> & getVisualWords() const {return _visualWords;}
252
264 float getNndrRatio() const {return _nndrRatio;}
265
270 unsigned int getNotIndexedWordsCount() const {return (int)_notIndexedWords.size();}
271
277
282 int getTotalActiveReferences() const {return _totalActiveReferences;}
283
288 unsigned int getIndexedWordsCount() const;
289
294 unsigned int getIndexMemoryUsed() const; // KB
295
300 unsigned long getMemoryUsed() const; //Bytes
301
309 bool setNNStrategy(NNStrategy strategy);
310
315 NNStrategy getNNStrategy() const {return _strategy;}
316
321 bool isIncremental() const {return _incrementalDictionary;}
322
327 bool isIncrementalFlann() const {return _incrementalFlann;}
328
336
345 void setFixedDictionary(const std::string & dictionaryPath);
346
351 bool isModified() const;
352 // Re-index all the words from scratch. The index then contains the words in
353 // the same order than the one built by update() on a dictionary freshly
354 // loaded from a database, which is required to serialize it (the serialized
355 // index is matched against the words in that order when deserialized).
356 void rebuildIndex();
357
362 std::vector<unsigned char> serializeIndex() const;
363
369 bool deserializeIndex(const std::vector<unsigned char> & data);
370
377 bool deserializeIndex(const unsigned char * data, size_t size);
378
386 void exportDictionary(const char * fileNameReferences, const char * fileNameDescriptors) const;
387
394 void clear(bool printWarningsIfNotEmpty = true);
395
405 std::vector<VisualWord *> getUnusedWords() const;
406
411 std::vector<int> getUnusedWordIds() const;
412
417 unsigned int getUnusedWordsSize() const {return (int)_unusedWords.size();}
418
425 void removeWords(const std::vector<VisualWord*> & words); // caller must delete the words
426
433
434public:
446 static cv::Mat convertBinTo32F(const cv::Mat & descriptorsIn, bool byteToFloat = true);
447
460 static cv::Mat convert32FToBin(const cv::Mat & descriptorsIn, bool byteToFloat = true);
461
462protected:
468
469protected:
474 std::map<int, VisualWord *> _visualWords; //<id,VisualWord*>
475
481
482private:
486 bool _incrementalDictionary;
487
491 bool _incrementalFlann;
492
496 float _rebalancingFactor;
497
501 bool _byteToFloat;
502
509 float _nndrRatio;
510
514 std::string _dictionaryPath; // a pre-computed dictionary (.txt or .db)
515
519 std::string _newDictionaryPath; // a pre-computed dictionary (.txt or .db)
520
524 bool _newWordsComparedTogether;
525
529 bool _serializeWithChecksum;
530
534 int _lastWordId;
535
539 bool useDistanceL1_;
540
544 FlannIndex * _flannIndex;
545
549 cv::Mat _dataTree;
550
554 bool _modified;
555
559 NNStrategy _strategy;
560
564 std::map<int ,int> _mapIndexId;
565
569 std::map<int ,int> _mapIdIndex;
570
575 std::map<int, VisualWord*> _unusedWords; //<id,VisualWord*>
579 std::set<int> _notIndexedWords;
580
584 std::set<int> _removedIndexedWords;
585};
586
587} // 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.
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