From 9a037b33ff7c482b44fc08887905a6c7d1d98165 Mon Sep 17 00:00:00 2001 From: matlabbe Date: Wed, 9 Sep 2015 17:15:42 -0400 Subject: [PATCH] Changed LinearIndex to Bruteforce for nn with not yet indexed words --- corelib/src/VWDictionary.cpp | 66 ++++++++++++++---------------------- 1 file changed, 25 insertions(+), 41 deletions(-) diff --git a/corelib/src/VWDictionary.cpp b/corelib/src/VWDictionary.cpp index 5eee86aa..0859155a 100644 --- a/corelib/src/VWDictionary.cpp +++ b/corelib/src/VWDictionary.cpp @@ -947,32 +947,21 @@ std::list VWDictionary::addNewWords(const cv::Mat & descriptors, // Check if this descriptor matches with a word from the last signature (a word not already added to the tree) if(_newWordsComparedTogether && newWords.rows) { - FlannIndex linearSeach; - linearSeach.build(newWords, flann::LinearIndexParams()); - cv::Mat resultsLinear; - cv::Mat distsLinear; - linearSeach.knnSearch(descriptors.row(i), resultsLinear, distsLinear, newWords.rows>1?2:1); - // In case of binary descriptors - if(distsLinear.type() == CV_32S) + std::vector > matchesNewWords; + cv::BFMatcher matcher(type==CV_8U?cv::NORM_HAMMING:cv::NORM_L2SQR); + matcher.knnMatch(descriptors.row(i), newWords, matchesNewWords, newWords.rows>1?2:1); + UASSERT(matchesNewWords.size() == 1); + for(unsigned int j=0; j= 0.0f && id > 0) { - float d = distsLinear.at(0,j); - if(d >= 0.0f && resultsLinear.at(0,j) >= 0) - { - std::multimap::iterator iter = fullResults.insert(std::pair(d, newWordsId[resultsLinear.at(0,j)])); - UASSERT(iter->second > 0); - } - else - { - break; - } + fullResults.insert(std::pair(d, id)); + } + else + { + break; } } } @@ -1153,9 +1142,8 @@ std::vector VWDictionary::findNN(const std::list & vws) const } ULOGGER_DEBUG("Search dictionary time = %fs", timer.ticks()); - cv::Mat resultsNotIndexed; - cv::Mat distsNotIndexed; std::map mapIndexIdNotIndexed; + std::vector > matchesNotIndexed; if(_notIndexedWords.size()) { cv::Mat dataNotIndexed = cv::Mat::zeros(_notIndexedWords.size(), dim, type); @@ -1171,16 +1159,8 @@ std::vector VWDictionary::findNN(const std::list & vws) const // Find nearest neighbor ULOGGER_DEBUG("Searching in words not indexed..."); - FlannIndex linearSeach; - linearSeach.build(dataNotIndexed, flann::LinearIndexParams()); - linearSeach.knnSearch(query, resultsNotIndexed, distsNotIndexed, _notIndexedWords.size()>1?2:1); - // In case of binary descriptors - if(distsNotIndexed.type() == CV_32S) - { - cv::Mat temp; - distsNotIndexed.convertTo(temp, CV_32F); - distsNotIndexed = temp; - } + cv::BFMatcher matcher(type==CV_8U?cv::NORM_HAMMING:cv::NORM_L2SQR); + matcher.knnMatch(query, dataNotIndexed, matchesNotIndexed, dataNotIndexed.rows>1?2:1); } ULOGGER_DEBUG("Search not yet indexed words time = %fs", timer.ticks()); @@ -1213,13 +1193,17 @@ std::vector VWDictionary::findNN(const std::list & vws) const } // not indexed.. - for(int j=0; j(i,j); - if(d >= 0.0f && resultsNotIndexed.at(i,j) > 0) + float d = matchesNotIndexed.at(i).at(j).distance; + int id = uValue(mapIndexIdNotIndexed, matchesNotIndexed.at(i).at(j).trainIdx); + if(d >= 0.0f && id > 0) { - std::multimap::iterator iter = fullResults.insert(std::pair(d, uValue(mapIndexIdNotIndexed, resultsNotIndexed.at(i,j)))); - UASSERT(iter->second > 0); + fullResults.insert(std::pair(d, id)); + } + else + { + break; } }