First draft of a fixed vocabulary (issue #2)

This commit is contained in:
matlabbe 2015-06-23 18:21:30 -04:00
parent b07239a000
commit b4e97e450d
3 changed files with 56 additions and 21 deletions

View File

@ -801,11 +801,15 @@ void FindObject::updateVocabulary()
sessionModified_ = true; sessionModified_ = true;
QTime time; QTime time;
time.start(); time.start();
bool incremental = Settings::getGeneral_vocabularyIncremental(); bool incremental = Settings::getGeneral_vocabularyIncremental() && !Settings::getGeneral_vocabularyFixed();
if(incremental) if(incremental)
{ {
UINFO("Creating incremental vocabulary..."); UINFO("Creating incremental vocabulary...");
} }
else if(Settings::getGeneral_vocabularyFixed())
{
UINFO("Updating vocabulary correspondences only (vocabulary is fixed)...");
}
else else
{ {
UINFO("Creating vocabulary..."); UINFO("Creating vocabulary...");
@ -816,7 +820,7 @@ void FindObject::updateVocabulary()
int addedWords = 0; int addedWords = 0;
for(int i=0; i<objectsList.size(); ++i) for(int i=0; i<objectsList.size(); ++i)
{ {
QMultiMap<int, int> words = vocabulary_->addWords(objectsList[i]->descriptors(), objectsList.at(i)->id(), incremental); QMultiMap<int, int> words = vocabulary_->addWords(objectsList[i]->descriptors(), objectsList.at(i)->id());
objectsList[i]->setWords(words); objectsList[i]->setWords(words);
addedWords += words.uniqueKeys().size(); addedWords += words.uniqueKeys().size();
bool updated = false; bool updated = false;
@ -834,7 +838,7 @@ void FindObject::updateVocabulary()
localTime.restart(), localTime.restart(),
updated?"updated":""); updated?"updated":"");
} }
if(addedWords) if(addedWords && !Settings::getGeneral_vocabularyFixed())
{ {
vocabulary_->update(); vocabulary_->update();
} }
@ -843,6 +847,10 @@ void FindObject::updateVocabulary()
{ {
UINFO("Creating incremental vocabulary... done! size=%d (%d ms)", vocabulary_->size(), time.elapsed()); UINFO("Creating incremental vocabulary... done! size=%d (%d ms)", vocabulary_->size(), time.elapsed());
} }
else if(Settings::getGeneral_vocabularyFixed())
{
UINFO("Updating vocabulary correspondences only (vocabulary is fixed)... done! size=%d (%d ms)", time.elapsed());
}
else else
{ {
UINFO("Creating vocabulary... done! size=%d (%d ms)", vocabulary_->size(), time.elapsed()); UINFO("Creating vocabulary... done! size=%d (%d ms)", vocabulary_->size(), time.elapsed());
@ -1191,11 +1199,8 @@ bool FindObject::detect(const cv::Mat & image, find_object::DetectionInfo & info
vocabulary_->clear(); vocabulary_->clear();
// CREATE INDEX for the scene // CREATE INDEX for the scene
UDEBUG("CREATE INDEX FOR THE SCENE"); UDEBUG("CREATE INDEX FOR THE SCENE");
words = vocabulary_->addWords(info.sceneDescriptors_, -1, Settings::getGeneral_vocabularyIncremental()); words = vocabulary_->addWords(info.sceneDescriptors_, -1);
if(!Settings::getGeneral_vocabularyIncremental())
{
vocabulary_->update(); vocabulary_->update();
}
info.timeStamps_.insert(DetectionInfo::kTimeIndexing, time.restart()); info.timeStamps_.insert(DetectionInfo::kTimeIndexing, time.restart());
} }

View File

@ -54,10 +54,17 @@ Vocabulary::~Vocabulary()
void Vocabulary::clear() void Vocabulary::clear()
{ {
indexedDescriptors_ = cv::Mat();
notIndexedDescriptors_ = cv::Mat();
wordToObjects_.clear(); wordToObjects_.clear();
notIndexedDescriptors_ = cv::Mat();
notIndexedWordIds_.clear(); notIndexedWordIds_.clear();
if(Settings::getGeneral_vocabularyFixed() && Settings::getGeneral_invertedSearch())
{
// If the dictionary is fixed, don't clear indexed descriptors
return;
}
indexedDescriptors_ = cv::Mat();
} }
void Vocabulary::save(QDataStream & streamPtr) const void Vocabulary::save(QDataStream & streamPtr) const
@ -95,7 +102,7 @@ void Vocabulary::load(QDataStream & streamPtr)
update(); update();
} }
QMultiMap<int, int> Vocabulary::addWords(const cv::Mat & descriptors, int objectId, bool incremental) QMultiMap<int, int> Vocabulary::addWords(const cv::Mat & descriptors, int objectId)
{ {
QMultiMap<int, int> words; QMultiMap<int, int> words;
if (descriptors.empty()) if (descriptors.empty())
@ -103,7 +110,7 @@ QMultiMap<int, int> Vocabulary::addWords(const cv::Mat & descriptors, int object
return words; return words;
} }
if(incremental) if(Settings::getGeneral_vocabularyIncremental() || Settings::getGeneral_vocabularyFixed())
{ {
int k = 2; int k = 2;
cv::Mat results; cv::Mat results;
@ -125,8 +132,11 @@ QMultiMap<int, int> Vocabulary::addWords(const cv::Mat & descriptors, int object
globalSearch = true; globalSearch = true;
} }
if(!Settings::getGeneral_vocabularyFixed())
{
notIndexedWordIds_.reserve(notIndexedWordIds_.size() + descriptors.rows); notIndexedWordIds_.reserve(notIndexedWordIds_.size() + descriptors.rows);
notIndexedDescriptors_.reserve(notIndexedDescriptors_.rows + descriptors.rows); notIndexedDescriptors_.reserve(notIndexedDescriptors_.rows + descriptors.rows);
}
int matches = 0; int matches = 0;
for(int i = 0; i < descriptors.rows; ++i) for(int i = 0; i < descriptors.rows; ++i)
{ {
@ -199,21 +209,37 @@ QMultiMap<int, int> Vocabulary::addWords(const cv::Mat & descriptors, int object
} }
} }
bool match = false; bool matched = false;
// Apply NNDR if(Settings::getNearestNeighbor_3nndrRatioUsed() &&
if(fullResults.size() >= 2 && fullResults.size() >= 2 &&
fullResults.begin().key() <= Settings::getNearestNeighbor_4nndrRatio() * (++fullResults.begin()).key()) fullResults.begin().key() <= Settings::getNearestNeighbor_4nndrRatio() * (++fullResults.begin()).key())
{ {
match = true; matched = true;
}
if((matched || !Settings::getNearestNeighbor_3nndrRatioUsed()) &&
Settings::getNearestNeighbor_5minDistanceUsed())
{
if(fullResults.begin().key() <= Settings::getNearestNeighbor_6minDistance())
{
matched = true;
}
else
{
matched = false;
}
}
if(!matched && !Settings::getNearestNeighbor_3nndrRatioUsed() && !Settings::getNearestNeighbor_5minDistanceUsed())
{
matched = true; // no criterion, match to the nearest descriptor
} }
if(match) if(matched)
{ {
words.insert(fullResults.begin().value(), i); words.insert(fullResults.begin().value(), i);
wordToObjects_.insert(fullResults.begin().value(), objectId); wordToObjects_.insert(fullResults.begin().value(), objectId);
++matches; ++matches;
} }
else else if(!Settings::getGeneral_invertedSearch() || !Settings::getGeneral_vocabularyFixed())
{ {
//concatenate new words //concatenate new words
notIndexedWordIds_.push_back(indexedDescriptors_.rows + notIndexedDescriptors_.rows); notIndexedWordIds_.push_back(indexedDescriptors_.rows + notIndexedDescriptors_.rows);
@ -221,6 +247,10 @@ QMultiMap<int, int> Vocabulary::addWords(const cv::Mat & descriptors, int object
words.insert(notIndexedWordIds_.back(), i); words.insert(notIndexedWordIds_.back(), i);
wordToObjects_.insert(notIndexedWordIds_.back(), objectId); wordToObjects_.insert(notIndexedWordIds_.back(), objectId);
} }
else
{
words.insert(-1, i); // invalid word
}
} }
} }
else else

View File

@ -40,7 +40,7 @@ public:
virtual ~Vocabulary(); virtual ~Vocabulary();
void clear(); void clear();
QMultiMap<int, int> addWords(const cv::Mat & descriptors, int objectId, bool incremental); QMultiMap<int, int> addWords(const cv::Mat & descriptors, int objectId);
void update(); void update();
void search(const cv::Mat & descriptors, cv::Mat & results, cv::Mat & dists, int k); void search(const cv::Mat & descriptors, cv::Mat & results, cv::Mat & dists, int k);
int size() const {return indexedDescriptors_.rows + notIndexedDescriptors_.rows;} int size() const {return indexedDescriptors_.rows + notIndexedDescriptors_.rows;}