@ -121,6 +121,10 @@ BOWImgDescriptorExtractor::BOWImgDescriptorExtractor( const Ptr<DescriptorExtrac
dextractor ( _dextractor ) , dmatcher ( _dmatcher )
dextractor ( _dextractor ) , dmatcher ( _dmatcher )
{ }
{ }
BOWImgDescriptorExtractor : : BOWImgDescriptorExtractor ( const Ptr < DescriptorMatcher > & _dmatcher ) :
dmatcher ( _dmatcher )
{ }
BOWImgDescriptorExtractor : : ~ BOWImgDescriptorExtractor ( )
BOWImgDescriptorExtractor : : ~ BOWImgDescriptorExtractor ( )
{ }
{ }
@ -144,38 +148,11 @@ void BOWImgDescriptorExtractor::compute( const Mat& image, std::vector<KeyPoint>
if ( keypoints . empty ( ) )
if ( keypoints . empty ( ) )
return ;
return ;
int clusterCount = descriptorSize ( ) ; // = vocabulary.rows
// Compute descriptors for the image.
// Compute descriptors for the image.
Mat descriptors ;
Mat descriptors ;
dextractor - > compute ( image , keypoints , descriptors ) ;
dextractor - > compute ( image , keypoints , descriptors ) ;
// Match keypoint descriptors to cluster center (to vocabulary)
compute ( descriptors , imgDescriptor , pointIdxsOfClusters ) ;
std : : vector < DMatch > matches ;
dmatcher - > match ( descriptors , matches ) ;
// Compute image descriptor
if ( pointIdxsOfClusters )
{
pointIdxsOfClusters - > clear ( ) ;
pointIdxsOfClusters - > resize ( clusterCount ) ;
}
imgDescriptor = Mat ( 1 , clusterCount , descriptorType ( ) , Scalar : : all ( 0.0 ) ) ;
float * dptr = ( float * ) imgDescriptor . data ;
for ( size_t i = 0 ; i < matches . size ( ) ; i + + )
{
int queryIdx = matches [ i ] . queryIdx ;
int trainIdx = matches [ i ] . trainIdx ; // cluster index
CV_Assert ( queryIdx = = ( int ) i ) ;
dptr [ trainIdx ] = dptr [ trainIdx ] + 1.f ;
if ( pointIdxsOfClusters )
( * pointIdxsOfClusters ) [ trainIdx ] . push_back ( queryIdx ) ;
}
// Normalize image descriptor.
imgDescriptor / = descriptors . rows ;
// Add the descriptors of image keypoints
// Add the descriptors of image keypoints
if ( _descriptors ) {
if ( _descriptors ) {
@ -193,44 +170,16 @@ int BOWImgDescriptorExtractor::descriptorType() const
return CV_32FC1 ;
return CV_32FC1 ;
}
}
BOWImgDescriptorMatcher : : BOWImgDescriptorMatcher ( const Ptr < DescriptorMatcher > & _dmatcher ) :
void BOWImgDescriptorExtractor : : compute ( const Mat & descriptors , Mat & imgDescriptor , std : : vector < std : : vector < int > > * pointIdxsOfClusters )
dmatcher ( _dmatcher ) ,
_type ( - 1 )
{ }
BOWImgDescriptorMatcher : : ~ BOWImgDescriptorMatcher ( )
{ }
void BOWImgDescriptorMatcher : : setVocabulary ( const Mat & _vocabulary )
{
dmatcher - > clear ( ) ;
CV_Assert ( _vocabulary . type ( ) = = CV_32F ) ;
vocabulary = _vocabulary ;
dmatcher - > add ( std : : vector < Mat > ( 1 , vocabulary ) ) ;
}
const Mat & BOWImgDescriptorMatcher : : getVocabulary ( ) const
{
{
return vocabulary ;
CV_Assert ( vocabulary . empty ( ) ! = false ) ;
}
void BOWImgDescriptorMatcher : : compute ( const Mat & descriptors , Mat & vocDescriptor , std : : vector < std : : vector < int > > * pointIdxsOfClusters )
{
vocDescriptor . release ( ) ;
int clusterCount = descriptorSize ( ) ; // = vocabulary.rows
int clusterCount = descriptorSize ( ) ; // = vocabulary.rows
_type = descriptors . type ( ) ;
Mat _descriptors ;
if ( _type ! = CV_32F )
descriptors . convertTo ( _descriptors , CV_32F ) ;
else
descriptors . copyTo ( _descriptors ) ;
// Match keypoint descriptors to cluster center (to vocabulary)
// Match keypoint descriptors to cluster center (to vocabulary)
std : : vector < DMatch > matches ;
std : : vector < DMatch > matches ;
dmatcher - > match ( _ descriptors, matches ) ;
dmatcher - > match ( descriptors , matches ) ;
// Compute image descriptor
// Compute image descriptor
if ( pointIdxsOfClusters )
if ( pointIdxsOfClusters )
{
{
@ -238,8 +187,8 @@ void BOWImgDescriptorMatcher::compute( const Mat & descriptors, Mat& vocDescript
pointIdxsOfClusters - > resize ( clusterCount ) ;
pointIdxsOfClusters - > resize ( clusterCount ) ;
}
}
vocDescriptor = Mat : : zeros ( 1 , clusterCount , descriptorType ( ) ) ;
imgDescriptor = Mat ( 1 , clusterCount , descriptorType ( ) , Scalar : : all ( 0.0 ) ) ;
float * dptr = ( float * ) voc Descriptor. data ;
float * dptr = ( float * ) img Descriptor. data ;
for ( size_t i = 0 ; i < matches . size ( ) ; i + + )
for ( size_t i = 0 ; i < matches . size ( ) ; i + + )
{
{
int queryIdx = matches [ i ] . queryIdx ;
int queryIdx = matches [ i ] . queryIdx ;
@ -252,17 +201,7 @@ void BOWImgDescriptorMatcher::compute( const Mat & descriptors, Mat& vocDescript
}
}
// Normalize image descriptor.
// Normalize image descriptor.
vocDescriptor / = descriptors . rows ;
imgDescriptor / = descriptors . rows ;
}
int BOWImgDescriptorMatcher : : descriptorSize ( ) const
{
return vocabulary . empty ( ) ? 0 : vocabulary . rows ;
}
int BOWImgDescriptorMatcher : : descriptorType ( ) const
{
return _type ;
}
}
}
}