Commit e406dfee authored by Maria Dimashova's avatar Maria Dimashova

refactored features2d and sample on matching to many images

parent cf0d9da6
...@@ -46,6 +46,12 @@ using namespace std; ...@@ -46,6 +46,12 @@ using namespace std;
namespace cv namespace cv
{ {
BOWTrainer::BOWTrainer()
{}
BOWTrainer::~BOWTrainer()
{}
void BOWTrainer::add( const Mat& _descriptors ) void BOWTrainer::add( const Mat& _descriptors )
{ {
CV_Assert( !_descriptors.empty() ); CV_Assert( !_descriptors.empty() );
...@@ -63,6 +69,16 @@ void BOWTrainer::add( const Mat& _descriptors ) ...@@ -63,6 +69,16 @@ void BOWTrainer::add( const Mat& _descriptors )
descriptors.push_back(_descriptors); descriptors.push_back(_descriptors);
} }
const vector<Mat>& BOWTrainer::getDescriptors() const
{
return descriptors;
}
int BOWTrainer::descripotorsCount() const
{
return descriptors.empty() ? 0 : size;
}
void BOWTrainer::clear() void BOWTrainer::clear()
{ {
descriptors.clear(); descriptors.clear();
...@@ -91,6 +107,9 @@ Mat BOWKMeansTrainer::cluster() const ...@@ -91,6 +107,9 @@ Mat BOWKMeansTrainer::cluster() const
return cluster( mergedDescriptors ); return cluster( mergedDescriptors );
} }
BOWKMeansTrainer::~BOWKMeansTrainer()
{}
Mat BOWKMeansTrainer::cluster( const Mat& descriptors ) const Mat BOWKMeansTrainer::cluster( const Mat& descriptors ) const
{ {
Mat labels, vocabulary; Mat labels, vocabulary;
...@@ -104,6 +123,9 @@ BOWImgDescriptorExtractor::BOWImgDescriptorExtractor( const Ptr<DescriptorExtrac ...@@ -104,6 +123,9 @@ BOWImgDescriptorExtractor::BOWImgDescriptorExtractor( const Ptr<DescriptorExtrac
dextractor(_dextractor), dmatcher(_dmatcher) dextractor(_dextractor), dmatcher(_dmatcher)
{} {}
BOWImgDescriptorExtractor::~BOWImgDescriptorExtractor()
{}
void BOWImgDescriptorExtractor::setVocabulary( const Mat& _vocabulary ) void BOWImgDescriptorExtractor::setVocabulary( const Mat& _vocabulary )
{ {
dmatcher->clear(); dmatcher->clear();
...@@ -111,6 +133,11 @@ void BOWImgDescriptorExtractor::setVocabulary( const Mat& _vocabulary ) ...@@ -111,6 +133,11 @@ void BOWImgDescriptorExtractor::setVocabulary( const Mat& _vocabulary )
dmatcher->add( vector<Mat>(1, vocabulary) ); dmatcher->add( vector<Mat>(1, vocabulary) );
} }
const Mat& BOWImgDescriptorExtractor::getVocabulary() const
{
return vocabulary;
}
void BOWImgDescriptorExtractor::compute( const Mat& image, vector<KeyPoint>& keypoints, Mat& imgDescriptor, void BOWImgDescriptorExtractor::compute( const Mat& image, vector<KeyPoint>& keypoints, Mat& imgDescriptor,
vector<vector<int> >* pointIdxsOfClusters, Mat* _descriptors ) vector<vector<int> >* pointIdxsOfClusters, Mat* _descriptors )
{ {
...@@ -153,4 +180,14 @@ void BOWImgDescriptorExtractor::compute( const Mat& image, vector<KeyPoint>& key ...@@ -153,4 +180,14 @@ void BOWImgDescriptorExtractor::compute( const Mat& image, vector<KeyPoint>& key
imgDescriptor /= descriptors.rows; imgDescriptor /= descriptors.rows;
} }
int BOWImgDescriptorExtractor::descriptorSize() const
{
return vocabulary.empty() ? 0 : vocabulary.rows;
}
int BOWImgDescriptorExtractor::descriptorType() const
{
return CV_32FC1;
}
} }
This diff is collapsed.
...@@ -67,6 +67,21 @@ struct RoiPredicate ...@@ -67,6 +67,21 @@ struct RoiPredicate
float minX, minY, maxX, maxY; float minX, minY, maxX, maxY;
}; };
DescriptorExtractor::~DescriptorExtractor()
{}
void DescriptorExtractor::compute( const Mat& image, vector<KeyPoint>& keypoints, Mat& descriptors ) const
{
if( image.empty() || keypoints.empty() )
return;
// Check keypoints are in image. Do filter bad points here?
//for( size_t i = 0; i < keypoints.size(); i++ )
// CV_Assert( Rect(0,0, image.cols, image.rows).contains(keypoints[i].pt) );
computeImpl( image, keypoints, descriptors );
}
void DescriptorExtractor::compute( const vector<Mat>& imageCollection, vector<vector<KeyPoint> >& pointCollection, vector<Mat>& descCollection ) const void DescriptorExtractor::compute( const vector<Mat>& imageCollection, vector<vector<KeyPoint> >& pointCollection, vector<Mat>& descCollection ) const
{ {
descCollection.resize( imageCollection.size() ); descCollection.resize( imageCollection.size() );
...@@ -74,27 +89,42 @@ void DescriptorExtractor::compute( const vector<Mat>& imageCollection, vector<ve ...@@ -74,27 +89,42 @@ void DescriptorExtractor::compute( const vector<Mat>& imageCollection, vector<ve
compute( imageCollection[i], pointCollection[i], descCollection[i] ); compute( imageCollection[i], pointCollection[i], descCollection[i] );
} }
void DescriptorExtractor::read( const FileNode& )
{}
void DescriptorExtractor::write( FileStorage& ) const
{}
void DescriptorExtractor::removeBorderKeypoints( vector<KeyPoint>& keypoints, void DescriptorExtractor::removeBorderKeypoints( vector<KeyPoint>& keypoints,
Size imageSize, int borderPixels ) Size imageSize, int borderSize )
{ {
keypoints.erase( remove_if(keypoints.begin(), keypoints.end(), if( borderSize > 0)
RoiPredicate((float)borderPixels, (float)borderPixels, {
(float)(imageSize.width - borderPixels), keypoints.erase( remove_if(keypoints.begin(), keypoints.end(),
(float)(imageSize.height - borderPixels))), RoiPredicate((float)borderSize, (float)borderSize,
keypoints.end()); (float)(imageSize.width - borderSize),
(float)(imageSize.height - borderSize))),
keypoints.end() );
}
} }
/****************************************************************************************\ /****************************************************************************************\
* SiftDescriptorExtractor * * SiftDescriptorExtractor *
\****************************************************************************************/ \****************************************************************************************/
SiftDescriptorExtractor::SiftDescriptorExtractor(const SIFT::DescriptorParams& descriptorParams,
const SIFT::CommonParams& commonParams)
: sift( descriptorParams.magnification, descriptorParams.isNormalize, descriptorParams.recalculateAngles,
commonParams.nOctaves, commonParams.nOctaveLayers, commonParams.firstOctave, commonParams.angleMode )
{}
SiftDescriptorExtractor::SiftDescriptorExtractor( double magnification, bool isNormalize, bool recalculateAngles, SiftDescriptorExtractor::SiftDescriptorExtractor( double magnification, bool isNormalize, bool recalculateAngles,
int nOctaves, int nOctaveLayers, int firstOctave, int angleMode ) int nOctaves, int nOctaveLayers, int firstOctave, int angleMode )
: sift( magnification, isNormalize, recalculateAngles, nOctaves, nOctaveLayers, firstOctave, angleMode ) : sift( magnification, isNormalize, recalculateAngles, nOctaves, nOctaveLayers, firstOctave, angleMode )
{} {}
void SiftDescriptorExtractor::compute( const Mat& image, void SiftDescriptorExtractor::computeImpl( const Mat& image,
vector<KeyPoint>& keypoints, vector<KeyPoint>& keypoints,
Mat& descriptors) const Mat& descriptors) const
{ {
bool useProvidedKeypoints = true; bool useProvidedKeypoints = true;
Mat grayImage = image; Mat grayImage = image;
...@@ -131,6 +161,16 @@ void SiftDescriptorExtractor::write (FileStorage &fs) const ...@@ -131,6 +161,16 @@ void SiftDescriptorExtractor::write (FileStorage &fs) const
fs << "angleMode" << commParams.angleMode; fs << "angleMode" << commParams.angleMode;
} }
int SiftDescriptorExtractor::descriptorSize() const
{
return sift.descriptorSize();
}
int SiftDescriptorExtractor::descriptorType() const
{
return CV_32FC1;
}
/****************************************************************************************\ /****************************************************************************************\
* SurfDescriptorExtractor * * SurfDescriptorExtractor *
\****************************************************************************************/ \****************************************************************************************/
...@@ -139,9 +179,9 @@ SurfDescriptorExtractor::SurfDescriptorExtractor( int nOctaves, ...@@ -139,9 +179,9 @@ SurfDescriptorExtractor::SurfDescriptorExtractor( int nOctaves,
: surf( 0.0, nOctaves, nOctaveLayers, extended ) : surf( 0.0, nOctaves, nOctaveLayers, extended )
{} {}
void SurfDescriptorExtractor::compute( const Mat& image, void SurfDescriptorExtractor::computeImpl( const Mat& image,
vector<KeyPoint>& keypoints, vector<KeyPoint>& keypoints,
Mat& descriptors) const Mat& descriptors) const
{ {
// Compute descriptors for given keypoints // Compute descriptors for given keypoints
vector<float> _descriptors; vector<float> _descriptors;
...@@ -175,11 +215,21 @@ void SurfDescriptorExtractor::write( FileStorage &fs ) const ...@@ -175,11 +215,21 @@ void SurfDescriptorExtractor::write( FileStorage &fs ) const
fs << "extended" << surf.extended; fs << "extended" << surf.extended;
} }
int SurfDescriptorExtractor::descriptorSize() const
{
return surf.descriptorSize();
}
int SurfDescriptorExtractor::descriptorType() const
{
return CV_32FC1;
}
/****************************************************************************************\ /****************************************************************************************\
* OpponentColorDescriptorExtractor * * OpponentColorDescriptorExtractor *
\****************************************************************************************/ \****************************************************************************************/
OpponentColorDescriptorExtractor::OpponentColorDescriptorExtractor( const Ptr<DescriptorExtractor>& _dextractor ) : OpponentColorDescriptorExtractor::OpponentColorDescriptorExtractor( const Ptr<DescriptorExtractor>& _descriptorExtractor ) :
dextractor(_dextractor) descriptorExtractor(_descriptorExtractor)
{} {}
void convertBGRImageToOpponentColorSpace( const Mat& bgrImage, vector<Mat>& opponentChannels ) void convertBGRImageToOpponentColorSpace( const Mat& bgrImage, vector<Mat>& opponentChannels )
...@@ -246,33 +296,42 @@ void convertBGRImageToOpponentColorSpace( const Mat& bgrImage, vector<Mat>& oppo ...@@ -246,33 +296,42 @@ void convertBGRImageToOpponentColorSpace( const Mat& bgrImage, vector<Mat>& oppo
} }
} }
void OpponentColorDescriptorExtractor::compute( const Mat& bgrImage, vector<KeyPoint>& keypoints, Mat& descriptors ) const void OpponentColorDescriptorExtractor::computeImpl( const Mat& bgrImage, vector<KeyPoint>& keypoints, Mat& descriptors ) const
{ {
vector<Mat> opponentChannels; vector<Mat> opponentChannels;
convertBGRImageToOpponentColorSpace( bgrImage, opponentChannels ); convertBGRImageToOpponentColorSpace( bgrImage, opponentChannels );
// Compute descriptors three times, once for each Opponent channel // Compute descriptors three times, once for each Opponent channel
// and concatenate into a single color surf descriptor // and concatenate into a single color surf descriptor
int descriptorSize = dextractor->descriptorSize(); int descriptorSize = descriptorExtractor->descriptorSize();
descriptors.create( static_cast<int>(keypoints.size()), 3*descriptorSize, CV_32FC1 ); descriptors.create( static_cast<int>(keypoints.size()), 3*descriptorSize, CV_32FC1 );
for( int i = 0; i < 3/*channel count*/; i++ ) for( int i = 0; i < 3/*channel count*/; i++ )
{ {
CV_Assert( opponentChannels[i].type() == CV_8UC1 ); CV_Assert( opponentChannels[i].type() == CV_8UC1 );
Mat opponentDescriptors = descriptors.colRange( i*descriptorSize, (i+1)*descriptorSize ); Mat opponentDescriptors = descriptors.colRange( i*descriptorSize, (i+1)*descriptorSize );
dextractor->compute( opponentChannels[i], keypoints, opponentDescriptors ); descriptorExtractor->compute( opponentChannels[i], keypoints, opponentDescriptors );
} }
} }
void OpponentColorDescriptorExtractor::read( const FileNode& fn ) void OpponentColorDescriptorExtractor::read( const FileNode& fn )
{ {
dextractor->read( fn ); descriptorExtractor->read(fn);
} }
void OpponentColorDescriptorExtractor::write( FileStorage& fs ) const void OpponentColorDescriptorExtractor::write( FileStorage& fs ) const
{ {
dextractor->write( fs ); descriptorExtractor->write(fs);
}
int OpponentColorDescriptorExtractor::descriptorSize() const
{
return 3*descriptorExtractor->descriptorSize();
} }
int OpponentColorDescriptorExtractor::descriptorType() const
{
return descriptorExtractor->descriptorType();
}
/****************************************************************************************\ /****************************************************************************************\
* Factory function for descriptor extractor creating * * Factory function for descriptor extractor creating *
\****************************************************************************************/ \****************************************************************************************/
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -60,7 +60,7 @@ public: ...@@ -60,7 +60,7 @@ public:
CvTest( testName, "cv::FeatureDetector::detect"), fdetector(_fdetector) {} CvTest( testName, "cv::FeatureDetector::detect"), fdetector(_fdetector) {}
protected: protected:
virtual void run( int start_from ) virtual void run( int /*start_from*/ )
{ {
const float maxPtDif = 1.f; const float maxPtDif = 1.f;
const float maxSizeDif = 1.f; const float maxSizeDif = 1.f;
...@@ -112,7 +112,7 @@ protected: ...@@ -112,7 +112,7 @@ protected:
for( size_t c = 0; c < calcKeypoints.size(); c++ ) for( size_t c = 0; c < calcKeypoints.size(); c++ )
{ {
progress = update_progress( progress, v*calcKeypoints.size() + c, progressCount, 0 ); progress = update_progress( progress, v*calcKeypoints.size() + c, progressCount, 0 );
float curDist = norm( calcKeypoints[c].pt - validKeypoints[v].pt ); float curDist = (float)norm( calcKeypoints[c].pt - validKeypoints[v].pt );
if( curDist < minDist ) if( curDist < minDist )
{ {
minDist = curDist; minDist = curDist;
...@@ -434,7 +434,7 @@ int CV_DescriptorMatcherTest::testMatch( const Mat& query, const Mat& train ) ...@@ -434,7 +434,7 @@ int CV_DescriptorMatcherTest::testMatch( const Mat& query, const Mat& train )
for( size_t i = 0; i < matches.size(); i++ ) for( size_t i = 0; i < matches.size(); i++ )
{ {
DMatch match = matches[i]; DMatch match = matches[i];
int shift = dmatcher->supportMask() ? 1 : 0; int shift = dmatcher->isMaskSupported() ? 1 : 0;
{ {
if( i < queryDescCount/2 ) if( i < queryDescCount/2 )
{ {
...@@ -533,7 +533,7 @@ int CV_DescriptorMatcherTest::testKnnMatch( const Mat& query, const Mat& train ) ...@@ -533,7 +533,7 @@ int CV_DescriptorMatcherTest::testKnnMatch( const Mat& query, const Mat& train )
else else
{ {
int badCount = 0; int badCount = 0;
int shift = dmatcher->supportMask() ? 1 : 0; int shift = dmatcher->isMaskSupported() ? 1 : 0;
for( size_t i = 0; i < matches.size(); i++ ) for( size_t i = 0; i < matches.size(); i++ )
{ {
if( (int)matches[i].size() != knn ) if( (int)matches[i].size() != knn )
...@@ -641,8 +641,8 @@ int CV_DescriptorMatcherTest::testRadiusMatch( const Mat& query, const Mat& trai ...@@ -641,8 +641,8 @@ int CV_DescriptorMatcherTest::testRadiusMatch( const Mat& query, const Mat& trai
res = curRes != CvTS::OK ? curRes : res; res = curRes != CvTS::OK ? curRes : res;
int badCount = 0; int badCount = 0;
int shift = dmatcher->supportMask() ? 1 : 0; int shift = dmatcher->isMaskSupported() ? 1 : 0;
int needMatchCount = dmatcher->supportMask() ? n-1 : n; int needMatchCount = dmatcher->isMaskSupported() ? n-1 : n;
for( size_t i = 0; i < matches.size(); i++ ) for( size_t i = 0; i < matches.size(); i++ )
{ {
if( (int)matches[i].size() != needMatchCount ) if( (int)matches[i].size() != needMatchCount )
...@@ -741,6 +741,6 @@ CV_CalonderDescriptorExtractorTest<float> floatCalonderTest( "descriptor-calonde ...@@ -741,6 +741,6 @@ CV_CalonderDescriptorExtractorTest<float> floatCalonderTest( "descriptor-calonde
* Matchers * Matchers
*/ */
CV_DescriptorMatcherTest bruteForceMatcherTest( "descriptor-matcher-brute-force", CV_DescriptorMatcherTest bruteForceMatcherTest( "descriptor-matcher-brute-force",
new BruteForceMatcher<L2<float> >, 0.01 ); new BruteForceMatcher<L2<float> >, 0.01f );
CV_DescriptorMatcherTest flannBasedMatcherTest( "descriptor-matcher-flann-based", CV_DescriptorMatcherTest flannBasedMatcherTest( "descriptor-matcher-flann-based",
new FlannBasedMatcher, 0.04 ); new FlannBasedMatcher, 0.04f );
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment