Changed LinearIndex to Bruteforce for nn with not yet indexed words

This commit is contained in:
matlabbe
2015-09-09 17:15:42 -04:00
parent 1b13857693
commit 9a037b33ff

View File

@@ -947,32 +947,21 @@ std::list<int> 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) // Check if this descriptor matches with a word from the last signature (a word not already added to the tree)
if(_newWordsComparedTogether && newWords.rows) if(_newWordsComparedTogether && newWords.rows)
{ {
FlannIndex linearSeach; std::vector<std::vector<cv::DMatch> > matchesNewWords;
linearSeach.build(newWords, flann::LinearIndexParams()); cv::BFMatcher matcher(type==CV_8U?cv::NORM_HAMMING:cv::NORM_L2SQR);
cv::Mat resultsLinear; matcher.knnMatch(descriptors.row(i), newWords, matchesNewWords, newWords.rows>1?2:1);
cv::Mat distsLinear; UASSERT(matchesNewWords.size() == 1);
linearSeach.knnSearch(descriptors.row(i), resultsLinear, distsLinear, newWords.rows>1?2:1); for(unsigned int j=0; j<matchesNewWords.at(0).size(); ++j)
// In case of binary descriptors
if(distsLinear.type() == CV_32S)
{ {
cv::Mat temp; float d = matchesNewWords.at(0).at(j).distance;
distsLinear.convertTo(temp, CV_32F); int id = newWordsId[matchesNewWords.at(0).at(j).trainIdx];
distsLinear = temp; if(d >= 0.0f && id > 0)
}
if(resultsLinear.cols)
{
for(int j=0; j<resultsLinear.cols; ++j)
{ {
float d = distsLinear.at<float>(0,j); fullResults.insert(std::pair<float, int>(d, id));
if(d >= 0.0f && resultsLinear.at<int>(0,j) >= 0) }
{ else
std::multimap<float, int>::iterator iter = fullResults.insert(std::pair<float, int>(d, newWordsId[resultsLinear.at<int>(0,j)])); {
UASSERT(iter->second > 0); break;
}
else
{
break;
}
} }
} }
} }
@@ -1153,9 +1142,8 @@ std::vector<int> VWDictionary::findNN(const std::list<VisualWord *> & vws) const
} }
ULOGGER_DEBUG("Search dictionary time = %fs", timer.ticks()); ULOGGER_DEBUG("Search dictionary time = %fs", timer.ticks());
cv::Mat resultsNotIndexed;
cv::Mat distsNotIndexed;
std::map<int, int> mapIndexIdNotIndexed; std::map<int, int> mapIndexIdNotIndexed;
std::vector<std::vector<cv::DMatch> > matchesNotIndexed;
if(_notIndexedWords.size()) if(_notIndexedWords.size())
{ {
cv::Mat dataNotIndexed = cv::Mat::zeros(_notIndexedWords.size(), dim, type); cv::Mat dataNotIndexed = cv::Mat::zeros(_notIndexedWords.size(), dim, type);
@@ -1171,16 +1159,8 @@ std::vector<int> VWDictionary::findNN(const std::list<VisualWord *> & vws) const
// Find nearest neighbor // Find nearest neighbor
ULOGGER_DEBUG("Searching in words not indexed..."); ULOGGER_DEBUG("Searching in words not indexed...");
FlannIndex linearSeach; cv::BFMatcher matcher(type==CV_8U?cv::NORM_HAMMING:cv::NORM_L2SQR);
linearSeach.build(dataNotIndexed, flann::LinearIndexParams()); matcher.knnMatch(query, dataNotIndexed, matchesNotIndexed, dataNotIndexed.rows>1?2:1);
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;
}
} }
ULOGGER_DEBUG("Search not yet indexed words time = %fs", timer.ticks()); ULOGGER_DEBUG("Search not yet indexed words time = %fs", timer.ticks());
@@ -1213,13 +1193,17 @@ std::vector<int> VWDictionary::findNN(const std::list<VisualWord *> & vws) const
} }
// not indexed.. // not indexed..
for(int j=0; j<distsNotIndexed.cols; ++j) for(unsigned int j=0; j<matchesNotIndexed.at(i).size(); ++j)
{ {
float d = distsNotIndexed.at<float>(i,j); float d = matchesNotIndexed.at(i).at(j).distance;
if(d >= 0.0f && resultsNotIndexed.at<int>(i,j) > 0) int id = uValue(mapIndexIdNotIndexed, matchesNotIndexed.at(i).at(j).trainIdx);
if(d >= 0.0f && id > 0)
{ {
std::multimap<float, int>::iterator iter = fullResults.insert(std::pair<float, int>(d, uValue(mapIndexIdNotIndexed, resultsNotIndexed.at<int>(i,j)))); fullResults.insert(std::pair<float, int>(d, id));
UASSERT(iter->second > 0); }
else
{
break;
} }
} }