From 166e3b3513671a1d6ef30b59a516940d0d6bb858 Mon Sep 17 00:00:00 2001 From: matlabbe Date: Tue, 13 May 2014 14:41:36 +0000 Subject: [PATCH] refactored vocabulary matrix copy git-svn-id: http://find-object.googlecode.com/svn/trunk/find_object@270 620bd6b2-0a58-f614-fd9a-1bd335dccda9 --- src/MainWindow.cpp | 17 +++++++------ src/Vocabulary.cpp | 59 ++++++++++++-------------------------------- src/ui/mainWindow.ui | 6 ++--- 3 files changed, 29 insertions(+), 53 deletions(-) diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 727b51c9..0dd2e4c3 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -801,15 +801,18 @@ void MainWindow::updateData() int row = 0; for(int i=0; idescriptors().rows)); - objects_.at(i)->descriptors().copyTo(dest); - row += objects_.at(i)->descriptors().rows; - // dataRange contains the upper_bound for each - // object (the last descriptors position in the - // global object descriptors matrix) if(objects_.at(i)->descriptors().rows) { - dataRange_.insert(row-1, i); + cv::Mat dest(objectsDescriptors_[0], cv::Range(row, row+objects_.at(i)->descriptors().rows)); + objects_.at(i)->descriptors().copyTo(dest); + row += objects_.at(i)->descriptors().rows; + // dataRange contains the upper_bound for each + // object (the last descriptors position in the + // global object descriptors matrix) + if(objects_.at(i)->descriptors().rows) + { + dataRange_.insert(row-1, i); + } } } diff --git a/src/Vocabulary.cpp b/src/Vocabulary.cpp index 82ce4838..e598e779 100644 --- a/src/Vocabulary.cpp +++ b/src/Vocabulary.cpp @@ -56,13 +56,13 @@ QMultiMap Vocabulary::addWords(const cv::Mat & descriptors, int object globalSearch = true; } - QVector newWordIds = notIndexedWordIds_; // index global - cv::Mat newWords = notIndexedDescriptors_; + notIndexedWordIds_.reserve(notIndexedWordIds_.size() + descriptors.rows); + notIndexedDescriptors_.reserve(notIndexedDescriptors_.rows + descriptors.rows); int matches = 0; for(int i = 0; i < descriptors.rows; ++i) { QMap fullResults; // nearest descriptors sorted by distance - if(newWords.rows) + if(notIndexedDescriptors_.rows) { Q_ASSERT(newWords.type() == descriptors.type() && newWords.cols == descriptors.cols); @@ -84,12 +84,12 @@ QMultiMap Vocabulary::addWords(const cv::Mat & descriptors, int object } cv::batchDistance( descriptors.row(i), - newWords, + notIndexedDescriptors_, tmpDists, CV_32S, tmpResults, normType, - newWords.rows>=k?k:1, + notIndexedDescriptors_.rows>=k?k:1, cv::Mat(), 0, false); @@ -97,8 +97,8 @@ QMultiMap Vocabulary::addWords(const cv::Mat & descriptors, int object else { cv::flann::Index tmpIndex; - tmpIndex.build(newWords, cv::flann::LinearIndexParams(), Settings::getFlannDistanceType()); - tmpIndex.knnSearch(descriptors.row(i), tmpResults, tmpDists, newWords.rows>1?k:1, Settings::getFlannSearchParams()); + tmpIndex.build(notIndexedDescriptors_, cv::flann::LinearIndexParams(), Settings::getFlannDistanceType()); + tmpIndex.knnSearch(descriptors.row(i), tmpResults, tmpDists, notIndexedDescriptors_.rows>1?k:1, Settings::getFlannSearchParams()); } if( tmpDists.type() == CV_32S ) @@ -108,12 +108,12 @@ QMultiMap Vocabulary::addWords(const cv::Mat & descriptors, int object tmpDists = temp; } - for(int j = 0; j < (newWords.rows>=k?k:1); ++j) + for(int j = 0; j < tmpResults.cols; ++j) { if(tmpResults.at(0,j) >= 0) { //printf("local i=%d, j=%d, tmpDist=%f tmpResult=%d\n", i ,j, tmpDists.at(0,j), tmpResults.at(0,j)); - fullResults.insert(tmpDists.at(0,j), newWordIds.at(tmpResults.at(0,j))); + fullResults.insert(tmpDists.at(0,j), notIndexedWordIds_.at(tmpResults.at(0,j))); } } } @@ -146,27 +146,13 @@ QMultiMap Vocabulary::addWords(const cv::Mat & descriptors, int object } else { - cv::Mat tmp(newWords.rows+1, descriptors.cols, descriptors.type()); - if(newWords.rows) - { - cv::Mat dest(tmp, cv::Range(0, newWords.rows)); - newWords.copyTo(dest); - } - cv::Mat dest(tmp, cv::Range(newWords.rows, newWords.rows+1)); - descriptors.row(i).copyTo(dest); - newWordIds.push_back(indexedDescriptors_.rows + newWords.rows); - newWords = tmp; - words.insert(newWordIds.back(), i); - wordToObjects_.insert(newWordIds.back(), objectIndex); + //concatenate new words + notIndexedWordIds_.push_back(indexedDescriptors_.rows + notIndexedDescriptors_.rows); + notIndexedDescriptors_.push_back(descriptors.row(i)); + words.insert(notIndexedWordIds_.back(), i); + wordToObjects_.insert(notIndexedWordIds_.back(), objectIndex); } } - - //concatenate new words - if(newWords.rows) - { - notIndexedWordIds_ = newWordIds; - notIndexedDescriptors_ = newWords; - } } else { @@ -178,15 +164,7 @@ QMultiMap Vocabulary::addWords(const cv::Mat & descriptors, int object } //just concatenate descriptors - cv::Mat tmp(notIndexedDescriptors_.rows+descriptors.rows, descriptors.cols, descriptors.type()); - if(notIndexedDescriptors_.rows) - { - cv::Mat dest(tmp, cv::Range(0, notIndexedDescriptors_.rows)); - notIndexedDescriptors_.copyTo(dest); - } - cv::Mat dest(tmp, cv::Range(notIndexedDescriptors_.rows, notIndexedDescriptors_.rows+descriptors.rows)); - descriptors.copyTo(dest); - notIndexedDescriptors_ = tmp; + notIndexedDescriptors_.push_back(descriptors); } return words; @@ -200,12 +178,7 @@ void Vocabulary::update() indexedDescriptors_.type() == notIndexedDescriptors_.type() ); //concatenate descriptors - cv::Mat tmp(indexedDescriptors_.rows+notIndexedDescriptors_.rows, notIndexedDescriptors_.cols, notIndexedDescriptors_.type()); - cv::Mat dest(tmp, cv::Range(0, indexedDescriptors_.rows)); - indexedDescriptors_.copyTo(dest); - dest = cv::Mat(tmp, cv::Range(indexedDescriptors_.rows, indexedDescriptors_.rows+notIndexedDescriptors_.rows)); - notIndexedDescriptors_.copyTo(dest); - indexedDescriptors_ = tmp; + indexedDescriptors_.push_back(notIndexedDescriptors_); notIndexedDescriptors_ = cv::Mat(); notIndexedWordIds_.clear(); diff --git a/src/ui/mainWindow.ui b/src/ui/mainWindow.ui index 98a1b674..2af0934a 100644 --- a/src/ui/mainWindow.ui +++ b/src/ui/mainWindow.ui @@ -7,7 +7,7 @@ 0 0 826 - 572 + 689 @@ -346,7 +346,7 @@ 0 0 198 - 442 + 559 @@ -529,7 +529,7 @@ - Detect outliers and GUI + Homograhies