2014-05-11 23:57:08 +00:00
|
|
|
|
/*
|
2014-08-06 13:43:29 +00:00
|
|
|
|
Copyright (c) 2011-2014, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
|
|
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
* Neither the name of the Universite de Sherbrooke nor the
|
|
|
|
|
|
names of its contributors may be used to endorse or promote products
|
|
|
|
|
|
derived from this software without specific prior written permission.
|
|
|
|
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
2014-08-11 15:49:53 +00:00
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
|
2014-08-06 13:43:29 +00:00
|
|
|
|
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
|
|
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
*/
|
2014-05-11 23:57:08 +00:00
|
|
|
|
|
2014-07-31 20:11:46 +00:00
|
|
|
|
#include "find_object/Settings.h"
|
|
|
|
|
|
|
2015-01-05 23:11:15 +00:00
|
|
|
|
#include "find_object/utilite/ULogger.h"
|
2014-05-11 23:57:08 +00:00
|
|
|
|
#include "Vocabulary.h"
|
|
|
|
|
|
#include <QtCore/QVector>
|
|
|
|
|
|
#include <stdio.h>
|
2014-10-24 02:46:03 +00:00
|
|
|
|
#include <opencv2/gpu/gpu.hpp>
|
2014-05-11 23:57:08 +00:00
|
|
|
|
|
2014-08-06 13:43:29 +00:00
|
|
|
|
namespace find_object {
|
|
|
|
|
|
|
2014-05-11 23:57:08 +00:00
|
|
|
|
Vocabulary::Vocabulary()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Vocabulary::~Vocabulary()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Vocabulary::clear()
|
|
|
|
|
|
{
|
2014-05-12 22:58:57 +00:00
|
|
|
|
indexedDescriptors_ = cv::Mat();
|
|
|
|
|
|
notIndexedDescriptors_ = cv::Mat();
|
2014-05-11 23:57:08 +00:00
|
|
|
|
wordToObjects_.clear();
|
2014-05-12 22:58:57 +00:00
|
|
|
|
notIndexedWordIds_.clear();
|
2014-05-11 23:57:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-01-09 22:44:16 +00:00
|
|
|
|
void Vocabulary::save(QDataStream & streamPtr) const
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!indexedDescriptors_.empty() && !wordToObjects_.empty())
|
|
|
|
|
|
{
|
|
|
|
|
|
UASSERT(notIndexedDescriptors_.empty() && notIndexedWordIds_.empty());
|
|
|
|
|
|
|
|
|
|
|
|
// save index
|
|
|
|
|
|
streamPtr << wordToObjects_;
|
|
|
|
|
|
|
|
|
|
|
|
// save words
|
|
|
|
|
|
qint64 dataSize = indexedDescriptors_.elemSize()*indexedDescriptors_.cols*indexedDescriptors_.rows;
|
|
|
|
|
|
streamPtr << indexedDescriptors_.rows <<
|
|
|
|
|
|
indexedDescriptors_.cols <<
|
|
|
|
|
|
indexedDescriptors_.type() <<
|
|
|
|
|
|
dataSize;
|
|
|
|
|
|
streamPtr << QByteArray((char*)indexedDescriptors_.data, dataSize);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Vocabulary::load(QDataStream & streamPtr)
|
|
|
|
|
|
{
|
|
|
|
|
|
// load index
|
|
|
|
|
|
streamPtr >> wordToObjects_;
|
|
|
|
|
|
|
|
|
|
|
|
// load words
|
|
|
|
|
|
int rows,cols,type;
|
|
|
|
|
|
qint64 dataSize;
|
|
|
|
|
|
streamPtr >> rows >> cols >> type >> dataSize;
|
|
|
|
|
|
QByteArray data;
|
|
|
|
|
|
streamPtr >> data;
|
|
|
|
|
|
indexedDescriptors_ = cv::Mat(rows, cols, type, data.data()).clone();
|
|
|
|
|
|
|
|
|
|
|
|
update();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-07-31 19:02:31 +00:00
|
|
|
|
QMultiMap<int, int> Vocabulary::addWords(const cv::Mat & descriptors, int objectId, bool incremental)
|
2014-05-11 23:57:08 +00:00
|
|
|
|
{
|
|
|
|
|
|
QMultiMap<int, int> words;
|
|
|
|
|
|
if (descriptors.empty())
|
|
|
|
|
|
{
|
|
|
|
|
|
return words;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(incremental)
|
|
|
|
|
|
{
|
|
|
|
|
|
int k = 2;
|
2014-05-12 22:58:57 +00:00
|
|
|
|
cv::Mat results;
|
|
|
|
|
|
cv::Mat dists;
|
2014-05-11 23:57:08 +00:00
|
|
|
|
|
|
|
|
|
|
bool globalSearch = false;
|
2014-05-12 22:58:57 +00:00
|
|
|
|
if(!indexedDescriptors_.empty() && indexedDescriptors_.rows >= (int)k)
|
2014-05-11 23:57:08 +00:00
|
|
|
|
{
|
2015-01-05 23:11:15 +00:00
|
|
|
|
UASSERT(indexedDescriptors_.type() == descriptors.type() && indexedDescriptors_.cols == descriptors.cols);
|
2014-10-24 02:46:03 +00:00
|
|
|
|
this->search(descriptors, results, dists, k);
|
2014-05-12 22:58:57 +00:00
|
|
|
|
|
|
|
|
|
|
if( dists.type() == CV_32S )
|
|
|
|
|
|
{
|
|
|
|
|
|
cv::Mat temp;
|
|
|
|
|
|
dists.convertTo(temp, CV_32F);
|
|
|
|
|
|
dists = temp;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-05-11 23:57:08 +00:00
|
|
|
|
globalSearch = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-05-13 14:41:36 +00:00
|
|
|
|
notIndexedWordIds_.reserve(notIndexedWordIds_.size() + descriptors.rows);
|
|
|
|
|
|
notIndexedDescriptors_.reserve(notIndexedDescriptors_.rows + descriptors.rows);
|
2014-05-11 23:57:08 +00:00
|
|
|
|
int matches = 0;
|
|
|
|
|
|
for(int i = 0; i < descriptors.rows; ++i)
|
|
|
|
|
|
{
|
2014-06-21 00:05:23 +00:00
|
|
|
|
QMultiMap<float, int> fullResults; // nearest descriptors sorted by distance
|
2014-05-13 14:41:36 +00:00
|
|
|
|
if(notIndexedDescriptors_.rows)
|
2014-05-11 23:57:08 +00:00
|
|
|
|
{
|
2015-01-05 23:11:15 +00:00
|
|
|
|
UASSERT(notIndexedDescriptors_.type() == descriptors.type() && notIndexedDescriptors_.cols == descriptors.cols);
|
2014-05-12 22:58:57 +00:00
|
|
|
|
|
2014-05-11 23:57:08 +00:00
|
|
|
|
// Check if this descriptor matches with a word not already added to the vocabulary
|
2014-05-12 22:58:57 +00:00
|
|
|
|
// Do linear search only
|
|
|
|
|
|
cv::Mat tmpResults;
|
|
|
|
|
|
cv::Mat tmpDists;
|
2014-05-11 23:57:08 +00:00
|
|
|
|
if(descriptors.type()==CV_8U)
|
|
|
|
|
|
{
|
2014-05-12 22:58:57 +00:00
|
|
|
|
//normType – One of NORM_L1, NORM_L2, NORM_HAMMING, NORM_HAMMING2. L1 and L2 norms are
|
|
|
|
|
|
// preferable choices for SIFT and SURF descriptors, NORM_HAMMING should be
|
|
|
|
|
|
// used with ORB, BRISK and BRIEF, NORM_HAMMING2 should be used with ORB
|
|
|
|
|
|
// when WTA_K==3 or 4 (see ORB::ORB constructor description).
|
|
|
|
|
|
int normType = cv::NORM_HAMMING;
|
|
|
|
|
|
if(Settings::currentDescriptorType().compare("ORB") &&
|
|
|
|
|
|
(Settings::getFeature2D_ORB_WTA_K()==3 || Settings::getFeature2D_ORB_WTA_K()==4))
|
|
|
|
|
|
{
|
|
|
|
|
|
normType = cv::NORM_HAMMING2;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
cv::batchDistance( descriptors.row(i),
|
2014-05-13 14:41:36 +00:00
|
|
|
|
notIndexedDescriptors_,
|
2014-05-12 22:58:57 +00:00
|
|
|
|
tmpDists,
|
|
|
|
|
|
CV_32S,
|
|
|
|
|
|
tmpResults,
|
|
|
|
|
|
normType,
|
2014-05-13 14:41:36 +00:00
|
|
|
|
notIndexedDescriptors_.rows>=k?k:1,
|
2014-05-12 22:58:57 +00:00
|
|
|
|
cv::Mat(),
|
|
|
|
|
|
0,
|
|
|
|
|
|
false);
|
2014-05-11 23:57:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2014-05-12 22:58:57 +00:00
|
|
|
|
cv::flann::Index tmpIndex;
|
2014-05-13 14:41:36 +00:00
|
|
|
|
tmpIndex.build(notIndexedDescriptors_, cv::flann::LinearIndexParams(), Settings::getFlannDistanceType());
|
|
|
|
|
|
tmpIndex.knnSearch(descriptors.row(i), tmpResults, tmpDists, notIndexedDescriptors_.rows>1?k:1, Settings::getFlannSearchParams());
|
2014-05-11 23:57:08 +00:00
|
|
|
|
}
|
2014-05-12 22:58:57 +00:00
|
|
|
|
|
|
|
|
|
|
if( tmpDists.type() == CV_32S )
|
2014-05-11 23:57:08 +00:00
|
|
|
|
{
|
2014-05-12 22:58:57 +00:00
|
|
|
|
cv::Mat temp;
|
|
|
|
|
|
tmpDists.convertTo(temp, CV_32F);
|
|
|
|
|
|
tmpDists = temp;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-05-13 14:41:36 +00:00
|
|
|
|
for(int j = 0; j < tmpResults.cols; ++j)
|
2014-05-12 22:58:57 +00:00
|
|
|
|
{
|
|
|
|
|
|
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));
|
2014-05-13 14:41:36 +00:00
|
|
|
|
fullResults.insert(tmpDists.at<float>(0,j), notIndexedWordIds_.at(tmpResults.at<int>(0,j)));
|
2014-05-12 22:58:57 +00:00
|
|
|
|
}
|
2014-05-11 23:57:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(globalSearch)
|
|
|
|
|
|
{
|
|
|
|
|
|
for(int j=0; j<k; ++j)
|
|
|
|
|
|
{
|
2014-05-12 22:58:57 +00:00
|
|
|
|
if(results.at<int>(i,j) >= 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
//printf("global i=%d, j=%d, dist=%f\n", i ,j, dists.at<float>(i,j));
|
|
|
|
|
|
fullResults.insert(dists.at<float>(i,j), results.at<int>(i,j));
|
|
|
|
|
|
}
|
2014-05-11 23:57:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool match = false;
|
|
|
|
|
|
// Apply NNDR
|
|
|
|
|
|
if(fullResults.size() >= 2 &&
|
|
|
|
|
|
fullResults.begin().key() <= Settings::getNearestNeighbor_4nndrRatio() * (++fullResults.begin()).key())
|
|
|
|
|
|
{
|
|
|
|
|
|
match = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(match)
|
|
|
|
|
|
{
|
|
|
|
|
|
words.insert(fullResults.begin().value(), i);
|
2014-07-31 19:02:31 +00:00
|
|
|
|
wordToObjects_.insert(fullResults.begin().value(), objectId);
|
2014-05-11 23:57:08 +00:00
|
|
|
|
++matches;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2014-05-13 14:41:36 +00:00
|
|
|
|
//concatenate new words
|
|
|
|
|
|
notIndexedWordIds_.push_back(indexedDescriptors_.rows + notIndexedDescriptors_.rows);
|
|
|
|
|
|
notIndexedDescriptors_.push_back(descriptors.row(i));
|
|
|
|
|
|
words.insert(notIndexedWordIds_.back(), i);
|
2014-07-31 19:02:31 +00:00
|
|
|
|
wordToObjects_.insert(notIndexedWordIds_.back(), objectId);
|
2014-05-11 23:57:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
for(int i = 0; i < descriptors.rows; ++i)
|
|
|
|
|
|
{
|
2014-07-31 19:02:31 +00:00
|
|
|
|
wordToObjects_.insert(indexedDescriptors_.rows + notIndexedDescriptors_.rows+i, objectId);
|
2014-05-12 22:58:57 +00:00
|
|
|
|
words.insert(indexedDescriptors_.rows + notIndexedDescriptors_.rows+i, i);
|
|
|
|
|
|
notIndexedWordIds_.push_back(indexedDescriptors_.rows + notIndexedDescriptors_.rows+i);
|
2014-05-11 23:57:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//just concatenate descriptors
|
2014-05-13 14:41:36 +00:00
|
|
|
|
notIndexedDescriptors_.push_back(descriptors);
|
2014-05-11 23:57:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return words;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Vocabulary::update()
|
|
|
|
|
|
{
|
2014-05-12 22:58:57 +00:00
|
|
|
|
if(!notIndexedDescriptors_.empty())
|
|
|
|
|
|
{
|
2015-01-05 23:11:15 +00:00
|
|
|
|
if(!indexedDescriptors_.empty())
|
|
|
|
|
|
{
|
|
|
|
|
|
UASSERT(indexedDescriptors_.cols == notIndexedDescriptors_.cols &&
|
|
|
|
|
|
indexedDescriptors_.type() == notIndexedDescriptors_.type() );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-05-12 22:58:57 +00:00
|
|
|
|
//concatenate descriptors
|
2014-05-13 14:41:36 +00:00
|
|
|
|
indexedDescriptors_.push_back(notIndexedDescriptors_);
|
2014-05-12 22:58:57 +00:00
|
|
|
|
|
|
|
|
|
|
notIndexedDescriptors_ = cv::Mat();
|
|
|
|
|
|
notIndexedWordIds_.clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-10-24 02:46:03 +00:00
|
|
|
|
if(!indexedDescriptors_.empty() && !Settings::isBruteForceNearestNeighbor())
|
2014-05-11 23:57:08 +00:00
|
|
|
|
{
|
|
|
|
|
|
cv::flann::IndexParams * params = Settings::createFlannIndexParams();
|
2014-05-12 22:58:57 +00:00
|
|
|
|
flannIndex_.build(indexedDescriptors_, *params, Settings::getFlannDistanceType());
|
2014-05-11 23:57:08 +00:00
|
|
|
|
delete params;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Vocabulary::search(const cv::Mat & descriptors, cv::Mat & results, cv::Mat & dists, int k)
|
|
|
|
|
|
{
|
2014-05-12 22:58:57 +00:00
|
|
|
|
if(!indexedDescriptors_.empty())
|
2014-05-11 23:57:08 +00:00
|
|
|
|
{
|
2015-01-05 23:11:15 +00:00
|
|
|
|
UASSERT(descriptors.type() == indexedDescriptors_.type() && descriptors.cols == indexedDescriptors_.cols);
|
2014-05-12 22:58:57 +00:00
|
|
|
|
|
2014-10-24 02:46:03 +00:00
|
|
|
|
if(Settings::isBruteForceNearestNeighbor())
|
|
|
|
|
|
{
|
|
|
|
|
|
std::vector<std::vector<cv::DMatch> > matches;
|
|
|
|
|
|
if(Settings::getNearestNeighbor_BruteForce_gpu() && cv::gpu::getCudaEnabledDeviceCount())
|
|
|
|
|
|
{
|
|
|
|
|
|
cv::gpu::GpuMat newDescriptorsGpu(descriptors);
|
|
|
|
|
|
cv::gpu::GpuMat lastDescriptorsGpu(indexedDescriptors_);
|
|
|
|
|
|
if(indexedDescriptors_.type()==CV_8U)
|
|
|
|
|
|
{
|
|
|
|
|
|
cv::gpu::BruteForceMatcher_GPU<cv::Hamming> gpuMatcher;
|
|
|
|
|
|
gpuMatcher.knnMatch(newDescriptorsGpu, lastDescriptorsGpu, matches, k);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
cv::gpu::BruteForceMatcher_GPU<cv::L2<float> > gpuMatcher;
|
|
|
|
|
|
gpuMatcher.knnMatch(newDescriptorsGpu, lastDescriptorsGpu, matches, k);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
cv::BFMatcher matcher(indexedDescriptors_.type()==CV_8U?cv::NORM_HAMMING:cv::NORM_L2);
|
|
|
|
|
|
matcher.knnMatch(descriptors, indexedDescriptors_, matches, k);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//convert back to matrix style
|
2015-03-02 16:39:48 +00:00
|
|
|
|
results = cv::Mat((int)matches.size(), k, CV_32SC1);
|
|
|
|
|
|
dists = cv::Mat((int)matches.size(), k, CV_32FC1);
|
2014-10-24 02:46:03 +00:00
|
|
|
|
for(unsigned int i=0; i<matches.size(); ++i)
|
|
|
|
|
|
{
|
|
|
|
|
|
for(int j=0; j<k; ++j)
|
|
|
|
|
|
{
|
|
|
|
|
|
results.at<int>(i, j) = matches[i].at(j).trainIdx;
|
|
|
|
|
|
dists.at<float>(i, j) = matches[i].at(j).distance;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
flannIndex_.knnSearch(descriptors, results, dists, k, Settings::getFlannSearchParams());
|
|
|
|
|
|
}
|
2014-05-12 22:58:57 +00:00
|
|
|
|
|
|
|
|
|
|
if( dists.type() == CV_32S )
|
|
|
|
|
|
{
|
|
|
|
|
|
cv::Mat temp;
|
|
|
|
|
|
dists.convertTo(temp, CV_32F);
|
|
|
|
|
|
dists = temp;
|
|
|
|
|
|
}
|
2014-05-11 23:57:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2014-08-06 13:43:29 +00:00
|
|
|
|
|
|
|
|
|
|
} // namespace find_object
|