2010-09-23 21:44:23 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Intel License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2000, Intel Corporation, all rights reserved.
|
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's 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.
|
|
|
|
//
|
|
|
|
// * The name of Intel Corporation may not 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 disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any 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.
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
|
|
|
#include "precomp.hpp"
|
|
|
|
|
2011-05-27 20:15:36 +08:00
|
|
|
#if defined(HAVE_EIGEN) && EIGEN_WORLD_VERSION == 2
|
2010-09-23 21:44:23 +08:00
|
|
|
#include <Eigen/Array>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace cv
|
|
|
|
{
|
|
|
|
|
|
|
|
Mat windowedMatchingMask( const vector<KeyPoint>& keypoints1, const vector<KeyPoint>& keypoints2,
|
|
|
|
float maxDeltaX, float maxDeltaY )
|
|
|
|
{
|
|
|
|
if( keypoints1.empty() || keypoints2.empty() )
|
|
|
|
return Mat();
|
|
|
|
|
2010-11-26 00:55:46 +08:00
|
|
|
int n1 = (int)keypoints1.size(), n2 = (int)keypoints2.size();
|
|
|
|
Mat mask( n1, n2, CV_8UC1 );
|
|
|
|
for( int i = 0; i < n1; i++ )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-11-26 00:55:46 +08:00
|
|
|
for( int j = 0; j < n2; j++ )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
|
|
|
Point2f diff = keypoints2[j].pt - keypoints1[i].pt;
|
|
|
|
mask.at<uchar>(i, j) = std::abs(diff.x) < maxDeltaX && std::abs(diff.y) < maxDeltaY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************************\
|
|
|
|
* DescriptorMatcher *
|
|
|
|
\****************************************************************************************/
|
2010-11-23 02:27:08 +08:00
|
|
|
DescriptorMatcher::DescriptorCollection::DescriptorCollection()
|
|
|
|
{}
|
|
|
|
|
|
|
|
DescriptorMatcher::DescriptorCollection::DescriptorCollection( const DescriptorCollection& collection )
|
|
|
|
{
|
|
|
|
mergedDescriptors = collection.mergedDescriptors.clone();
|
|
|
|
copy( collection.startIdxs.begin(), collection.startIdxs.begin(), startIdxs.begin() );
|
|
|
|
}
|
|
|
|
|
|
|
|
DescriptorMatcher::DescriptorCollection::~DescriptorCollection()
|
|
|
|
{}
|
|
|
|
|
|
|
|
void DescriptorMatcher::DescriptorCollection::set( const vector<Mat>& descriptors )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
clear();
|
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
size_t imageCount = descriptors.size();
|
2010-10-29 16:44:42 +08:00
|
|
|
CV_Assert( imageCount > 0 );
|
|
|
|
|
|
|
|
startIdxs.resize( imageCount );
|
|
|
|
|
|
|
|
int dim = -1;
|
|
|
|
int type = -1;
|
|
|
|
startIdxs[0] = 0;
|
|
|
|
for( size_t i = 1; i < imageCount; i++ )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
int s = 0;
|
2010-11-23 02:27:08 +08:00
|
|
|
if( !descriptors[i-1].empty() )
|
2010-10-29 16:44:42 +08:00
|
|
|
{
|
2010-11-23 02:27:08 +08:00
|
|
|
dim = descriptors[i-1].cols;
|
|
|
|
type = descriptors[i-1].type();
|
|
|
|
s = descriptors[i-1].rows;
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
|
|
|
startIdxs[i] = startIdxs[i-1] + s;
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
2010-10-29 16:44:42 +08:00
|
|
|
if( imageCount == 1 )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-11-23 02:27:08 +08:00
|
|
|
if( descriptors[0].empty() ) return;
|
2010-10-29 16:44:42 +08:00
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
dim = descriptors[0].cols;
|
|
|
|
type = descriptors[0].type();
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
|
|
|
assert( dim > 0 );
|
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
int count = startIdxs[imageCount-1] + descriptors[imageCount-1].rows;
|
2010-10-29 16:44:42 +08:00
|
|
|
|
|
|
|
if( count > 0 )
|
|
|
|
{
|
2010-11-23 02:27:08 +08:00
|
|
|
mergedDescriptors.create( count, dim, type );
|
2010-10-29 16:44:42 +08:00
|
|
|
for( size_t i = 0; i < imageCount; i++ )
|
|
|
|
{
|
2010-11-23 02:27:08 +08:00
|
|
|
if( !descriptors[i].empty() )
|
2010-10-29 16:44:42 +08:00
|
|
|
{
|
2010-11-23 02:27:08 +08:00
|
|
|
CV_Assert( descriptors[i].cols == dim && descriptors[i].type() == type );
|
|
|
|
Mat m = mergedDescriptors.rowRange( startIdxs[i], startIdxs[i] + descriptors[i].rows );
|
|
|
|
descriptors[i].copyTo(m);
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
void DescriptorMatcher::DescriptorCollection::clear()
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
startIdxs.clear();
|
2010-11-23 02:27:08 +08:00
|
|
|
mergedDescriptors.release();
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
const Mat DescriptorMatcher::DescriptorCollection::getDescriptor( int imgIdx, int localDescIdx ) const
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
CV_Assert( imgIdx < (int)startIdxs.size() );
|
|
|
|
int globalIdx = startIdxs[imgIdx] + localDescIdx;
|
|
|
|
CV_Assert( globalIdx < (int)size() );
|
|
|
|
|
|
|
|
return getDescriptor( globalIdx );
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
const Mat& DescriptorMatcher::DescriptorCollection::getDescriptors() const
|
|
|
|
{
|
|
|
|
return mergedDescriptors;
|
|
|
|
}
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
const Mat DescriptorMatcher::DescriptorCollection::getDescriptor( int globalDescIdx ) const
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
CV_Assert( globalDescIdx < size() );
|
2010-11-23 02:27:08 +08:00
|
|
|
return mergedDescriptors.row( globalDescIdx );
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
void DescriptorMatcher::DescriptorCollection::getLocalIdx( int globalDescIdx, int& imgIdx, int& localDescIdx ) const
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2011-03-13 08:31:17 +08:00
|
|
|
CV_Assert( (globalDescIdx>=0) && (globalDescIdx < size()) );
|
|
|
|
std::vector<int>::const_iterator img_it = std::upper_bound(startIdxs.begin(), startIdxs.end(), globalDescIdx);
|
|
|
|
--img_it;
|
2011-05-17 23:44:01 +08:00
|
|
|
imgIdx = (int)(img_it - startIdxs.begin());
|
2011-03-13 08:31:17 +08:00
|
|
|
localDescIdx = globalDescIdx - (*img_it);
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
int DescriptorMatcher::DescriptorCollection::size() const
|
|
|
|
{
|
|
|
|
return mergedDescriptors.rows;
|
|
|
|
}
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
/*
|
|
|
|
* DescriptorMatcher
|
|
|
|
*/
|
|
|
|
void convertMatches( const vector<vector<DMatch> >& knnMatches, vector<DMatch>& matches )
|
|
|
|
{
|
|
|
|
matches.clear();
|
|
|
|
matches.reserve( knnMatches.size() );
|
|
|
|
for( size_t i = 0; i < knnMatches.size(); i++ )
|
|
|
|
{
|
|
|
|
CV_Assert( knnMatches[i].size() <= 1 );
|
|
|
|
if( !knnMatches[i].empty() )
|
|
|
|
matches.push_back( knnMatches[i][0] );
|
|
|
|
}
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
DescriptorMatcher::~DescriptorMatcher()
|
|
|
|
{}
|
|
|
|
|
|
|
|
void DescriptorMatcher::add( const vector<Mat>& descriptors )
|
|
|
|
{
|
|
|
|
trainDescCollection.insert( trainDescCollection.end(), descriptors.begin(), descriptors.end() );
|
|
|
|
}
|
|
|
|
|
|
|
|
const vector<Mat>& DescriptorMatcher::getTrainDescriptors() const
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-11-23 02:27:08 +08:00
|
|
|
return trainDescCollection;
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
void DescriptorMatcher::clear()
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
trainDescCollection.clear();
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
bool DescriptorMatcher::empty() const
|
|
|
|
{
|
2011-01-31 22:18:50 +08:00
|
|
|
return trainDescCollection.empty();
|
2010-11-23 02:27:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DescriptorMatcher::train()
|
|
|
|
{}
|
|
|
|
|
|
|
|
void DescriptorMatcher::match( const Mat& queryDescriptors, const Mat& trainDescriptors, vector<DMatch>& matches, const Mat& mask ) const
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-11-23 02:27:08 +08:00
|
|
|
Ptr<DescriptorMatcher> tempMatcher = clone(true);
|
|
|
|
tempMatcher->add( vector<Mat>(1, trainDescriptors) );
|
|
|
|
tempMatcher->match( queryDescriptors, matches, vector<Mat>(1, mask) );
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
void DescriptorMatcher::knnMatch( const Mat& queryDescriptors, const Mat& trainDescriptors, vector<vector<DMatch> >& matches, int knn,
|
2010-10-29 16:44:42 +08:00
|
|
|
const Mat& mask, bool compactResult ) const
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-11-23 02:27:08 +08:00
|
|
|
Ptr<DescriptorMatcher> tempMatcher = clone(true);
|
|
|
|
tempMatcher->add( vector<Mat>(1, trainDescriptors) );
|
|
|
|
tempMatcher->knnMatch( queryDescriptors, matches, knn, vector<Mat>(1, mask), compactResult );
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
void DescriptorMatcher::radiusMatch( const Mat& queryDescriptors, const Mat& trainDescriptors, vector<vector<DMatch> >& matches, float maxDistance,
|
2010-10-29 16:44:42 +08:00
|
|
|
const Mat& mask, bool compactResult ) const
|
|
|
|
{
|
2010-11-23 02:27:08 +08:00
|
|
|
Ptr<DescriptorMatcher> tempMatcher = clone(true);
|
|
|
|
tempMatcher->add( vector<Mat>(1, trainDescriptors) );
|
|
|
|
tempMatcher->radiusMatch( queryDescriptors, matches, maxDistance, vector<Mat>(1, mask), compactResult );
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
void DescriptorMatcher::match( const Mat& queryDescriptors, vector<DMatch>& matches, const vector<Mat>& masks )
|
2010-10-29 16:44:42 +08:00
|
|
|
{
|
|
|
|
vector<vector<DMatch> > knnMatches;
|
2010-11-23 02:27:08 +08:00
|
|
|
knnMatch( queryDescriptors, knnMatches, 1, masks, true /*compactResult*/ );
|
2010-10-29 16:44:42 +08:00
|
|
|
convertMatches( knnMatches, matches );
|
|
|
|
}
|
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
void DescriptorMatcher::checkMasks( const vector<Mat>& masks, int queryDescriptorsCount ) const
|
|
|
|
{
|
2011-05-02 01:38:52 +08:00
|
|
|
if( isMaskSupported() && !masks.empty() )
|
|
|
|
{
|
|
|
|
// Check masks
|
|
|
|
size_t imageCount = trainDescCollection.size();
|
|
|
|
CV_Assert( masks.size() == imageCount );
|
|
|
|
for( size_t i = 0; i < imageCount; i++ )
|
|
|
|
{
|
|
|
|
if( !masks[i].empty() && !trainDescCollection[i].empty() )
|
|
|
|
{
|
|
|
|
CV_Assert( masks[i].rows == queryDescriptorsCount &&
|
|
|
|
masks[i].cols == trainDescCollection[i].rows &&
|
|
|
|
masks[i].type() == CV_8UC1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-11-23 02:27:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DescriptorMatcher::knnMatch( const Mat& queryDescriptors, vector<vector<DMatch> >& matches, int knn,
|
2010-10-29 16:44:42 +08:00
|
|
|
const vector<Mat>& masks, bool compactResult )
|
|
|
|
{
|
2011-04-29 22:12:17 +08:00
|
|
|
matches.clear();
|
|
|
|
if( empty() || queryDescriptors.empty() )
|
|
|
|
return;
|
2010-11-23 02:27:08 +08:00
|
|
|
|
2011-04-29 22:12:17 +08:00
|
|
|
CV_Assert( knn > 0 );
|
2010-11-23 02:27:08 +08:00
|
|
|
|
2011-04-29 22:12:17 +08:00
|
|
|
checkMasks( masks, queryDescriptors.rows );
|
2010-11-23 02:27:08 +08:00
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
train();
|
2010-11-23 02:27:08 +08:00
|
|
|
knnMatchImpl( queryDescriptors, matches, knn, masks, compactResult );
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
void DescriptorMatcher::radiusMatch( const Mat& queryDescriptors, vector<vector<DMatch> >& matches, float maxDistance,
|
2010-10-29 16:44:42 +08:00
|
|
|
const vector<Mat>& masks, bool compactResult )
|
|
|
|
{
|
2011-04-29 22:12:17 +08:00
|
|
|
matches.clear();
|
|
|
|
if( empty() || queryDescriptors.empty() )
|
|
|
|
return;
|
2010-11-23 02:27:08 +08:00
|
|
|
|
2011-04-29 22:12:17 +08:00
|
|
|
CV_Assert( maxDistance > std::numeric_limits<float>::epsilon() );
|
2010-11-23 02:27:08 +08:00
|
|
|
|
2011-04-29 22:12:17 +08:00
|
|
|
checkMasks( masks, queryDescriptors.rows );
|
2010-11-23 02:27:08 +08:00
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
train();
|
2010-11-23 02:27:08 +08:00
|
|
|
radiusMatchImpl( queryDescriptors, matches, maxDistance, masks, compactResult );
|
|
|
|
}
|
|
|
|
|
|
|
|
void DescriptorMatcher::read( const FileNode& )
|
|
|
|
{}
|
|
|
|
|
|
|
|
void DescriptorMatcher::write( FileStorage& ) const
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool DescriptorMatcher::isPossibleMatch( const Mat& mask, int queryIdx, int trainIdx )
|
|
|
|
{
|
|
|
|
return mask.empty() || mask.at<uchar>(queryIdx, trainIdx);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DescriptorMatcher::isMaskedOut( const vector<Mat>& masks, int queryIdx )
|
|
|
|
{
|
|
|
|
size_t outCount = 0;
|
|
|
|
for( size_t i = 0; i < masks.size(); i++ )
|
|
|
|
{
|
|
|
|
if( !masks[i].empty() && (countNonZero(masks[i].row(queryIdx)) == 0) )
|
|
|
|
outCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !masks.empty() && outCount == masks.size() ;
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
|
2010-11-25 23:59:37 +08:00
|
|
|
/*
|
|
|
|
* Factory function for DescriptorMatcher creating
|
|
|
|
*/
|
|
|
|
Ptr<DescriptorMatcher> DescriptorMatcher::create( const string& descriptorMatcherType )
|
|
|
|
{
|
|
|
|
DescriptorMatcher* dm = 0;
|
|
|
|
if( !descriptorMatcherType.compare( "FlannBased" ) )
|
|
|
|
{
|
|
|
|
dm = new FlannBasedMatcher();
|
|
|
|
}
|
|
|
|
else if( !descriptorMatcherType.compare( "BruteForce" ) ) // L2
|
|
|
|
{
|
|
|
|
dm = new BruteForceMatcher<L2<float> >();
|
|
|
|
}
|
|
|
|
else if( !descriptorMatcherType.compare( "BruteForce-L1" ) )
|
|
|
|
{
|
|
|
|
dm = new BruteForceMatcher<L1<float> >();
|
|
|
|
}
|
|
|
|
else if( !descriptorMatcherType.compare("BruteForce-Hamming") )
|
|
|
|
{
|
|
|
|
dm = new BruteForceMatcher<Hamming>();
|
|
|
|
}
|
|
|
|
else if( !descriptorMatcherType.compare( "BruteForce-HammingLUT") )
|
|
|
|
{
|
|
|
|
dm = new BruteForceMatcher<HammingLUT>();
|
|
|
|
}
|
|
|
|
|
|
|
|
return dm;
|
|
|
|
}
|
2010-11-23 02:27:08 +08:00
|
|
|
|
2010-11-25 23:59:37 +08:00
|
|
|
/*
|
|
|
|
* BruteForce L2 specialization
|
|
|
|
*/
|
2010-09-23 21:44:23 +08:00
|
|
|
template<>
|
2010-11-23 02:27:08 +08:00
|
|
|
void BruteForceMatcher<L2<float> >::knnMatchImpl( const Mat& queryDescriptors, vector<vector<DMatch> >& matches, int knn,
|
|
|
|
const vector<Mat>& masks, bool compactResult )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2011-05-27 20:15:36 +08:00
|
|
|
#ifndef HAVE_EIGEN
|
2010-11-23 02:27:08 +08:00
|
|
|
commonKnnMatchImpl( *this, queryDescriptors, matches, knn, masks, compactResult );
|
2010-10-29 16:44:42 +08:00
|
|
|
#else
|
2010-11-23 02:27:08 +08:00
|
|
|
CV_Assert( queryDescriptors.type() == CV_32FC1 || queryDescriptors.empty() );
|
2010-10-29 16:44:42 +08:00
|
|
|
CV_Assert( masks.empty() || masks.size() == trainDescCollection.size() );
|
2010-09-23 21:44:23 +08:00
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
matches.reserve(queryDescriptors.rows);
|
2010-10-29 16:44:42 +08:00
|
|
|
size_t imgCount = trainDescCollection.size();
|
|
|
|
|
|
|
|
Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> e_query_t;
|
|
|
|
vector<Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> > e_trainCollection(trainDescCollection.size());
|
|
|
|
vector<Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> > e_trainNorms2(trainDescCollection.size());
|
2010-11-23 02:27:08 +08:00
|
|
|
cv2eigen( queryDescriptors.t(), e_query_t);
|
2010-10-29 16:44:42 +08:00
|
|
|
for( size_t i = 0; i < trainDescCollection.size(); i++ )
|
|
|
|
{
|
|
|
|
cv2eigen( trainDescCollection[i], e_trainCollection[i] );
|
|
|
|
e_trainNorms2[i] = e_trainCollection[i].rowwise().squaredNorm() / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<Eigen::Matrix<float, Eigen::Dynamic, 1> > e_allDists( imgCount ); // distances between one query descriptor and all train descriptors
|
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
for( int qIdx = 0; qIdx < queryDescriptors.rows; qIdx++ )
|
2010-10-29 16:44:42 +08:00
|
|
|
{
|
2010-11-23 17:00:32 +08:00
|
|
|
if( isMaskedOut( masks, qIdx ) )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
if( !compactResult ) // push empty vector
|
|
|
|
matches.push_back( vector<DMatch>() );
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
float queryNorm2 = e_query_t.col(qIdx).squaredNorm();
|
|
|
|
// 1. compute distances between i-th query descriptor and all train descriptors
|
|
|
|
for( size_t iIdx = 0; iIdx < imgCount; iIdx++ )
|
|
|
|
{
|
|
|
|
CV_Assert( masks.empty() || masks[iIdx].empty() ||
|
2010-11-23 02:27:08 +08:00
|
|
|
( masks[iIdx].rows == queryDescriptors.rows && masks[iIdx].cols == trainDescCollection[iIdx].rows &&
|
2010-10-29 16:44:42 +08:00
|
|
|
masks[iIdx].type() == CV_8UC1 ) );
|
|
|
|
CV_Assert( trainDescCollection[iIdx].type() == CV_32FC1 || trainDescCollection[iIdx].empty() );
|
2010-11-23 02:27:08 +08:00
|
|
|
CV_Assert( queryDescriptors.cols == trainDescCollection[iIdx].cols );
|
2010-09-23 21:44:23 +08:00
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
e_allDists[iIdx] = e_trainCollection[iIdx] *e_query_t.col(qIdx);
|
|
|
|
e_allDists[iIdx] -= e_trainNorms2[iIdx];
|
|
|
|
|
|
|
|
if( !masks.empty() && !masks[iIdx].empty() )
|
|
|
|
{
|
|
|
|
const uchar* maskPtr = (uchar*)masks[iIdx].ptr(qIdx);
|
|
|
|
for( int c = 0; c < masks[iIdx].cols; c++ )
|
|
|
|
{
|
|
|
|
if( maskPtr[c] == 0 )
|
2011-01-12 20:03:03 +08:00
|
|
|
e_allDists[iIdx](c) = -std::numeric_limits<float>::max();
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. choose knn nearest matches for query[i]
|
|
|
|
matches.push_back( vector<DMatch>() );
|
|
|
|
vector<vector<DMatch> >::reverse_iterator curMatches = matches.rbegin();
|
|
|
|
for( int k = 0; k < knn; k++ )
|
|
|
|
{
|
2011-01-12 20:03:03 +08:00
|
|
|
float totalMaxCoeff = -std::numeric_limits<float>::max();
|
2010-10-29 16:44:42 +08:00
|
|
|
int bestTrainIdx = -1, bestImgIdx = -1;
|
|
|
|
for( size_t iIdx = 0; iIdx < imgCount; iIdx++ )
|
|
|
|
{
|
|
|
|
int loc;
|
|
|
|
float curMaxCoeff = e_allDists[iIdx].maxCoeff( &loc );
|
|
|
|
if( curMaxCoeff > totalMaxCoeff )
|
|
|
|
{
|
|
|
|
totalMaxCoeff = curMaxCoeff;
|
|
|
|
bestTrainIdx = loc;
|
|
|
|
bestImgIdx = iIdx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( bestTrainIdx == -1 )
|
|
|
|
break;
|
|
|
|
|
2011-01-12 20:03:03 +08:00
|
|
|
e_allDists[bestImgIdx](bestTrainIdx) = -std::numeric_limits<float>::max();
|
2010-10-29 16:44:42 +08:00
|
|
|
curMatches->push_back( DMatch(qIdx, bestTrainIdx, bestImgIdx, sqrt((-2)*totalMaxCoeff + queryNorm2)) );
|
|
|
|
}
|
|
|
|
std::sort( curMatches->begin(), curMatches->end() );
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
}
|
2010-10-29 16:44:42 +08:00
|
|
|
#endif
|
|
|
|
}
|
2010-09-23 21:44:23 +08:00
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
template<>
|
2010-11-23 02:27:08 +08:00
|
|
|
void BruteForceMatcher<L2<float> >::radiusMatchImpl( const Mat& queryDescriptors, vector<vector<DMatch> >& matches, float maxDistance,
|
2010-10-29 16:44:42 +08:00
|
|
|
const vector<Mat>& masks, bool compactResult )
|
|
|
|
{
|
2011-05-27 20:15:36 +08:00
|
|
|
#ifndef HAVE_EIGEN
|
2010-11-23 02:27:08 +08:00
|
|
|
commonRadiusMatchImpl( *this, queryDescriptors, matches, maxDistance, masks, compactResult );
|
2010-09-23 21:44:23 +08:00
|
|
|
#else
|
2010-11-23 02:27:08 +08:00
|
|
|
CV_Assert( queryDescriptors.type() == CV_32FC1 || queryDescriptors.empty() );
|
2010-10-29 16:44:42 +08:00
|
|
|
CV_Assert( masks.empty() || masks.size() == trainDescCollection.size() );
|
2010-09-23 21:44:23 +08:00
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
matches.reserve(queryDescriptors.rows);
|
2010-10-29 16:44:42 +08:00
|
|
|
size_t imgCount = trainDescCollection.size();
|
2010-09-23 21:44:23 +08:00
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> e_query_t;
|
|
|
|
vector<Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> > e_trainCollection(trainDescCollection.size());
|
|
|
|
vector<Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> > e_trainNorms2(trainDescCollection.size());
|
2010-11-23 02:27:08 +08:00
|
|
|
cv2eigen( queryDescriptors.t(), e_query_t);
|
2010-10-29 16:44:42 +08:00
|
|
|
for( size_t i = 0; i < trainDescCollection.size(); i++ )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
cv2eigen( trainDescCollection[i], e_trainCollection[i] );
|
|
|
|
e_trainNorms2[i] = e_trainCollection[i].rowwise().squaredNorm() / 2;
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
2010-10-29 16:44:42 +08:00
|
|
|
|
|
|
|
vector<Eigen::Matrix<float, Eigen::Dynamic, 1> > e_allDists( imgCount ); // distances between one query descriptor and all train descriptors
|
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
for( int qIdx = 0; qIdx < queryDescriptors.rows; qIdx++ )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-11-23 17:00:32 +08:00
|
|
|
if( isMaskedOut( masks, qIdx ) )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
if( !compactResult ) // push empty vector
|
|
|
|
matches.push_back( vector<DMatch>() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
float queryNorm2 = e_query_t.col(qIdx).squaredNorm();
|
|
|
|
// 1. compute distances between i-th query descriptor and all train descriptors
|
|
|
|
for( size_t iIdx = 0; iIdx < imgCount; iIdx++ )
|
|
|
|
{
|
|
|
|
CV_Assert( masks.empty() || masks[iIdx].empty() ||
|
2010-11-23 02:27:08 +08:00
|
|
|
( masks[iIdx].rows == queryDescriptors.rows && masks[iIdx].cols == trainDescCollection[iIdx].rows &&
|
2010-10-29 16:44:42 +08:00
|
|
|
masks[iIdx].type() == CV_8UC1 ) );
|
|
|
|
CV_Assert( trainDescCollection[iIdx].type() == CV_32FC1 || trainDescCollection[iIdx].empty() );
|
2010-11-23 02:27:08 +08:00
|
|
|
CV_Assert( queryDescriptors.cols == trainDescCollection[iIdx].cols );
|
2010-10-29 16:44:42 +08:00
|
|
|
|
|
|
|
e_allDists[iIdx] = e_trainCollection[iIdx] *e_query_t.col(qIdx);
|
|
|
|
e_allDists[iIdx] -= e_trainNorms2[iIdx];
|
|
|
|
}
|
2010-09-23 21:44:23 +08:00
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
matches.push_back( vector<DMatch>() );
|
|
|
|
vector<vector<DMatch> >::reverse_iterator curMatches = matches.rbegin();
|
|
|
|
for( size_t iIdx = 0; iIdx < imgCount; iIdx++ )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
assert( e_allDists[iIdx].rows() == trainDescCollection[iIdx].rows );
|
|
|
|
for( int tIdx = 0; tIdx < e_allDists[iIdx].rows(); tIdx++ )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-11-23 17:00:32 +08:00
|
|
|
if( masks.empty() || isPossibleMatch(masks[iIdx], qIdx, tIdx) )
|
2010-10-29 16:44:42 +08:00
|
|
|
{
|
|
|
|
float d = sqrt((-2)*e_allDists[iIdx](tIdx) + queryNorm2);
|
|
|
|
if( d < maxDistance )
|
|
|
|
curMatches->push_back( DMatch( qIdx, tIdx, iIdx, d ) );
|
|
|
|
}
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
}
|
2010-10-29 16:44:42 +08:00
|
|
|
std::sort( curMatches->begin(), curMatches->end() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Flann based matcher
|
|
|
|
*/
|
|
|
|
FlannBasedMatcher::FlannBasedMatcher( const Ptr<flann::IndexParams>& _indexParams, const Ptr<flann::SearchParams>& _searchParams )
|
|
|
|
: indexParams(_indexParams), searchParams(_searchParams), addedDescCount(0)
|
|
|
|
{
|
|
|
|
CV_Assert( !_indexParams.empty() );
|
|
|
|
CV_Assert( !_searchParams.empty() );
|
|
|
|
}
|
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
void FlannBasedMatcher::add( const vector<Mat>& descriptors )
|
2010-10-29 16:44:42 +08:00
|
|
|
{
|
2010-11-23 02:27:08 +08:00
|
|
|
DescriptorMatcher::add( descriptors );
|
|
|
|
for( size_t i = 0; i < descriptors.size(); i++ )
|
2010-10-29 16:44:42 +08:00
|
|
|
{
|
2010-11-23 02:27:08 +08:00
|
|
|
addedDescCount += descriptors[i].rows;
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlannBasedMatcher::clear()
|
|
|
|
{
|
|
|
|
DescriptorMatcher::clear();
|
|
|
|
|
|
|
|
mergedDescriptors.clear();
|
|
|
|
flannIndex.release();
|
|
|
|
|
|
|
|
addedDescCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlannBasedMatcher::train()
|
|
|
|
{
|
|
|
|
if( flannIndex.empty() || mergedDescriptors.size() < addedDescCount )
|
|
|
|
{
|
|
|
|
mergedDescriptors.set( trainDescCollection );
|
|
|
|
flannIndex = new flann::Index( mergedDescriptors.getDescriptors(), *indexParams );
|
|
|
|
}
|
|
|
|
}
|
2010-09-23 21:44:23 +08:00
|
|
|
|
2011-08-04 22:13:07 +08:00
|
|
|
void FlannBasedMatcher::read( const FileNode& fn)
|
|
|
|
{
|
|
|
|
if (indexParams == 0)
|
|
|
|
indexParams = new flann::IndexParams();
|
|
|
|
|
|
|
|
FileNode ip = fn["indexParams"];
|
|
|
|
CV_Assert(ip.type() == FileNode::SEQ);
|
|
|
|
|
|
|
|
for(size_t i = 0; i < ip.size(); ++i)
|
|
|
|
{
|
|
|
|
CV_Assert(ip[i].type() == FileNode::MAP);
|
|
|
|
std::string name = (std::string)ip[i]["name"];
|
|
|
|
int type = (int)ip[i]["type"];
|
|
|
|
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case CV_8U:
|
|
|
|
case CV_8S:
|
|
|
|
case CV_16U:
|
|
|
|
case CV_16S:
|
|
|
|
case CV_32S:
|
|
|
|
indexParams->setInt(name, (int) ip[i]["value"]);
|
|
|
|
break;
|
|
|
|
case CV_32F:
|
|
|
|
indexParams->setFloat(name, (float) ip[i]["value"]);
|
|
|
|
break;
|
|
|
|
case CV_64F:
|
|
|
|
indexParams->setDouble(name, (double) ip[i]["value"]);
|
|
|
|
break;
|
|
|
|
case CV_USRTYPE1:
|
|
|
|
indexParams->setString(name, (std::string) ip[i]["value"]);
|
|
|
|
break;
|
|
|
|
case CV_MAKETYPE(CV_USRTYPE1,2):
|
|
|
|
indexParams->setBool(name, (int) ip[i]["value"]);
|
|
|
|
break;
|
|
|
|
case CV_MAKETYPE(CV_USRTYPE1,3):
|
|
|
|
indexParams->setAlgorithm(name, (int) ip[i]["value"]);
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (searchParams == 0)
|
|
|
|
searchParams = new flann::SearchParams();
|
|
|
|
|
|
|
|
FileNode sp = fn["searchParams"];
|
|
|
|
CV_Assert(sp.type() == FileNode::SEQ);
|
|
|
|
|
|
|
|
for(size_t i = 0; i < sp.size(); ++i)
|
|
|
|
{
|
|
|
|
CV_Assert(sp[i].type() == FileNode::MAP);
|
|
|
|
std::string name = (std::string)sp[i]["name"];
|
|
|
|
int type = (int)sp[i]["type"];
|
|
|
|
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case CV_8U:
|
|
|
|
case CV_8S:
|
|
|
|
case CV_16U:
|
|
|
|
case CV_16S:
|
|
|
|
case CV_32S:
|
|
|
|
searchParams->setInt(name, (int) sp[i]["value"]);
|
|
|
|
break;
|
|
|
|
case CV_32F:
|
|
|
|
searchParams->setFloat(name, (float) ip[i]["value"]);
|
|
|
|
break;
|
|
|
|
case CV_64F:
|
|
|
|
searchParams->setDouble(name, (double) ip[i]["value"]);
|
|
|
|
break;
|
|
|
|
case CV_USRTYPE1:
|
|
|
|
searchParams->setString(name, (std::string) ip[i]["value"]);
|
|
|
|
break;
|
|
|
|
case CV_MAKETYPE(CV_USRTYPE1,2):
|
|
|
|
searchParams->setBool(name, (int) ip[i]["value"]);
|
|
|
|
break;
|
|
|
|
case CV_MAKETYPE(CV_USRTYPE1,3):
|
|
|
|
searchParams->setAlgorithm(name, (int) ip[i]["value"]);
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
flannIndex.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlannBasedMatcher::write( FileStorage& fs) const
|
|
|
|
{
|
|
|
|
fs << "indexParams" << "[";
|
|
|
|
|
|
|
|
if (indexParams != 0)
|
|
|
|
{
|
|
|
|
std::vector<std::string> names;
|
|
|
|
std::vector<int> types;
|
|
|
|
std::vector<std::string> strValues;
|
|
|
|
std::vector<double> numValues;
|
|
|
|
|
|
|
|
indexParams->getAll(names, types, strValues, numValues);
|
|
|
|
|
|
|
|
for(size_t i = 0; i < names.size(); ++i)
|
|
|
|
{
|
|
|
|
fs << "{" << "name" << names[i] << "type" << types[i] << "value";
|
|
|
|
switch(types[i])
|
|
|
|
{
|
|
|
|
case CV_8U:
|
|
|
|
fs << (uchar)numValues[i];
|
|
|
|
break;
|
|
|
|
case CV_8S:
|
|
|
|
fs << (char)numValues[i];
|
|
|
|
break;
|
|
|
|
case CV_16U:
|
|
|
|
fs << (ushort)numValues[i];
|
|
|
|
break;
|
|
|
|
case CV_16S:
|
|
|
|
fs << (short)numValues[i];
|
|
|
|
break;
|
|
|
|
case CV_32S:
|
|
|
|
case CV_MAKETYPE(CV_USRTYPE1,2):
|
|
|
|
case CV_MAKETYPE(CV_USRTYPE1,3):
|
|
|
|
fs << (int)numValues[i];
|
|
|
|
break;
|
|
|
|
case CV_32F:
|
|
|
|
fs << (float)numValues[i];
|
|
|
|
break;
|
|
|
|
case CV_64F:
|
|
|
|
fs << (double)numValues[i];
|
|
|
|
break;
|
|
|
|
case CV_USRTYPE1:
|
|
|
|
fs << strValues[i];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fs << (double)numValues[i];
|
|
|
|
fs << "typename" << strValues[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fs << "}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fs << "]" << "searchParams" << "[";
|
|
|
|
|
|
|
|
if (searchParams != 0)
|
|
|
|
{
|
|
|
|
std::vector<std::string> names;
|
|
|
|
std::vector<int> types;
|
|
|
|
std::vector<std::string> strValues;
|
|
|
|
std::vector<double> numValues;
|
|
|
|
|
|
|
|
searchParams->getAll(names, types, strValues, numValues);
|
|
|
|
|
|
|
|
for(size_t i = 0; i < names.size(); ++i)
|
|
|
|
{
|
|
|
|
fs << "{" << "name" << names[i] << "type" << types[i] << "value";
|
|
|
|
switch(types[i])
|
|
|
|
{
|
|
|
|
case CV_8U:
|
|
|
|
fs << (uchar)numValues[i];
|
|
|
|
break;
|
|
|
|
case CV_8S:
|
|
|
|
fs << (char)numValues[i];
|
|
|
|
break;
|
|
|
|
case CV_16U:
|
|
|
|
fs << (ushort)numValues[i];
|
|
|
|
break;
|
|
|
|
case CV_16S:
|
|
|
|
fs << (short)numValues[i];
|
|
|
|
break;
|
|
|
|
case CV_32S:
|
|
|
|
case CV_MAKETYPE(CV_USRTYPE1,2):
|
|
|
|
case CV_MAKETYPE(CV_USRTYPE1,3):
|
|
|
|
fs << (int)numValues[i];
|
|
|
|
break;
|
|
|
|
case CV_32F:
|
|
|
|
fs << (float)numValues[i];
|
|
|
|
break;
|
|
|
|
case CV_64F:
|
|
|
|
fs << (double)numValues[i];
|
|
|
|
break;
|
|
|
|
case CV_USRTYPE1:
|
|
|
|
fs << strValues[i];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fs << (double)numValues[i];
|
|
|
|
fs << "typename" << strValues[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fs << "}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fs << "]";
|
|
|
|
}
|
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
bool FlannBasedMatcher::isMaskSupported() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ptr<DescriptorMatcher> FlannBasedMatcher::clone( bool emptyTrainData ) const
|
|
|
|
{
|
|
|
|
FlannBasedMatcher* matcher = new FlannBasedMatcher(indexParams, searchParams);
|
|
|
|
if( !emptyTrainData )
|
|
|
|
{
|
|
|
|
CV_Error( CV_StsNotImplemented, "deep clone functionality is not implemented, because "
|
|
|
|
"Flann::Index has not copy constructor or clone method ");
|
|
|
|
//matcher->flannIndex;
|
|
|
|
matcher->addedDescCount = addedDescCount;
|
|
|
|
matcher->mergedDescriptors = DescriptorCollection( mergedDescriptors );
|
2011-01-12 14:39:08 +08:00
|
|
|
std::transform( trainDescCollection.begin(), trainDescCollection.end(),
|
|
|
|
matcher->trainDescCollection.begin(), clone_op );
|
2010-11-23 02:27:08 +08:00
|
|
|
}
|
|
|
|
return matcher;
|
|
|
|
}
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
void FlannBasedMatcher::convertToDMatches( const DescriptorCollection& collection, const Mat& indices, const Mat& dists,
|
|
|
|
vector<vector<DMatch> >& matches )
|
|
|
|
{
|
|
|
|
matches.resize( indices.rows );
|
|
|
|
for( int i = 0; i < indices.rows; i++ )
|
|
|
|
{
|
|
|
|
for( int j = 0; j < indices.cols; j++ )
|
|
|
|
{
|
|
|
|
int idx = indices.at<int>(i, j);
|
|
|
|
if( idx >= 0 )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
int imgIdx, trainIdx;
|
|
|
|
collection.getLocalIdx( idx, imgIdx, trainIdx );
|
|
|
|
matches[i].push_back( DMatch( i, trainIdx, imgIdx, std::sqrt(dists.at<float>(i,j))) );
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
void FlannBasedMatcher::knnMatchImpl( const Mat& queryDescriptors, vector<vector<DMatch> >& matches, int knn,
|
2010-10-29 16:44:42 +08:00
|
|
|
const vector<Mat>& /*masks*/, bool /*compactResult*/ )
|
|
|
|
{
|
2010-11-23 02:27:08 +08:00
|
|
|
Mat indices( queryDescriptors.rows, knn, CV_32SC1 );
|
|
|
|
Mat dists( queryDescriptors.rows, knn, CV_32FC1);
|
|
|
|
flannIndex->knnSearch( queryDescriptors, indices, dists, knn, *searchParams );
|
2010-10-29 16:44:42 +08:00
|
|
|
|
|
|
|
convertToDMatches( mergedDescriptors, indices, dists, matches );
|
|
|
|
}
|
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
void FlannBasedMatcher::radiusMatchImpl( const Mat& queryDescriptors, vector<vector<DMatch> >& matches, float maxDistance,
|
2010-10-29 16:44:42 +08:00
|
|
|
const vector<Mat>& /*masks*/, bool /*compactResult*/ )
|
|
|
|
{
|
|
|
|
const int count = mergedDescriptors.size(); // TODO do count as param?
|
2010-11-23 02:27:08 +08:00
|
|
|
Mat indices( queryDescriptors.rows, count, CV_32SC1, Scalar::all(-1) );
|
|
|
|
Mat dists( queryDescriptors.rows, count, CV_32FC1, Scalar::all(-1) );
|
|
|
|
for( int qIdx = 0; qIdx < queryDescriptors.rows; qIdx++ )
|
2010-10-29 16:44:42 +08:00
|
|
|
{
|
2010-11-23 02:27:08 +08:00
|
|
|
Mat queryDescriptorsRow = queryDescriptors.row(qIdx);
|
2010-10-29 16:44:42 +08:00
|
|
|
Mat indicesRow = indices.row(qIdx);
|
|
|
|
Mat distsRow = dists.row(qIdx);
|
2011-07-14 07:04:39 +08:00
|
|
|
flannIndex->radiusSearch( queryDescriptorsRow, indicesRow, distsRow, maxDistance*maxDistance, count, *searchParams );
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
convertToDMatches( mergedDescriptors, indices, dists, matches );
|
|
|
|
}
|
|
|
|
|
2010-09-23 21:44:23 +08:00
|
|
|
/****************************************************************************************\
|
2010-10-29 16:44:42 +08:00
|
|
|
* GenericDescriptorMatcher *
|
2010-09-23 21:44:23 +08:00
|
|
|
\****************************************************************************************/
|
|
|
|
/*
|
|
|
|
* KeyPointCollection
|
|
|
|
*/
|
2010-11-23 02:27:08 +08:00
|
|
|
GenericDescriptorMatcher::KeyPointCollection::KeyPointCollection() : pointCount(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
GenericDescriptorMatcher::KeyPointCollection::KeyPointCollection( const KeyPointCollection& collection )
|
|
|
|
{
|
|
|
|
pointCount = collection.pointCount;
|
|
|
|
|
2011-01-12 14:39:08 +08:00
|
|
|
std::transform( collection.images.begin(), collection.images.end(), images.begin(), clone_op );
|
2010-11-23 02:27:08 +08:00
|
|
|
|
|
|
|
keypoints.resize( collection.keypoints.size() );
|
|
|
|
for( size_t i = 0; i < keypoints.size(); i++ )
|
|
|
|
copy( collection.keypoints[i].begin(), collection.keypoints[i].end(), keypoints[i].begin() );
|
|
|
|
|
|
|
|
copy( collection.startIndices.begin(), collection.startIndices.end(), startIndices.begin() );
|
|
|
|
}
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
void GenericDescriptorMatcher::KeyPointCollection::add( const vector<Mat>& _images,
|
|
|
|
const vector<vector<KeyPoint> >& _points )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
CV_Assert( !_images.empty() );
|
|
|
|
CV_Assert( _images.size() == _points.size() );
|
|
|
|
|
|
|
|
images.insert( images.end(), _images.begin(), _images.end() );
|
2010-11-23 02:27:08 +08:00
|
|
|
keypoints.insert( keypoints.end(), _points.begin(), _points.end() );
|
2010-10-29 16:44:42 +08:00
|
|
|
for( size_t i = 0; i < _points.size(); i++ )
|
2010-11-26 00:55:46 +08:00
|
|
|
pointCount += (int)_points[i].size();
|
2010-10-29 16:44:42 +08:00
|
|
|
|
|
|
|
size_t prevSize = startIndices.size(), addSize = _images.size();
|
|
|
|
startIndices.resize( prevSize + addSize );
|
|
|
|
|
|
|
|
if( prevSize == 0 )
|
|
|
|
startIndices[prevSize] = 0; //first
|
2010-09-23 21:44:23 +08:00
|
|
|
else
|
2010-11-26 00:55:46 +08:00
|
|
|
startIndices[prevSize] = (int)(startIndices[prevSize-1] + keypoints[prevSize-1].size());
|
2010-09-23 21:44:23 +08:00
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
for( size_t i = prevSize + 1; i < prevSize + addSize; i++ )
|
|
|
|
{
|
2010-11-26 00:55:46 +08:00
|
|
|
startIndices[i] = (int)(startIndices[i - 1] + keypoints[i - 1].size());
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
void GenericDescriptorMatcher::KeyPointCollection::clear()
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2011-04-30 00:42:38 +08:00
|
|
|
pointCount = 0;
|
|
|
|
|
|
|
|
images.clear();
|
2010-11-23 02:27:08 +08:00
|
|
|
keypoints.clear();
|
2011-04-30 00:42:38 +08:00
|
|
|
startIndices.clear();
|
2010-11-23 02:27:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t GenericDescriptorMatcher::KeyPointCollection::keypointCount() const
|
|
|
|
{
|
|
|
|
return pointCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t GenericDescriptorMatcher::KeyPointCollection::imageCount() const
|
|
|
|
{
|
|
|
|
return images.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
const vector<vector<KeyPoint> >& GenericDescriptorMatcher::KeyPointCollection::getKeypoints() const
|
|
|
|
{
|
|
|
|
return keypoints;
|
|
|
|
}
|
|
|
|
|
|
|
|
const vector<KeyPoint>& GenericDescriptorMatcher::KeyPointCollection::getKeypoints( int imgIdx ) const
|
|
|
|
{
|
|
|
|
CV_Assert( imgIdx < (int)imageCount() );
|
|
|
|
return keypoints[imgIdx];
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
2010-09-23 21:44:23 +08:00
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
const KeyPoint& GenericDescriptorMatcher::KeyPointCollection::getKeyPoint( int imgIdx, int localPointIdx ) const
|
|
|
|
{
|
|
|
|
CV_Assert( imgIdx < (int)images.size() );
|
2010-11-23 02:27:08 +08:00
|
|
|
CV_Assert( localPointIdx < (int)keypoints[imgIdx].size() );
|
|
|
|
return keypoints[imgIdx][localPointIdx];
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
const KeyPoint& GenericDescriptorMatcher::KeyPointCollection::getKeyPoint( int globalPointIdx ) const
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
int imgIdx, localPointIdx;
|
|
|
|
getLocalIdx( globalPointIdx, imgIdx, localPointIdx );
|
2010-11-23 02:27:08 +08:00
|
|
|
return keypoints[imgIdx][localPointIdx];
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
void GenericDescriptorMatcher::KeyPointCollection::getLocalIdx( int globalPointIdx, int& imgIdx, int& localPointIdx ) const
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
imgIdx = -1;
|
2010-11-23 02:27:08 +08:00
|
|
|
CV_Assert( globalPointIdx < (int)keypointCount() );
|
2010-10-29 16:44:42 +08:00
|
|
|
for( size_t i = 1; i < startIndices.size(); i++ )
|
|
|
|
{
|
|
|
|
if( globalPointIdx < startIndices[i] )
|
|
|
|
{
|
2010-11-26 00:55:46 +08:00
|
|
|
imgIdx = (int)(i - 1);
|
2010-10-29 16:44:42 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-11-26 00:55:46 +08:00
|
|
|
imgIdx = imgIdx == -1 ? (int)(startIndices.size() - 1) : imgIdx;
|
2010-10-29 16:44:42 +08:00
|
|
|
localPointIdx = globalPointIdx - startIndices[imgIdx];
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
const vector<Mat>& GenericDescriptorMatcher::KeyPointCollection::getImages() const
|
|
|
|
{
|
|
|
|
return images;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Mat& GenericDescriptorMatcher::KeyPointCollection::getImage( int imgIdx ) const
|
|
|
|
{
|
|
|
|
CV_Assert( imgIdx < (int)imageCount() );
|
|
|
|
return images[imgIdx];
|
|
|
|
}
|
|
|
|
|
2010-09-23 21:44:23 +08:00
|
|
|
/*
|
2010-10-29 16:44:42 +08:00
|
|
|
* GenericDescriptorMatcher
|
2010-09-23 21:44:23 +08:00
|
|
|
*/
|
2010-11-23 02:27:08 +08:00
|
|
|
GenericDescriptorMatcher::GenericDescriptorMatcher()
|
|
|
|
{}
|
|
|
|
|
|
|
|
GenericDescriptorMatcher::~GenericDescriptorMatcher()
|
|
|
|
{}
|
|
|
|
|
2011-05-02 01:38:52 +08:00
|
|
|
void GenericDescriptorMatcher::add( const vector<Mat>& images,
|
|
|
|
vector<vector<KeyPoint> >& keypoints )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2011-05-02 01:38:52 +08:00
|
|
|
CV_Assert( !images.empty() );
|
|
|
|
CV_Assert( images.size() == keypoints.size() );
|
|
|
|
|
|
|
|
for( size_t i = 0; i < images.size(); i++ )
|
|
|
|
{
|
|
|
|
CV_Assert( !images[i].empty() );
|
|
|
|
KeyPointsFilter::runByImageBorder( keypoints[i], images[i].size(), 0 );
|
|
|
|
KeyPointsFilter::runByKeypointSize( keypoints[i], std::numeric_limits<float>::epsilon() );
|
|
|
|
}
|
|
|
|
|
|
|
|
trainPointCollection.add( images, keypoints );
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
const vector<Mat>& GenericDescriptorMatcher::getTrainImages() const
|
|
|
|
{
|
|
|
|
return trainPointCollection.getImages();
|
|
|
|
}
|
|
|
|
|
|
|
|
const vector<vector<KeyPoint> >& GenericDescriptorMatcher::getTrainKeypoints() const
|
|
|
|
{
|
|
|
|
return trainPointCollection.getKeypoints();
|
|
|
|
}
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
void GenericDescriptorMatcher::clear()
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
trainPointCollection.clear();
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
void GenericDescriptorMatcher::train()
|
|
|
|
{}
|
|
|
|
|
2010-11-24 01:00:55 +08:00
|
|
|
void GenericDescriptorMatcher::classify( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
|
|
|
|
const Mat& trainImage, vector<KeyPoint>& trainKeypoints ) const
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
vector<DMatch> matches;
|
2010-11-24 01:00:55 +08:00
|
|
|
match( queryImage, queryKeypoints, trainImage, trainKeypoints, matches );
|
2010-10-29 16:44:42 +08:00
|
|
|
|
|
|
|
// remap keypoint indices to descriptors
|
|
|
|
for( size_t i = 0; i < matches.size(); i++ )
|
2010-11-24 01:00:55 +08:00
|
|
|
queryKeypoints[matches[i].queryIdx].class_id = trainKeypoints[matches[i].trainIdx].class_id;
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
|
2010-11-24 01:00:55 +08:00
|
|
|
void GenericDescriptorMatcher::classify( const Mat& queryImage, vector<KeyPoint>& queryKeypoints )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
vector<DMatch> matches;
|
2010-11-24 01:00:55 +08:00
|
|
|
match( queryImage, queryKeypoints, matches );
|
2010-09-23 21:44:23 +08:00
|
|
|
|
|
|
|
// remap keypoint indices to descriptors
|
2010-10-29 16:44:42 +08:00
|
|
|
for( size_t i = 0; i < matches.size(); i++ )
|
2010-11-24 01:00:55 +08:00
|
|
|
queryKeypoints[matches[i].queryIdx].class_id = trainPointCollection.getKeyPoint( matches[i].trainIdx, matches[i].trainIdx ).class_id;
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
|
|
|
|
2010-11-24 01:00:55 +08:00
|
|
|
void GenericDescriptorMatcher::match( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
|
|
|
|
const Mat& trainImage, vector<KeyPoint>& trainKeypoints,
|
2010-10-29 16:44:42 +08:00
|
|
|
vector<DMatch>& matches, const Mat& mask ) const
|
|
|
|
{
|
2010-11-23 02:27:08 +08:00
|
|
|
Ptr<GenericDescriptorMatcher> tempMatcher = clone( true );
|
2010-11-24 01:00:55 +08:00
|
|
|
vector<vector<KeyPoint> > vecTrainPoints(1, trainKeypoints);
|
|
|
|
tempMatcher->add( vector<Mat>(1, trainImage), vecTrainPoints );
|
|
|
|
tempMatcher->match( queryImage, queryKeypoints, matches, vector<Mat>(1, mask) );
|
|
|
|
vecTrainPoints[0].swap( trainKeypoints );
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
|
|
|
|
2010-11-24 01:00:55 +08:00
|
|
|
void GenericDescriptorMatcher::knnMatch( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
|
|
|
|
const Mat& trainImage, vector<KeyPoint>& trainKeypoints,
|
2010-10-29 16:44:42 +08:00
|
|
|
vector<vector<DMatch> >& matches, int knn, const Mat& mask, bool compactResult ) const
|
|
|
|
{
|
2010-11-23 02:27:08 +08:00
|
|
|
Ptr<GenericDescriptorMatcher> tempMatcher = clone( true );
|
2010-11-24 01:00:55 +08:00
|
|
|
vector<vector<KeyPoint> > vecTrainPoints(1, trainKeypoints);
|
|
|
|
tempMatcher->add( vector<Mat>(1, trainImage), vecTrainPoints );
|
|
|
|
tempMatcher->knnMatch( queryImage, queryKeypoints, matches, knn, vector<Mat>(1, mask), compactResult );
|
|
|
|
vecTrainPoints[0].swap( trainKeypoints );
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
2010-09-23 21:44:23 +08:00
|
|
|
|
2010-11-24 01:00:55 +08:00
|
|
|
void GenericDescriptorMatcher::radiusMatch( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
|
|
|
|
const Mat& trainImage, vector<KeyPoint>& trainKeypoints,
|
2010-10-29 16:44:42 +08:00
|
|
|
vector<vector<DMatch> >& matches, float maxDistance,
|
|
|
|
const Mat& mask, bool compactResult ) const
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-11-23 02:27:08 +08:00
|
|
|
Ptr<GenericDescriptorMatcher> tempMatcher = clone( true );
|
2010-11-24 01:00:55 +08:00
|
|
|
vector<vector<KeyPoint> > vecTrainPoints(1, trainKeypoints);
|
|
|
|
tempMatcher->add( vector<Mat>(1, trainImage), vecTrainPoints );
|
|
|
|
tempMatcher->radiusMatch( queryImage, queryKeypoints, matches, maxDistance, vector<Mat>(1, mask), compactResult );
|
|
|
|
vecTrainPoints[0].swap( trainKeypoints );
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
|
|
|
|
2010-11-24 01:00:55 +08:00
|
|
|
void GenericDescriptorMatcher::match( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
|
2010-10-29 16:44:42 +08:00
|
|
|
vector<DMatch>& matches, const vector<Mat>& masks )
|
|
|
|
{
|
|
|
|
vector<vector<DMatch> > knnMatches;
|
2010-11-24 01:00:55 +08:00
|
|
|
knnMatch( queryImage, queryKeypoints, knnMatches, 1, masks, false );
|
2010-10-29 16:44:42 +08:00
|
|
|
convertMatches( knnMatches, matches );
|
|
|
|
}
|
|
|
|
|
2010-11-24 01:00:55 +08:00
|
|
|
void GenericDescriptorMatcher::knnMatch( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
|
2010-10-29 16:44:42 +08:00
|
|
|
vector<vector<DMatch> >& matches, int knn,
|
|
|
|
const vector<Mat>& masks, bool compactResult )
|
|
|
|
{
|
2011-05-02 01:38:52 +08:00
|
|
|
matches.clear();
|
|
|
|
|
|
|
|
if( queryImage.empty() || queryKeypoints.empty() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
KeyPointsFilter::runByImageBorder( queryKeypoints, queryImage.size(), 0 );
|
|
|
|
KeyPointsFilter::runByKeypointSize( queryKeypoints, std::numeric_limits<float>::epsilon() );
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
train();
|
2010-11-24 01:00:55 +08:00
|
|
|
knnMatchImpl( queryImage, queryKeypoints, matches, knn, masks, compactResult );
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
|
|
|
|
2010-11-24 01:00:55 +08:00
|
|
|
void GenericDescriptorMatcher::radiusMatch( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
|
2010-10-29 16:44:42 +08:00
|
|
|
vector<vector<DMatch> >& matches, float maxDistance,
|
|
|
|
const vector<Mat>& masks, bool compactResult )
|
|
|
|
{
|
2011-05-02 01:38:52 +08:00
|
|
|
matches.clear();
|
|
|
|
|
|
|
|
if( queryImage.empty() || queryKeypoints.empty() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
KeyPointsFilter::runByImageBorder( queryKeypoints, queryImage.size(), 0 );
|
|
|
|
KeyPointsFilter::runByKeypointSize( queryKeypoints, std::numeric_limits<float>::epsilon() );
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
train();
|
2010-11-24 01:00:55 +08:00
|
|
|
radiusMatchImpl( queryImage, queryKeypoints, matches, maxDistance, masks, compactResult );
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
void GenericDescriptorMatcher::read( const FileNode& )
|
|
|
|
{}
|
|
|
|
|
|
|
|
void GenericDescriptorMatcher::write( FileStorage& ) const
|
|
|
|
{}
|
|
|
|
|
2011-01-31 22:18:50 +08:00
|
|
|
bool GenericDescriptorMatcher::empty() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-11-25 23:59:37 +08:00
|
|
|
/*
|
|
|
|
* Factory function for GenericDescriptorMatch creating
|
|
|
|
*/
|
|
|
|
Ptr<GenericDescriptorMatcher> GenericDescriptorMatcher::create( const string& genericDescritptorMatcherType,
|
|
|
|
const string ¶msFilename )
|
|
|
|
{
|
|
|
|
Ptr<GenericDescriptorMatcher> descriptorMatcher;
|
|
|
|
if( ! genericDescritptorMatcherType.compare("ONEWAY") )
|
|
|
|
{
|
|
|
|
descriptorMatcher = new OneWayDescriptorMatcher();
|
|
|
|
}
|
|
|
|
else if( ! genericDescritptorMatcherType.compare("FERN") )
|
|
|
|
{
|
|
|
|
descriptorMatcher = new FernDescriptorMatcher();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !paramsFilename.empty() && !descriptorMatcher.empty() )
|
|
|
|
{
|
|
|
|
FileStorage fs = FileStorage( paramsFilename, FileStorage::READ );
|
|
|
|
if( fs.isOpened() )
|
|
|
|
{
|
|
|
|
descriptorMatcher->read( fs.root() );
|
|
|
|
fs.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return descriptorMatcher;
|
|
|
|
}
|
|
|
|
|
2010-09-23 21:44:23 +08:00
|
|
|
/****************************************************************************************\
|
2010-10-29 16:44:42 +08:00
|
|
|
* OneWayDescriptorMatcher *
|
2010-09-23 21:44:23 +08:00
|
|
|
\****************************************************************************************/
|
2010-11-23 02:27:08 +08:00
|
|
|
|
|
|
|
OneWayDescriptorMatcher::Params::Params( int _poseCount, Size _patchSize, string _pcaFilename,
|
|
|
|
string _trainPath, string _trainImagesList,
|
|
|
|
float _minScale, float _maxScale, float _stepScale ) :
|
|
|
|
poseCount(_poseCount), patchSize(_patchSize), pcaFilename(_pcaFilename),
|
|
|
|
trainPath(_trainPath), trainImagesList(_trainImagesList),
|
|
|
|
minScale(_minScale), maxScale(_maxScale), stepScale(_stepScale)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
OneWayDescriptorMatcher::OneWayDescriptorMatcher( const Params& _params)
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
|
|
|
initialize(_params);
|
|
|
|
}
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
OneWayDescriptorMatcher::~OneWayDescriptorMatcher()
|
2010-09-23 21:44:23 +08:00
|
|
|
{}
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
void OneWayDescriptorMatcher::initialize( const Params& _params, const Ptr<OneWayDescriptorBase>& _base )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
clear();
|
|
|
|
|
|
|
|
if( _base.empty() )
|
2010-09-23 21:44:23 +08:00
|
|
|
base = _base;
|
2010-10-29 16:44:42 +08:00
|
|
|
|
2010-09-23 21:44:23 +08:00
|
|
|
params = _params;
|
|
|
|
}
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
void OneWayDescriptorMatcher::clear()
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
GenericDescriptorMatcher::clear();
|
2010-09-23 21:44:23 +08:00
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
prevTrainCount = 0;
|
2011-04-30 01:12:55 +08:00
|
|
|
if( !base.empty() )
|
|
|
|
base->clear();
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
void OneWayDescriptorMatcher::train()
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-11-23 02:27:08 +08:00
|
|
|
if( base.empty() || prevTrainCount < (int)trainPointCollection.keypointCount() )
|
2010-10-29 16:44:42 +08:00
|
|
|
{
|
2010-09-23 21:44:23 +08:00
|
|
|
base = new OneWayDescriptorObject( params.patchSize, params.poseCount, params.pcaFilename,
|
2010-10-29 16:44:42 +08:00
|
|
|
params.trainPath, params.trainImagesList, params.minScale, params.maxScale, params.stepScale );
|
2010-09-23 21:44:23 +08:00
|
|
|
|
2010-11-26 00:55:46 +08:00
|
|
|
base->Allocate( (int)trainPointCollection.keypointCount() );
|
|
|
|
prevTrainCount = (int)trainPointCollection.keypointCount();
|
2010-09-23 21:44:23 +08:00
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
const vector<vector<KeyPoint> >& points = trainPointCollection.getKeypoints();
|
|
|
|
int count = 0;
|
|
|
|
for( size_t i = 0; i < points.size(); i++ )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-11-26 00:55:46 +08:00
|
|
|
IplImage _image = trainPointCollection.getImage((int)i);
|
2010-10-29 16:44:42 +08:00
|
|
|
for( size_t j = 0; j < points[i].size(); j++ )
|
|
|
|
base->InitializeDescriptor( count++, &_image, points[i][j], "" );
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(_KDTREE)
|
2010-10-29 16:44:42 +08:00
|
|
|
base->ConvertDescriptorsArrayToTree();
|
2010-09-23 21:44:23 +08:00
|
|
|
#endif
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
bool OneWayDescriptorMatcher::isMaskSupported()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-11-24 01:00:55 +08:00
|
|
|
void OneWayDescriptorMatcher::knnMatchImpl( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
|
2010-10-29 16:44:42 +08:00
|
|
|
vector<vector<DMatch> >& matches, int knn,
|
|
|
|
const vector<Mat>& /*masks*/, bool /*compactResult*/ )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
train();
|
2010-09-23 21:44:23 +08:00
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
CV_Assert( knn == 1 ); // knn > 1 unsupported because of bug in OneWayDescriptorBase for this case
|
2010-09-23 21:44:23 +08:00
|
|
|
|
2010-11-24 01:00:55 +08:00
|
|
|
matches.resize( queryKeypoints.size() );
|
|
|
|
IplImage _qimage = queryImage;
|
|
|
|
for( size_t i = 0; i < queryKeypoints.size(); i++ )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
int descIdx = -1, poseIdx = -1;
|
|
|
|
float distance;
|
2010-11-24 01:00:55 +08:00
|
|
|
base->FindDescriptor( &_qimage, queryKeypoints[i].pt, descIdx, poseIdx, distance );
|
2010-11-26 00:55:46 +08:00
|
|
|
matches[i].push_back( DMatch((int)i, descIdx, distance) );
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-24 01:00:55 +08:00
|
|
|
void OneWayDescriptorMatcher::radiusMatchImpl( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
|
2010-10-29 16:44:42 +08:00
|
|
|
vector<vector<DMatch> >& matches, float maxDistance,
|
|
|
|
const vector<Mat>& /*masks*/, bool /*compactResult*/ )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
train();
|
2010-09-23 21:44:23 +08:00
|
|
|
|
2010-11-24 01:00:55 +08:00
|
|
|
matches.resize( queryKeypoints.size() );
|
|
|
|
IplImage _qimage = queryImage;
|
|
|
|
for( size_t i = 0; i < queryKeypoints.size(); i++ )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
int descIdx = -1, poseIdx = -1;
|
|
|
|
float distance;
|
2010-11-24 01:00:55 +08:00
|
|
|
base->FindDescriptor( &_qimage, queryKeypoints[i].pt, descIdx, poseIdx, distance );
|
2010-10-29 16:44:42 +08:00
|
|
|
if( distance < maxDistance )
|
2010-11-26 00:55:46 +08:00
|
|
|
matches[i].push_back( DMatch((int)i, descIdx, distance) );
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
void OneWayDescriptorMatcher::read( const FileNode &fn )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
|
|
|
base = new OneWayDescriptorObject( params.patchSize, params.poseCount, string (), string (), string (),
|
|
|
|
params.minScale, params.maxScale, params.stepScale );
|
|
|
|
base->Read (fn);
|
|
|
|
}
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
void OneWayDescriptorMatcher::write( FileStorage& fs ) const
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
|
|
|
base->Write (fs);
|
|
|
|
}
|
|
|
|
|
2011-01-31 22:18:50 +08:00
|
|
|
bool OneWayDescriptorMatcher::empty() const
|
|
|
|
{
|
|
|
|
return base.empty() || base->empty();
|
|
|
|
}
|
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
Ptr<GenericDescriptorMatcher> OneWayDescriptorMatcher::clone( bool emptyTrainData ) const
|
|
|
|
{
|
|
|
|
OneWayDescriptorMatcher* matcher = new OneWayDescriptorMatcher( params );
|
|
|
|
|
|
|
|
if( !emptyTrainData )
|
|
|
|
{
|
2011-01-31 22:18:50 +08:00
|
|
|
CV_Error( CV_StsNotImplemented, "deep clone functionality is not implemented, because "
|
2010-11-23 02:27:08 +08:00
|
|
|
"OneWayDescriptorBase has not copy constructor or clone method ");
|
|
|
|
|
|
|
|
//matcher->base;
|
|
|
|
matcher->params = params;
|
|
|
|
matcher->prevTrainCount = prevTrainCount;
|
|
|
|
matcher->trainPointCollection = trainPointCollection;
|
|
|
|
}
|
|
|
|
return matcher;
|
|
|
|
}
|
|
|
|
|
2010-09-23 21:44:23 +08:00
|
|
|
/****************************************************************************************\
|
2010-10-29 16:44:42 +08:00
|
|
|
* FernDescriptorMatcher *
|
2010-09-23 21:44:23 +08:00
|
|
|
\****************************************************************************************/
|
2010-10-29 16:44:42 +08:00
|
|
|
FernDescriptorMatcher::Params::Params( int _nclasses, int _patchSize, int _signatureSize,
|
2010-09-23 21:44:23 +08:00
|
|
|
int _nstructs, int _structSize, int _nviews, int _compressionMethod,
|
|
|
|
const PatchGenerator& _patchGenerator ) :
|
|
|
|
nclasses(_nclasses), patchSize(_patchSize), signatureSize(_signatureSize),
|
|
|
|
nstructs(_nstructs), structSize(_structSize), nviews(_nviews),
|
|
|
|
compressionMethod(_compressionMethod), patchGenerator(_patchGenerator)
|
|
|
|
{}
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
FernDescriptorMatcher::Params::Params( const string& _filename )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
|
|
|
filename = _filename;
|
|
|
|
}
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
FernDescriptorMatcher::FernDescriptorMatcher( const Params& _params )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
prevTrainCount = 0;
|
2010-09-23 21:44:23 +08:00
|
|
|
params = _params;
|
|
|
|
if( !params.filename.empty() )
|
|
|
|
{
|
|
|
|
classifier = new FernClassifier;
|
|
|
|
FileStorage fs(params.filename, FileStorage::READ);
|
|
|
|
if( fs.isOpened() )
|
|
|
|
classifier->read( fs.getFirstTopLevelNode() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
FernDescriptorMatcher::~FernDescriptorMatcher()
|
|
|
|
{}
|
|
|
|
|
|
|
|
void FernDescriptorMatcher::clear()
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
GenericDescriptorMatcher::clear();
|
|
|
|
|
|
|
|
classifier.release();
|
|
|
|
prevTrainCount = 0;
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
void FernDescriptorMatcher::train()
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-11-23 02:27:08 +08:00
|
|
|
if( classifier.empty() || prevTrainCount < (int)trainPointCollection.keypointCount() )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
|
|
|
assert( params.filename.empty() );
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
vector<vector<Point2f> > points( trainPointCollection.imageCount() );
|
|
|
|
for( size_t imgIdx = 0; imgIdx < trainPointCollection.imageCount(); imgIdx++ )
|
2010-11-26 00:55:46 +08:00
|
|
|
KeyPoint::convert( trainPointCollection.getKeypoints((int)imgIdx), points[imgIdx] );
|
2010-09-23 21:44:23 +08:00
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
classifier = new FernClassifier( points, trainPointCollection.getImages(), vector<vector<int> >(), 0, // each points is a class
|
2010-09-23 21:44:23 +08:00
|
|
|
params.patchSize, params.signatureSize, params.nstructs, params.structSize,
|
|
|
|
params.nviews, params.compressionMethod, params.patchGenerator );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
bool FernDescriptorMatcher::isMaskSupported()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
void FernDescriptorMatcher::calcBestProbAndMatchIdx( const Mat& image, const Point2f& pt,
|
|
|
|
float& bestProb, int& bestMatchIdx, vector<float>& signature )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
|
|
|
(*classifier)( image, pt, signature);
|
|
|
|
|
|
|
|
bestProb = -FLT_MAX;
|
|
|
|
bestMatchIdx = -1;
|
|
|
|
for( int ci = 0; ci < classifier->getClassCount(); ci++ )
|
|
|
|
{
|
|
|
|
if( signature[ci] > bestProb )
|
|
|
|
{
|
|
|
|
bestProb = signature[ci];
|
|
|
|
bestMatchIdx = ci;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-24 01:00:55 +08:00
|
|
|
void FernDescriptorMatcher::knnMatchImpl( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
|
2010-10-29 16:44:42 +08:00
|
|
|
vector<vector<DMatch> >& matches, int knn,
|
|
|
|
const vector<Mat>& /*masks*/, bool /*compactResult*/ )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
train();
|
2010-09-23 21:44:23 +08:00
|
|
|
|
2010-11-24 01:00:55 +08:00
|
|
|
matches.resize( queryKeypoints.size() );
|
2010-09-23 21:44:23 +08:00
|
|
|
vector<float> signature( (size_t)classifier->getClassCount() );
|
|
|
|
|
2010-11-24 01:00:55 +08:00
|
|
|
for( size_t queryIdx = 0; queryIdx < queryKeypoints.size(); queryIdx++ )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-11-24 01:00:55 +08:00
|
|
|
(*classifier)( queryImage, queryKeypoints[queryIdx].pt, signature);
|
2010-09-23 21:44:23 +08:00
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
for( int k = 0; k < knn; k++ )
|
|
|
|
{
|
|
|
|
DMatch bestMatch;
|
2011-01-22 00:07:28 +08:00
|
|
|
size_t best_ci = 0;
|
2011-01-12 20:03:03 +08:00
|
|
|
for( size_t ci = 0; ci < signature.size(); ci++ )
|
2010-10-29 16:44:42 +08:00
|
|
|
{
|
|
|
|
if( -signature[ci] < bestMatch.distance )
|
|
|
|
{
|
|
|
|
int imgIdx = -1, trainIdx = -1;
|
2010-11-26 00:55:46 +08:00
|
|
|
trainPointCollection.getLocalIdx( (int)ci , imgIdx, trainIdx );
|
|
|
|
bestMatch = DMatch( (int)queryIdx, trainIdx, imgIdx, -signature[ci] );
|
2011-01-12 20:03:03 +08:00
|
|
|
best_ci = ci;
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
2010-09-23 21:44:23 +08:00
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
if( bestMatch.trainIdx == -1 )
|
|
|
|
break;
|
2011-01-12 20:46:26 +08:00
|
|
|
signature[best_ci] = -std::numeric_limits<float>::max();
|
2010-10-29 16:44:42 +08:00
|
|
|
matches[queryIdx].push_back( bestMatch );
|
|
|
|
}
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-24 01:00:55 +08:00
|
|
|
void FernDescriptorMatcher::radiusMatchImpl( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
|
2010-10-29 16:44:42 +08:00
|
|
|
vector<vector<DMatch> >& matches, float maxDistance,
|
|
|
|
const vector<Mat>& /*masks*/, bool /*compactResult*/ )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
train();
|
2010-11-24 01:00:55 +08:00
|
|
|
matches.resize( queryKeypoints.size() );
|
2010-09-23 21:44:23 +08:00
|
|
|
vector<float> signature( (size_t)classifier->getClassCount() );
|
|
|
|
|
2010-11-24 01:00:55 +08:00
|
|
|
for( size_t i = 0; i < queryKeypoints.size(); i++ )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-11-24 01:00:55 +08:00
|
|
|
(*classifier)( queryImage, queryKeypoints[i].pt, signature);
|
2010-09-23 21:44:23 +08:00
|
|
|
|
|
|
|
for( int ci = 0; ci < classifier->getClassCount(); ci++ )
|
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
if( -signature[ci] < maxDistance )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
int imgIdx = -1, trainIdx = -1;
|
|
|
|
trainPointCollection.getLocalIdx( ci , imgIdx, trainIdx );
|
2010-11-26 00:55:46 +08:00
|
|
|
matches[i].push_back( DMatch( (int)i, trainIdx, imgIdx, -signature[ci] ) );
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
void FernDescriptorMatcher::read( const FileNode &fn )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
|
|
|
params.nclasses = fn["nclasses"];
|
|
|
|
params.patchSize = fn["patchSize"];
|
|
|
|
params.signatureSize = fn["signatureSize"];
|
|
|
|
params.nstructs = fn["nstructs"];
|
|
|
|
params.structSize = fn["structSize"];
|
|
|
|
params.nviews = fn["nviews"];
|
|
|
|
params.compressionMethod = fn["compressionMethod"];
|
|
|
|
|
|
|
|
//classifier->read(fn);
|
|
|
|
}
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
void FernDescriptorMatcher::write( FileStorage& fs ) const
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
|
|
|
fs << "nclasses" << params.nclasses;
|
|
|
|
fs << "patchSize" << params.patchSize;
|
|
|
|
fs << "signatureSize" << params.signatureSize;
|
|
|
|
fs << "nstructs" << params.nstructs;
|
|
|
|
fs << "structSize" << params.structSize;
|
|
|
|
fs << "nviews" << params.nviews;
|
|
|
|
fs << "compressionMethod" << params.compressionMethod;
|
|
|
|
|
|
|
|
// classifier->write(fs);
|
|
|
|
}
|
|
|
|
|
2011-01-31 22:18:50 +08:00
|
|
|
bool FernDescriptorMatcher::empty() const
|
|
|
|
{
|
|
|
|
return classifier.empty() || classifier->empty();
|
|
|
|
}
|
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
Ptr<GenericDescriptorMatcher> FernDescriptorMatcher::clone( bool emptyTrainData ) const
|
|
|
|
{
|
|
|
|
FernDescriptorMatcher* matcher = new FernDescriptorMatcher( params );
|
|
|
|
if( !emptyTrainData )
|
|
|
|
{
|
|
|
|
CV_Error( CV_StsNotImplemented, "deep clone dunctionality is not implemented, because "
|
|
|
|
"FernClassifier has not copy constructor or clone method ");
|
|
|
|
|
|
|
|
//matcher->classifier;
|
|
|
|
matcher->params = params;
|
|
|
|
matcher->prevTrainCount = prevTrainCount;
|
|
|
|
matcher->trainPointCollection = trainPointCollection;
|
|
|
|
}
|
|
|
|
return matcher;
|
|
|
|
}
|
|
|
|
|
2010-09-23 21:44:23 +08:00
|
|
|
/****************************************************************************************\
|
2010-11-23 02:27:08 +08:00
|
|
|
* VectorDescriptorMatcher *
|
2010-09-23 21:44:23 +08:00
|
|
|
\****************************************************************************************/
|
2010-11-23 02:27:08 +08:00
|
|
|
VectorDescriptorMatcher::VectorDescriptorMatcher( const Ptr<DescriptorExtractor>& _extractor,
|
|
|
|
const Ptr<DescriptorMatcher>& _matcher )
|
|
|
|
: extractor( _extractor ), matcher( _matcher )
|
|
|
|
{
|
|
|
|
CV_Assert( !extractor.empty() && !matcher.empty() );
|
|
|
|
}
|
|
|
|
|
|
|
|
VectorDescriptorMatcher::~VectorDescriptorMatcher()
|
|
|
|
{}
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
void VectorDescriptorMatcher::add( const vector<Mat>& imgCollection,
|
|
|
|
vector<vector<KeyPoint> >& pointCollection )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-11-23 02:27:08 +08:00
|
|
|
vector<Mat> descriptors;
|
|
|
|
extractor->compute( imgCollection, pointCollection, descriptors );
|
2010-09-23 21:44:23 +08:00
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
matcher->add( descriptors );
|
2010-09-23 21:44:23 +08:00
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
trainPointCollection.add( imgCollection, pointCollection );
|
|
|
|
}
|
2010-09-23 21:44:23 +08:00
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
void VectorDescriptorMatcher::clear()
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
//extractor->clear();
|
|
|
|
matcher->clear();
|
|
|
|
GenericDescriptorMatcher::clear();
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
void VectorDescriptorMatcher::train()
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
matcher->train();
|
|
|
|
}
|
2010-09-23 21:44:23 +08:00
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
bool VectorDescriptorMatcher::isMaskSupported()
|
|
|
|
{
|
|
|
|
return matcher->isMaskSupported();
|
|
|
|
}
|
|
|
|
|
2010-11-24 01:00:55 +08:00
|
|
|
void VectorDescriptorMatcher::knnMatchImpl( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
|
2010-10-29 16:44:42 +08:00
|
|
|
vector<vector<DMatch> >& matches, int knn,
|
|
|
|
const vector<Mat>& masks, bool compactResult )
|
|
|
|
{
|
2010-11-23 02:27:08 +08:00
|
|
|
Mat queryDescriptors;
|
2010-11-24 01:00:55 +08:00
|
|
|
extractor->compute( queryImage, queryKeypoints, queryDescriptors );
|
2010-11-23 02:27:08 +08:00
|
|
|
matcher->knnMatch( queryDescriptors, matches, knn, masks, compactResult );
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
|
2010-11-24 01:00:55 +08:00
|
|
|
void VectorDescriptorMatcher::radiusMatchImpl( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
|
2010-10-29 16:44:42 +08:00
|
|
|
vector<vector<DMatch> >& matches, float maxDistance,
|
|
|
|
const vector<Mat>& masks, bool compactResult )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-11-23 02:27:08 +08:00
|
|
|
Mat queryDescriptors;
|
2010-11-24 01:00:55 +08:00
|
|
|
extractor->compute( queryImage, queryKeypoints, queryDescriptors );
|
2010-11-23 02:27:08 +08:00
|
|
|
matcher->radiusMatch( queryDescriptors, matches, maxDistance, masks, compactResult );
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
void VectorDescriptorMatcher::read( const FileNode& fn )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
GenericDescriptorMatcher::read(fn);
|
2010-11-23 02:27:08 +08:00
|
|
|
extractor->read(fn);
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
void VectorDescriptorMatcher::write (FileStorage& fs) const
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2010-10-29 16:44:42 +08:00
|
|
|
GenericDescriptorMatcher::write(fs);
|
2010-09-23 21:44:23 +08:00
|
|
|
extractor->write (fs);
|
|
|
|
}
|
|
|
|
|
2011-01-31 22:18:50 +08:00
|
|
|
bool VectorDescriptorMatcher::empty() const
|
|
|
|
{
|
|
|
|
return extractor.empty() || extractor->empty() ||
|
|
|
|
matcher.empty() || matcher->empty();
|
|
|
|
}
|
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
Ptr<GenericDescriptorMatcher> VectorDescriptorMatcher::clone( bool emptyTrainData ) const
|
|
|
|
{
|
|
|
|
// TODO clone extractor
|
|
|
|
return new VectorDescriptorMatcher( extractor, matcher->clone(emptyTrainData) );
|
|
|
|
}
|
|
|
|
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|