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"
|
2013-05-07 10:45:11 +08:00
|
|
|
#include <limits>
|
2010-09-23 21:44:23 +08:00
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
Mat windowedMatchingMask( const std::vector<KeyPoint>& keypoints1, const std::vector<KeyPoint>& keypoints2,
|
2010-09-23 21:44:23 +08:00
|
|
|
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();
|
2012-12-21 23:58:51 +08:00
|
|
|
std::copy( collection.startIdxs.begin(), collection.startIdxs.begin(), startIdxs.begin() );
|
2010-11-23 02:27:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DescriptorMatcher::DescriptorCollection::~DescriptorCollection()
|
|
|
|
{}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void DescriptorMatcher::DescriptorCollection::set( const std::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
|
|
|
}
|
2013-04-10 19:54:14 +08:00
|
|
|
CV_Assert( dim > 0 );
|
2010-10-29 16:44:42 +08:00
|
|
|
|
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
|
|
|
|
*/
|
2013-02-25 00:14:01 +08:00
|
|
|
static void convertMatches( const std::vector<std::vector<DMatch> >& knnMatches, std::vector<DMatch>& matches )
|
2010-10-29 16:44:42 +08:00
|
|
|
{
|
|
|
|
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()
|
|
|
|
{}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void DescriptorMatcher::add( const std::vector<Mat>& descriptors )
|
2010-11-23 02:27:08 +08:00
|
|
|
{
|
|
|
|
trainDescCollection.insert( trainDescCollection.end(), descriptors.begin(), descriptors.end() );
|
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
const std::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()
|
|
|
|
{}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void DescriptorMatcher::match( const Mat& queryDescriptors, const Mat& trainDescriptors, std::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);
|
2013-02-25 00:14:01 +08:00
|
|
|
tempMatcher->add( std::vector<Mat>(1, trainDescriptors) );
|
|
|
|
tempMatcher->match( queryDescriptors, matches, std::vector<Mat>(1, mask) );
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void DescriptorMatcher::knnMatch( const Mat& queryDescriptors, const Mat& trainDescriptors, std::vector<std::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);
|
2013-02-25 00:14:01 +08:00
|
|
|
tempMatcher->add( std::vector<Mat>(1, trainDescriptors) );
|
|
|
|
tempMatcher->knnMatch( queryDescriptors, matches, knn, std::vector<Mat>(1, mask), compactResult );
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void DescriptorMatcher::radiusMatch( const Mat& queryDescriptors, const Mat& trainDescriptors, std::vector<std::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);
|
2013-02-25 00:14:01 +08:00
|
|
|
tempMatcher->add( std::vector<Mat>(1, trainDescriptors) );
|
|
|
|
tempMatcher->radiusMatch( queryDescriptors, matches, maxDistance, std::vector<Mat>(1, mask), compactResult );
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void DescriptorMatcher::match( const Mat& queryDescriptors, std::vector<DMatch>& matches, const std::vector<Mat>& masks )
|
2010-10-29 16:44:42 +08:00
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<std::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 );
|
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void DescriptorMatcher::checkMasks( const std::vector<Mat>& masks, int queryDescriptorsCount ) const
|
2010-11-23 02:27:08 +08:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void DescriptorMatcher::knnMatch( const Mat& queryDescriptors, std::vector<std::vector<DMatch> >& matches, int knn,
|
|
|
|
const std::vector<Mat>& masks, bool compactResult )
|
2010-10-29 16:44:42 +08:00
|
|
|
{
|
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 );
|
2012-08-15 19:02:20 +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
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void DescriptorMatcher::radiusMatch( const Mat& queryDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance,
|
|
|
|
const std::vector<Mat>& masks, bool compactResult )
|
2010-10-29 16:44:42 +08:00
|
|
|
{
|
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() );
|
2012-08-15 19:02:20 +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);
|
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
bool DescriptorMatcher::isMaskedOut( const std::vector<Mat>& masks, int queryIdx )
|
2010-11-23 02:27:08 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-08-15 19:02:20 +08:00
|
|
|
|
2012-03-15 22:36:01 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////
|
2012-08-15 19:02:20 +08:00
|
|
|
|
2012-03-15 22:36:01 +08:00
|
|
|
BFMatcher::BFMatcher( int _normType, bool _crossCheck )
|
2010-11-25 23:59:37 +08:00
|
|
|
{
|
2012-03-15 22:36:01 +08:00
|
|
|
normType = _normType;
|
|
|
|
crossCheck = _crossCheck;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ptr<DescriptorMatcher> BFMatcher::clone( bool emptyTrainData ) const
|
|
|
|
{
|
2013-08-13 21:30:14 +08:00
|
|
|
Ptr<BFMatcher> matcher = makePtr<BFMatcher>(normType, crossCheck);
|
2012-03-15 22:36:01 +08:00
|
|
|
if( !emptyTrainData )
|
2010-11-25 23:59:37 +08:00
|
|
|
{
|
2012-03-15 22:36:01 +08:00
|
|
|
matcher->trainDescCollection.resize(trainDescCollection.size());
|
|
|
|
std::transform( trainDescCollection.begin(), trainDescCollection.end(),
|
|
|
|
matcher->trainDescCollection.begin(), clone_op );
|
2010-11-25 23:59:37 +08:00
|
|
|
}
|
2012-03-15 22:36:01 +08:00
|
|
|
return matcher;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void BFMatcher::knnMatchImpl( const Mat& queryDescriptors, std::vector<std::vector<DMatch> >& matches, int knn,
|
|
|
|
const std::vector<Mat>& masks, bool compactResult )
|
2012-03-15 22:36:01 +08:00
|
|
|
{
|
|
|
|
const int IMGIDX_SHIFT = 18;
|
|
|
|
const int IMGIDX_ONE = (1 << IMGIDX_SHIFT);
|
2012-08-15 19:02:20 +08:00
|
|
|
|
2012-03-15 22:36:01 +08:00
|
|
|
if( queryDescriptors.empty() || trainDescCollection.empty() )
|
2010-11-25 23:59:37 +08:00
|
|
|
{
|
2012-03-15 22:36:01 +08:00
|
|
|
matches.clear();
|
|
|
|
return;
|
2010-11-25 23:59:37 +08:00
|
|
|
}
|
2012-03-15 22:36:01 +08:00
|
|
|
CV_Assert( queryDescriptors.type() == trainDescCollection[0].type() );
|
2012-08-15 19:02:20 +08:00
|
|
|
|
2012-03-15 22:36:01 +08:00
|
|
|
matches.reserve(queryDescriptors.rows);
|
2012-08-15 19:02:20 +08:00
|
|
|
|
2012-03-15 22:36:01 +08:00
|
|
|
Mat dist, nidx;
|
2012-08-15 19:02:20 +08:00
|
|
|
|
2012-03-15 22:36:01 +08:00
|
|
|
int iIdx, imgCount = (int)trainDescCollection.size(), update = 0;
|
|
|
|
int dtype = normType == NORM_HAMMING || normType == NORM_HAMMING2 ||
|
|
|
|
(normType == NORM_L1 && queryDescriptors.type() == CV_8U) ? CV_32S : CV_32F;
|
2012-08-15 19:02:20 +08:00
|
|
|
|
2012-03-15 22:36:01 +08:00
|
|
|
CV_Assert( (int64)imgCount*IMGIDX_ONE < INT_MAX );
|
2012-08-15 19:02:20 +08:00
|
|
|
|
2012-03-15 22:36:01 +08:00
|
|
|
for( iIdx = 0; iIdx < imgCount; iIdx++ )
|
2010-11-25 23:59:37 +08:00
|
|
|
{
|
2012-03-15 22:36:01 +08:00
|
|
|
CV_Assert( trainDescCollection[iIdx].rows < IMGIDX_ONE );
|
|
|
|
batchDistance(queryDescriptors, trainDescCollection[iIdx], dist, dtype, nidx,
|
|
|
|
normType, knn, masks.empty() ? Mat() : masks[iIdx], update, crossCheck);
|
|
|
|
update += IMGIDX_ONE;
|
2010-11-25 23:59:37 +08:00
|
|
|
}
|
2012-08-15 19:02:20 +08:00
|
|
|
|
2012-03-15 22:36:01 +08:00
|
|
|
if( dtype == CV_32S )
|
2011-11-08 20:01:49 +08:00
|
|
|
{
|
2012-03-15 22:36:01 +08:00
|
|
|
Mat temp;
|
|
|
|
dist.convertTo(temp, CV_32F);
|
|
|
|
dist = temp;
|
2011-11-08 20:01:49 +08:00
|
|
|
}
|
2012-08-15 19:02:20 +08:00
|
|
|
|
2012-03-15 22:36:01 +08:00
|
|
|
for( int qIdx = 0; qIdx < queryDescriptors.rows; qIdx++ )
|
2011-11-08 20:01:49 +08:00
|
|
|
{
|
2012-03-15 22:36:01 +08:00
|
|
|
const float* distptr = dist.ptr<float>(qIdx);
|
|
|
|
const int* nidxptr = nidx.ptr<int>(qIdx);
|
2012-08-15 19:02:20 +08:00
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
matches.push_back( std::vector<DMatch>() );
|
|
|
|
std::vector<DMatch>& mq = matches.back();
|
2012-03-15 22:36:01 +08:00
|
|
|
mq.reserve(knn);
|
2012-08-15 19:02:20 +08:00
|
|
|
|
2012-04-14 05:50:59 +08:00
|
|
|
for( int k = 0; k < nidx.cols; k++ )
|
2012-03-15 22:36:01 +08:00
|
|
|
{
|
|
|
|
if( nidxptr[k] < 0 )
|
|
|
|
break;
|
|
|
|
mq.push_back( DMatch(qIdx, nidxptr[k] & (IMGIDX_ONE - 1),
|
|
|
|
nidxptr[k] >> IMGIDX_SHIFT, distptr[k]) );
|
|
|
|
}
|
2012-08-15 19:02:20 +08:00
|
|
|
|
2012-03-15 22:36:01 +08:00
|
|
|
if( mq.empty() && compactResult )
|
|
|
|
matches.pop_back();
|
2011-11-08 20:01:49 +08:00
|
|
|
}
|
2010-11-25 23:59:37 +08:00
|
|
|
}
|
2010-11-23 02:27:08 +08:00
|
|
|
|
2012-08-15 19:02:20 +08:00
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void BFMatcher::radiusMatchImpl( const Mat& queryDescriptors, std::vector<std::vector<DMatch> >& matches,
|
|
|
|
float maxDistance, const std::vector<Mat>& masks, bool compactResult )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2012-03-15 22:36:01 +08:00
|
|
|
if( queryDescriptors.empty() || trainDescCollection.empty() )
|
2010-10-29 16:44:42 +08:00
|
|
|
{
|
2012-03-15 22:36:01 +08:00
|
|
|
matches.clear();
|
|
|
|
return;
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
2012-03-15 22:36:01 +08:00
|
|
|
CV_Assert( queryDescriptors.type() == trainDescCollection[0].type() );
|
2012-08-15 19:02:20 +08:00
|
|
|
|
2012-03-15 22:36:01 +08:00
|
|
|
matches.resize(queryDescriptors.rows);
|
|
|
|
Mat dist, distf;
|
2012-08-15 19:02:20 +08:00
|
|
|
|
2012-03-15 22:36:01 +08:00
|
|
|
int iIdx, imgCount = (int)trainDescCollection.size();
|
|
|
|
int dtype = normType == NORM_HAMMING ||
|
|
|
|
(normType == NORM_L1 && queryDescriptors.type() == CV_8U) ? CV_32S : CV_32F;
|
2012-08-15 19:02:20 +08:00
|
|
|
|
2012-03-15 22:36:01 +08:00
|
|
|
for( iIdx = 0; iIdx < imgCount; iIdx++ )
|
2010-10-29 16:44:42 +08:00
|
|
|
{
|
2012-03-15 22:36:01 +08:00
|
|
|
batchDistance(queryDescriptors, trainDescCollection[iIdx], dist, dtype, noArray(),
|
|
|
|
normType, 0, masks.empty() ? Mat() : masks[iIdx], 0, false);
|
|
|
|
if( dtype == CV_32S )
|
|
|
|
dist.convertTo(distf, CV_32F);
|
2010-09-23 21:44:23 +08:00
|
|
|
else
|
2012-03-15 22:36:01 +08:00
|
|
|
distf = dist;
|
2012-08-15 19:02:20 +08:00
|
|
|
|
2012-03-15 22:36:01 +08:00
|
|
|
for( int qIdx = 0; qIdx < queryDescriptors.rows; qIdx++ )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2012-08-15 19:02:20 +08:00
|
|
|
const float* distptr = distf.ptr<float>(qIdx);
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<DMatch>& mq = matches[qIdx];
|
2012-08-15 19:02:20 +08:00
|
|
|
for( int k = 0; k < distf.cols; k++ )
|
2010-10-29 16:44:42 +08:00
|
|
|
{
|
2012-03-15 22:36:01 +08:00
|
|
|
if( distptr[k] <= maxDistance )
|
|
|
|
mq.push_back( DMatch(qIdx, k, iIdx, distptr[k]) );
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
}
|
2012-08-15 19:02:20 +08:00
|
|
|
|
2012-03-15 22:36:01 +08:00
|
|
|
int qIdx0 = 0;
|
|
|
|
for( int qIdx = 0; qIdx < queryDescriptors.rows; qIdx++ )
|
|
|
|
{
|
|
|
|
if( matches[qIdx].empty() && compactResult )
|
|
|
|
continue;
|
2012-08-15 19:02:20 +08:00
|
|
|
|
2012-03-15 22:36:01 +08:00
|
|
|
if( qIdx0 < qIdx )
|
|
|
|
std::swap(matches[qIdx], matches[qIdx0]);
|
2012-08-15 19:02:20 +08:00
|
|
|
|
2012-03-15 22:36:01 +08:00
|
|
|
std::sort( matches[qIdx0].begin(), matches[qIdx0].end() );
|
|
|
|
qIdx0++;
|
|
|
|
}
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
2012-08-15 19:02:20 +08:00
|
|
|
|
2012-03-15 22:36:01 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////
|
2012-08-15 19:02:20 +08:00
|
|
|
|
2012-03-15 22:36:01 +08:00
|
|
|
/*
|
|
|
|
* Factory function for DescriptorMatcher creating
|
|
|
|
*/
|
2013-03-23 00:37:49 +08:00
|
|
|
Ptr<DescriptorMatcher> DescriptorMatcher::create( const String& descriptorMatcherType )
|
2010-10-29 16:44:42 +08:00
|
|
|
{
|
2013-08-13 21:30:14 +08:00
|
|
|
Ptr<DescriptorMatcher> dm;
|
2012-03-15 22:36:01 +08:00
|
|
|
if( !descriptorMatcherType.compare( "FlannBased" ) )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2013-08-13 21:30:14 +08:00
|
|
|
dm = makePtr<FlannBasedMatcher>();
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
2012-03-15 22:36:01 +08:00
|
|
|
else if( !descriptorMatcherType.compare( "BruteForce" ) ) // L2
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2013-08-13 21:30:14 +08:00
|
|
|
dm = makePtr<BFMatcher>(int(NORM_L2)); // anonymous enums can't be template parameters
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
2012-03-15 22:36:01 +08:00
|
|
|
else if( !descriptorMatcherType.compare( "BruteForce-SL2" ) ) // Squared L2
|
2011-08-08 21:18:12 +08:00
|
|
|
{
|
2013-08-13 21:30:14 +08:00
|
|
|
dm = makePtr<BFMatcher>(int(NORM_L2SQR));
|
2011-08-08 21:18:12 +08:00
|
|
|
}
|
2012-03-15 22:36:01 +08:00
|
|
|
else if( !descriptorMatcherType.compare( "BruteForce-L1" ) )
|
|
|
|
{
|
2013-08-13 21:30:14 +08:00
|
|
|
dm = makePtr<BFMatcher>(int(NORM_L1));
|
2012-03-15 22:36:01 +08:00
|
|
|
}
|
|
|
|
else if( !descriptorMatcherType.compare("BruteForce-Hamming") ||
|
|
|
|
!descriptorMatcherType.compare("BruteForce-HammingLUT") )
|
|
|
|
{
|
2013-08-13 21:30:14 +08:00
|
|
|
dm = makePtr<BFMatcher>(int(NORM_HAMMING));
|
2012-03-15 22:36:01 +08:00
|
|
|
}
|
|
|
|
else if( !descriptorMatcherType.compare("BruteForce-Hamming(2)") )
|
|
|
|
{
|
2013-08-13 21:30:14 +08:00
|
|
|
dm = makePtr<BFMatcher>(int(NORM_HAMMING2));
|
2012-03-15 22:36:01 +08:00
|
|
|
}
|
|
|
|
else
|
2013-04-10 19:54:14 +08:00
|
|
|
CV_Error( Error::StsBadArg, "Unknown matcher name" );
|
2011-08-08 21:18:12 +08:00
|
|
|
|
2012-03-15 22:36:01 +08:00
|
|
|
return dm;
|
2011-08-08 21:18:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-29 16:44:42 +08:00
|
|
|
/*
|
|
|
|
* Flann based matcher
|
|
|
|
*/
|
|
|
|
FlannBasedMatcher::FlannBasedMatcher( const Ptr<flann::IndexParams>& _indexParams, const Ptr<flann::SearchParams>& _searchParams )
|
|
|
|
: indexParams(_indexParams), searchParams(_searchParams), addedDescCount(0)
|
|
|
|
{
|
2013-08-13 21:30:14 +08:00
|
|
|
CV_Assert( _indexParams );
|
|
|
|
CV_Assert( _searchParams );
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void FlannBasedMatcher::add( const std::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()
|
|
|
|
{
|
2013-08-13 21:30:14 +08:00
|
|
|
if( !flannIndex || mergedDescriptors.size() < addedDescCount )
|
2010-10-29 16:44:42 +08:00
|
|
|
{
|
|
|
|
mergedDescriptors.set( trainDescCollection );
|
2013-08-13 21:30:14 +08:00
|
|
|
flannIndex = makePtr<flann::Index>( mergedDescriptors.getDescriptors(), *indexParams );
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
2010-09-23 21:44:23 +08:00
|
|
|
|
2011-08-04 22:13:07 +08:00
|
|
|
void FlannBasedMatcher::read( const FileNode& fn)
|
|
|
|
{
|
2013-08-13 21:30:14 +08:00
|
|
|
if (!indexParams)
|
|
|
|
indexParams = makePtr<flann::IndexParams>();
|
2011-08-04 22:13:07 +08:00
|
|
|
|
|
|
|
FileNode ip = fn["indexParams"];
|
|
|
|
CV_Assert(ip.type() == FileNode::SEQ);
|
|
|
|
|
2012-03-17 05:21:04 +08:00
|
|
|
for(int i = 0; i < (int)ip.size(); ++i)
|
2011-08-04 22:13:07 +08:00
|
|
|
{
|
|
|
|
CV_Assert(ip[i].type() == FileNode::MAP);
|
2013-03-23 00:37:49 +08:00
|
|
|
String _name = (String)ip[i]["name"];
|
2011-08-04 22:13:07 +08:00
|
|
|
int type = (int)ip[i]["type"];
|
|
|
|
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case CV_8U:
|
|
|
|
case CV_8S:
|
|
|
|
case CV_16U:
|
|
|
|
case CV_16S:
|
|
|
|
case CV_32S:
|
2012-06-09 23:00:04 +08:00
|
|
|
indexParams->setInt(_name, (int) ip[i]["value"]);
|
2011-08-04 22:13:07 +08:00
|
|
|
break;
|
|
|
|
case CV_32F:
|
2012-06-09 23:00:04 +08:00
|
|
|
indexParams->setFloat(_name, (float) ip[i]["value"]);
|
2011-08-04 22:13:07 +08:00
|
|
|
break;
|
|
|
|
case CV_64F:
|
2012-06-09 23:00:04 +08:00
|
|
|
indexParams->setDouble(_name, (double) ip[i]["value"]);
|
2011-08-04 22:13:07 +08:00
|
|
|
break;
|
|
|
|
case CV_USRTYPE1:
|
2013-03-23 00:37:49 +08:00
|
|
|
indexParams->setString(_name, (String) ip[i]["value"]);
|
2011-08-04 22:13:07 +08:00
|
|
|
break;
|
|
|
|
case CV_MAKETYPE(CV_USRTYPE1,2):
|
2012-06-09 23:00:04 +08:00
|
|
|
indexParams->setBool(_name, (int) ip[i]["value"] != 0);
|
2011-08-04 22:13:07 +08:00
|
|
|
break;
|
|
|
|
case CV_MAKETYPE(CV_USRTYPE1,3):
|
2011-08-15 03:46:39 +08:00
|
|
|
indexParams->setAlgorithm((int) ip[i]["value"]);
|
2011-08-04 22:13:07 +08:00
|
|
|
break;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-08-13 21:30:14 +08:00
|
|
|
if (!searchParams)
|
|
|
|
searchParams = makePtr<flann::SearchParams>();
|
2011-08-04 22:13:07 +08:00
|
|
|
|
|
|
|
FileNode sp = fn["searchParams"];
|
|
|
|
CV_Assert(sp.type() == FileNode::SEQ);
|
|
|
|
|
2012-03-17 05:21:04 +08:00
|
|
|
for(int i = 0; i < (int)sp.size(); ++i)
|
2011-08-04 22:13:07 +08:00
|
|
|
{
|
|
|
|
CV_Assert(sp[i].type() == FileNode::MAP);
|
2013-03-23 00:37:49 +08:00
|
|
|
String _name = (String)sp[i]["name"];
|
2011-08-04 22:13:07 +08:00
|
|
|
int type = (int)sp[i]["type"];
|
|
|
|
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case CV_8U:
|
|
|
|
case CV_8S:
|
|
|
|
case CV_16U:
|
|
|
|
case CV_16S:
|
|
|
|
case CV_32S:
|
2012-06-09 23:00:04 +08:00
|
|
|
searchParams->setInt(_name, (int) sp[i]["value"]);
|
2011-08-04 22:13:07 +08:00
|
|
|
break;
|
|
|
|
case CV_32F:
|
2012-06-09 23:00:04 +08:00
|
|
|
searchParams->setFloat(_name, (float) ip[i]["value"]);
|
2011-08-04 22:13:07 +08:00
|
|
|
break;
|
|
|
|
case CV_64F:
|
2012-06-09 23:00:04 +08:00
|
|
|
searchParams->setDouble(_name, (double) ip[i]["value"]);
|
2011-08-04 22:13:07 +08:00
|
|
|
break;
|
|
|
|
case CV_USRTYPE1:
|
2013-03-23 00:37:49 +08:00
|
|
|
searchParams->setString(_name, (String) ip[i]["value"]);
|
2011-08-04 22:13:07 +08:00
|
|
|
break;
|
|
|
|
case CV_MAKETYPE(CV_USRTYPE1,2):
|
2012-06-09 23:00:04 +08:00
|
|
|
searchParams->setBool(_name, (int) ip[i]["value"] != 0);
|
2011-08-04 22:13:07 +08:00
|
|
|
break;
|
|
|
|
case CV_MAKETYPE(CV_USRTYPE1,3):
|
2011-08-15 03:46:39 +08:00
|
|
|
searchParams->setAlgorithm((int) ip[i]["value"]);
|
2011-08-04 22:13:07 +08:00
|
|
|
break;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
flannIndex.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlannBasedMatcher::write( FileStorage& fs) const
|
|
|
|
{
|
|
|
|
fs << "indexParams" << "[";
|
|
|
|
|
2012-12-22 03:27:29 +08:00
|
|
|
if (indexParams)
|
2011-08-04 22:13:07 +08:00
|
|
|
{
|
2013-03-23 00:37:49 +08:00
|
|
|
std::vector<String> names;
|
2011-08-04 22:13:07 +08:00
|
|
|
std::vector<int> types;
|
2013-03-23 00:37:49 +08:00
|
|
|
std::vector<String> strValues;
|
2011-08-04 22:13:07 +08:00
|
|
|
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" << "[";
|
|
|
|
|
2012-12-22 03:27:29 +08:00
|
|
|
if (searchParams)
|
2011-08-04 22:13:07 +08:00
|
|
|
{
|
2013-03-23 00:37:49 +08:00
|
|
|
std::vector<String> names;
|
2011-08-04 22:13:07 +08:00
|
|
|
std::vector<int> types;
|
2013-03-23 00:37:49 +08:00
|
|
|
std::vector<String> strValues;
|
2011-08-04 22:13:07 +08:00
|
|
|
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
|
|
|
|
{
|
2013-08-13 21:30:14 +08:00
|
|
|
Ptr<FlannBasedMatcher> matcher = makePtr<FlannBasedMatcher>(indexParams, searchParams);
|
2010-11-23 02:27:08 +08:00
|
|
|
if( !emptyTrainData )
|
|
|
|
{
|
2013-04-10 19:54:14 +08:00
|
|
|
CV_Error( Error::StsNotImplemented, "deep clone functionality is not implemented, because "
|
2010-11-23 02:27:08 +08:00
|
|
|
"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,
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<std::vector<DMatch> >& matches )
|
2010-10-29 16:44:42 +08:00
|
|
|
{
|
|
|
|
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 );
|
2011-09-22 16:52:40 +08:00
|
|
|
float dist = 0;
|
|
|
|
if (dists.type() == CV_32S)
|
|
|
|
dist = static_cast<float>( dists.at<int>(i,j) );
|
|
|
|
else
|
|
|
|
dist = std::sqrt(dists.at<float>(i,j));
|
|
|
|
matches[i].push_back( DMatch( i, trainIdx, imgIdx, dist ) );
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void FlannBasedMatcher::knnMatchImpl( const Mat& queryDescriptors, std::vector<std::vector<DMatch> >& matches, int knn,
|
|
|
|
const std::vector<Mat>& /*masks*/, bool /*compactResult*/ )
|
2010-10-29 16:44:42 +08:00
|
|
|
{
|
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 );
|
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void FlannBasedMatcher::radiusMatchImpl( const Mat& queryDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance,
|
|
|
|
const std::vector<Mat>& /*masks*/, bool /*compactResult*/ )
|
2010-10-29 16:44:42 +08:00
|
|
|
{
|
|
|
|
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++ )
|
2012-12-21 23:58:51 +08:00
|
|
|
std::copy( collection.keypoints[i].begin(), collection.keypoints[i].end(), keypoints[i].begin() );
|
2010-11-23 02:27:08 +08:00
|
|
|
|
2012-12-21 23:58:51 +08:00
|
|
|
std::copy( collection.startIndices.begin(), collection.startIndices.end(), startIndices.begin() );
|
2010-11-23 02:27:08 +08:00
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void GenericDescriptorMatcher::KeyPointCollection::add( const std::vector<Mat>& _images,
|
|
|
|
const std::vector<std::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();
|
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
const std::vector<std::vector<KeyPoint> >& GenericDescriptorMatcher::KeyPointCollection::getKeypoints() const
|
2010-11-23 02:27:08 +08:00
|
|
|
{
|
|
|
|
return keypoints;
|
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
const std::vector<KeyPoint>& GenericDescriptorMatcher::KeyPointCollection::getKeypoints( int imgIdx ) const
|
2010-11-23 02:27:08 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
const std::vector<Mat>& GenericDescriptorMatcher::KeyPointCollection::getImages() const
|
2010-11-23 02:27:08 +08:00
|
|
|
{
|
|
|
|
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()
|
|
|
|
{}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void GenericDescriptorMatcher::add( const std::vector<Mat>& images,
|
|
|
|
std::vector<std::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
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
const std::vector<Mat>& GenericDescriptorMatcher::getTrainImages() const
|
2010-11-23 02:27:08 +08:00
|
|
|
{
|
|
|
|
return trainPointCollection.getImages();
|
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
const std::vector<std::vector<KeyPoint> >& GenericDescriptorMatcher::getTrainKeypoints() const
|
2010-11-23 02:27:08 +08:00
|
|
|
{
|
|
|
|
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()
|
|
|
|
{}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void GenericDescriptorMatcher::classify( const Mat& queryImage, std::vector<KeyPoint>& queryKeypoints,
|
|
|
|
const Mat& trainImage, std::vector<KeyPoint>& trainKeypoints ) const
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
std::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
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void GenericDescriptorMatcher::classify( const Mat& queryImage, std::vector<KeyPoint>& queryKeypoints )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
std::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
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void GenericDescriptorMatcher::match( const Mat& queryImage, std::vector<KeyPoint>& queryKeypoints,
|
|
|
|
const Mat& trainImage, std::vector<KeyPoint>& trainKeypoints,
|
|
|
|
std::vector<DMatch>& matches, const Mat& mask ) const
|
2010-10-29 16:44:42 +08:00
|
|
|
{
|
2010-11-23 02:27:08 +08:00
|
|
|
Ptr<GenericDescriptorMatcher> tempMatcher = clone( true );
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<std::vector<KeyPoint> > vecTrainPoints(1, trainKeypoints);
|
|
|
|
tempMatcher->add( std::vector<Mat>(1, trainImage), vecTrainPoints );
|
|
|
|
tempMatcher->match( queryImage, queryKeypoints, matches, std::vector<Mat>(1, mask) );
|
2010-11-24 01:00:55 +08:00
|
|
|
vecTrainPoints[0].swap( trainKeypoints );
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void GenericDescriptorMatcher::knnMatch( const Mat& queryImage, std::vector<KeyPoint>& queryKeypoints,
|
|
|
|
const Mat& trainImage, std::vector<KeyPoint>& trainKeypoints,
|
|
|
|
std::vector<std::vector<DMatch> >& matches, int knn, const Mat& mask, bool compactResult ) const
|
2010-10-29 16:44:42 +08:00
|
|
|
{
|
2010-11-23 02:27:08 +08:00
|
|
|
Ptr<GenericDescriptorMatcher> tempMatcher = clone( true );
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<std::vector<KeyPoint> > vecTrainPoints(1, trainKeypoints);
|
|
|
|
tempMatcher->add( std::vector<Mat>(1, trainImage), vecTrainPoints );
|
|
|
|
tempMatcher->knnMatch( queryImage, queryKeypoints, matches, knn, std::vector<Mat>(1, mask), compactResult );
|
2010-11-24 01:00:55 +08:00
|
|
|
vecTrainPoints[0].swap( trainKeypoints );
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
2010-09-23 21:44:23 +08:00
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void GenericDescriptorMatcher::radiusMatch( const Mat& queryImage, std::vector<KeyPoint>& queryKeypoints,
|
|
|
|
const Mat& trainImage, std::vector<KeyPoint>& trainKeypoints,
|
|
|
|
std::vector<std::vector<DMatch> >& matches, float maxDistance,
|
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<GenericDescriptorMatcher> tempMatcher = clone( true );
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<std::vector<KeyPoint> > vecTrainPoints(1, trainKeypoints);
|
|
|
|
tempMatcher->add( std::vector<Mat>(1, trainImage), vecTrainPoints );
|
|
|
|
tempMatcher->radiusMatch( queryImage, queryKeypoints, matches, maxDistance, std::vector<Mat>(1, mask), compactResult );
|
2010-11-24 01:00:55 +08:00
|
|
|
vecTrainPoints[0].swap( trainKeypoints );
|
2010-10-29 16:44:42 +08:00
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void GenericDescriptorMatcher::match( const Mat& queryImage, std::vector<KeyPoint>& queryKeypoints,
|
|
|
|
std::vector<DMatch>& matches, const std::vector<Mat>& masks )
|
2010-10-29 16:44:42 +08:00
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<std::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 );
|
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void GenericDescriptorMatcher::knnMatch( const Mat& queryImage, std::vector<KeyPoint>& queryKeypoints,
|
|
|
|
std::vector<std::vector<DMatch> >& matches, int knn,
|
|
|
|
const std::vector<Mat>& masks, bool compactResult )
|
2010-10-29 16:44:42 +08:00
|
|
|
{
|
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() );
|
2012-08-15 19:02:20 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void GenericDescriptorMatcher::radiusMatch( const Mat& queryImage, std::vector<KeyPoint>& queryKeypoints,
|
|
|
|
std::vector<std::vector<DMatch> >& matches, float maxDistance,
|
|
|
|
const std::vector<Mat>& masks, bool compactResult )
|
2010-10-29 16:44:42 +08:00
|
|
|
{
|
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() );
|
2012-08-15 19:02:20 +08:00
|
|
|
|
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
|
|
|
|
*/
|
2013-03-23 00:37:49 +08:00
|
|
|
Ptr<GenericDescriptorMatcher> GenericDescriptorMatcher::create( const String& genericDescritptorMatcherType,
|
|
|
|
const String ¶msFilename )
|
2010-11-25 23:59:37 +08:00
|
|
|
{
|
2012-03-15 22:36:01 +08:00
|
|
|
Ptr<GenericDescriptorMatcher> descriptorMatcher =
|
|
|
|
Algorithm::create<GenericDescriptorMatcher>("DescriptorMatcher." + genericDescritptorMatcherType);
|
2012-08-15 19:02:20 +08:00
|
|
|
|
2013-08-13 21:30:14 +08:00
|
|
|
if( !paramsFilename.empty() && descriptorMatcher )
|
2010-11-25 23:59:37 +08:00
|
|
|
{
|
|
|
|
FileStorage fs = FileStorage( paramsFilename, FileStorage::READ );
|
|
|
|
if( fs.isOpened() )
|
|
|
|
{
|
|
|
|
descriptorMatcher->read( fs.root() );
|
|
|
|
fs.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return descriptorMatcher;
|
|
|
|
}
|
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
|
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 )
|
|
|
|
{
|
2013-08-13 21:30:14 +08:00
|
|
|
CV_Assert( extractor && matcher );
|
2010-11-23 02:27:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
VectorDescriptorMatcher::~VectorDescriptorMatcher()
|
|
|
|
{}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void VectorDescriptorMatcher::add( const std::vector<Mat>& imgCollection,
|
|
|
|
std::vector<std::vector<KeyPoint> >& pointCollection )
|
2010-09-23 21:44:23 +08:00
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<Mat> descriptors;
|
2010-11-23 02:27:08 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void VectorDescriptorMatcher::knnMatchImpl( const Mat& queryImage, std::vector<KeyPoint>& queryKeypoints,
|
|
|
|
std::vector<std::vector<DMatch> >& matches, int knn,
|
|
|
|
const std::vector<Mat>& masks, bool compactResult )
|
2010-10-29 16:44:42 +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->knnMatch( queryDescriptors, matches, knn, masks, compactResult );
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void VectorDescriptorMatcher::radiusMatchImpl( const Mat& queryImage, std::vector<KeyPoint>& queryKeypoints,
|
|
|
|
std::vector<std::vector<DMatch> >& matches, float maxDistance,
|
|
|
|
const std::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
|
|
|
|
{
|
2013-08-13 21:30:14 +08:00
|
|
|
return !extractor || extractor->empty() ||
|
|
|
|
!matcher || matcher->empty();
|
2011-01-31 22:18:50 +08:00
|
|
|
}
|
|
|
|
|
2010-11-23 02:27:08 +08:00
|
|
|
Ptr<GenericDescriptorMatcher> VectorDescriptorMatcher::clone( bool emptyTrainData ) const
|
|
|
|
{
|
|
|
|
// TODO clone extractor
|
2013-08-13 21:30:14 +08:00
|
|
|
return makePtr<VectorDescriptorMatcher>( extractor, matcher->clone(emptyTrainData) );
|
2010-11-23 02:27:08 +08:00
|
|
|
}
|
|
|
|
|
2010-09-23 21:44:23 +08:00
|
|
|
}
|