refactored vocabulary matrix copy

git-svn-id: http://find-object.googlecode.com/svn/trunk/find_object@270 620bd6b2-0a58-f614-fd9a-1bd335dccda9
This commit is contained in:
matlabbe 2014-05-13 14:41:36 +00:00
parent 20a32932bf
commit 166e3b3513
3 changed files with 29 additions and 53 deletions

View File

@ -801,15 +801,18 @@ void MainWindow::updateData()
int row = 0; int row = 0;
for(int i=0; i<objects_.size(); ++i) for(int i=0; i<objects_.size(); ++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) 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);
}
} }
} }

View File

@ -56,13 +56,13 @@ QMultiMap<int, int> Vocabulary::addWords(const cv::Mat & descriptors, int object
globalSearch = true; globalSearch = true;
} }
QVector<int> newWordIds = notIndexedWordIds_; // index global notIndexedWordIds_.reserve(notIndexedWordIds_.size() + descriptors.rows);
cv::Mat newWords = notIndexedDescriptors_; 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)
{ {
QMap<float, int> fullResults; // nearest descriptors sorted by distance QMap<float, int> fullResults; // nearest descriptors sorted by distance
if(newWords.rows) if(notIndexedDescriptors_.rows)
{ {
Q_ASSERT(newWords.type() == descriptors.type() && newWords.cols == descriptors.cols); Q_ASSERT(newWords.type() == descriptors.type() && newWords.cols == descriptors.cols);
@ -84,12 +84,12 @@ QMultiMap<int, int> Vocabulary::addWords(const cv::Mat & descriptors, int object
} }
cv::batchDistance( descriptors.row(i), cv::batchDistance( descriptors.row(i),
newWords, notIndexedDescriptors_,
tmpDists, tmpDists,
CV_32S, CV_32S,
tmpResults, tmpResults,
normType, normType,
newWords.rows>=k?k:1, notIndexedDescriptors_.rows>=k?k:1,
cv::Mat(), cv::Mat(),
0, 0,
false); false);
@ -97,8 +97,8 @@ QMultiMap<int, int> Vocabulary::addWords(const cv::Mat & descriptors, int object
else else
{ {
cv::flann::Index tmpIndex; cv::flann::Index tmpIndex;
tmpIndex.build(newWords, cv::flann::LinearIndexParams(), Settings::getFlannDistanceType()); tmpIndex.build(notIndexedDescriptors_, cv::flann::LinearIndexParams(), Settings::getFlannDistanceType());
tmpIndex.knnSearch(descriptors.row(i), tmpResults, tmpDists, newWords.rows>1?k:1, Settings::getFlannSearchParams()); tmpIndex.knnSearch(descriptors.row(i), tmpResults, tmpDists, notIndexedDescriptors_.rows>1?k:1, Settings::getFlannSearchParams());
} }
if( tmpDists.type() == CV_32S ) if( tmpDists.type() == CV_32S )
@ -108,12 +108,12 @@ QMultiMap<int, int> Vocabulary::addWords(const cv::Mat & descriptors, int object
tmpDists = temp; tmpDists = temp;
} }
for(int j = 0; j < (newWords.rows>=k?k:1); ++j) for(int j = 0; j < tmpResults.cols; ++j)
{ {
if(tmpResults.at<int>(0,j) >= 0) if(tmpResults.at<int>(0,j) >= 0)
{ {
//printf("local i=%d, j=%d, tmpDist=%f tmpResult=%d\n", i ,j, tmpDists.at<float>(0,j), tmpResults.at<int>(0,j)); //printf("local i=%d, j=%d, tmpDist=%f tmpResult=%d\n", i ,j, tmpDists.at<float>(0,j), tmpResults.at<int>(0,j));
fullResults.insert(tmpDists.at<float>(0,j), newWordIds.at(tmpResults.at<int>(0,j))); fullResults.insert(tmpDists.at<float>(0,j), notIndexedWordIds_.at(tmpResults.at<int>(0,j)));
} }
} }
} }
@ -146,27 +146,13 @@ QMultiMap<int, int> Vocabulary::addWords(const cv::Mat & descriptors, int object
} }
else else
{ {
cv::Mat tmp(newWords.rows+1, descriptors.cols, descriptors.type()); //concatenate new words
if(newWords.rows) notIndexedWordIds_.push_back(indexedDescriptors_.rows + notIndexedDescriptors_.rows);
{ notIndexedDescriptors_.push_back(descriptors.row(i));
cv::Mat dest(tmp, cv::Range(0, newWords.rows)); words.insert(notIndexedWordIds_.back(), i);
newWords.copyTo(dest); wordToObjects_.insert(notIndexedWordIds_.back(), objectIndex);
}
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
if(newWords.rows)
{
notIndexedWordIds_ = newWordIds;
notIndexedDescriptors_ = newWords;
}
} }
else else
{ {
@ -178,15 +164,7 @@ QMultiMap<int, int> Vocabulary::addWords(const cv::Mat & descriptors, int object
} }
//just concatenate descriptors //just concatenate descriptors
cv::Mat tmp(notIndexedDescriptors_.rows+descriptors.rows, descriptors.cols, descriptors.type()); notIndexedDescriptors_.push_back(descriptors);
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;
} }
return words; return words;
@ -200,12 +178,7 @@ void Vocabulary::update()
indexedDescriptors_.type() == notIndexedDescriptors_.type() ); indexedDescriptors_.type() == notIndexedDescriptors_.type() );
//concatenate descriptors //concatenate descriptors
cv::Mat tmp(indexedDescriptors_.rows+notIndexedDescriptors_.rows, notIndexedDescriptors_.cols, notIndexedDescriptors_.type()); indexedDescriptors_.push_back(notIndexedDescriptors_);
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;
notIndexedDescriptors_ = cv::Mat(); notIndexedDescriptors_ = cv::Mat();
notIndexedWordIds_.clear(); notIndexedWordIds_.clear();

View File

@ -7,7 +7,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>826</width> <width>826</width>
<height>572</height> <height>689</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -346,7 +346,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>198</width> <width>198</width>
<height>442</height> <height>559</height>
</rect> </rect>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_objects"> <layout class="QVBoxLayout" name="verticalLayout_objects">
@ -529,7 +529,7 @@
<item row="5" column="0"> <item row="5" column="0">
<widget class="QLabel" name="label_11"> <widget class="QLabel" name="label_11">
<property name="text"> <property name="text">
<string>Detect outliers and GUI</string> <string>Homograhies</string>
</property> </property>
</widget> </widget>
</item> </item>