fix and some enchantments for matching_to_many_images sample (#869)
This commit is contained in:
parent
d225ab238f
commit
518106af6d
@ -1,5 +1,6 @@
|
|||||||
#include "opencv2/highgui/highgui.hpp"
|
#include "opencv2/highgui/highgui.hpp"
|
||||||
#include "opencv2/features2d/features2d.hpp"
|
#include "opencv2/features2d/features2d.hpp"
|
||||||
|
#include "opencv2/contrib/contrib.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
@ -48,16 +49,21 @@ void maskMatchesByTrainImgIdx( const vector<DMatch>& matches, int trainImgIdx, v
|
|||||||
|
|
||||||
void readTrainFilenames( const string& filename, string& dirName, vector<string>& trainFilenames )
|
void readTrainFilenames( const string& filename, string& dirName, vector<string>& trainFilenames )
|
||||||
{
|
{
|
||||||
const char dlmtr = '/';
|
|
||||||
|
|
||||||
trainFilenames.clear();
|
trainFilenames.clear();
|
||||||
|
|
||||||
ifstream file( filename.c_str() );
|
ifstream file( filename.c_str() );
|
||||||
if ( !file.is_open() )
|
if ( !file.is_open() )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
size_t pos = filename.rfind(dlmtr);
|
size_t pos = filename.rfind('\\');
|
||||||
|
char dlmtr = '\\';
|
||||||
|
if (pos == String::npos)
|
||||||
|
{
|
||||||
|
pos = filename.rfind('/');
|
||||||
|
dlmtr = '/';
|
||||||
|
}
|
||||||
dirName = pos == string::npos ? "" : filename.substr(0, pos) + dlmtr;
|
dirName = pos == string::npos ? "" : filename.substr(0, pos) + dlmtr;
|
||||||
|
|
||||||
while( !file.eof() )
|
while( !file.eof() )
|
||||||
{
|
{
|
||||||
string str; getline( file, str );
|
string str; getline( file, str );
|
||||||
@ -142,6 +148,12 @@ void computeDescriptors( const Mat& queryImage, vector<KeyPoint>& queryKeypoints
|
|||||||
cout << "< Computing descriptors for keypoints..." << endl;
|
cout << "< Computing descriptors for keypoints..." << endl;
|
||||||
descriptorExtractor->compute( queryImage, queryKeypoints, queryDescriptors );
|
descriptorExtractor->compute( queryImage, queryKeypoints, queryDescriptors );
|
||||||
descriptorExtractor->compute( trainImages, trainKeypoints, trainDescriptors );
|
descriptorExtractor->compute( trainImages, trainKeypoints, trainDescriptors );
|
||||||
|
|
||||||
|
int totalTrainDesc = 0;
|
||||||
|
for( vector<Mat>::const_iterator tdIter = trainDescriptors.begin(); tdIter != trainDescriptors.end(); tdIter++ )
|
||||||
|
totalTrainDesc += tdIter->rows;
|
||||||
|
|
||||||
|
cout << "Query descriptors count: " << queryDescriptors.rows << "; Total train descriptors count: " << totalTrainDesc << endl;
|
||||||
cout << ">" << endl;
|
cout << ">" << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,9 +161,23 @@ void matchDescriptors( const Mat& queryDescriptors, const vector<Mat>& trainDesc
|
|||||||
vector<DMatch>& matches, Ptr<DescriptorMatcher>& descriptorMatcher )
|
vector<DMatch>& matches, Ptr<DescriptorMatcher>& descriptorMatcher )
|
||||||
{
|
{
|
||||||
cout << "< Set train descriptors collection in the matcher and match query descriptors to them..." << endl;
|
cout << "< Set train descriptors collection in the matcher and match query descriptors to them..." << endl;
|
||||||
|
TickMeter tm;
|
||||||
|
|
||||||
|
tm.start();
|
||||||
descriptorMatcher->add( trainDescriptors );
|
descriptorMatcher->add( trainDescriptors );
|
||||||
|
descriptorMatcher->train();
|
||||||
|
tm.stop();
|
||||||
|
double buildTime = tm.getTimeMilli();
|
||||||
|
|
||||||
|
tm.start();
|
||||||
descriptorMatcher->match( queryDescriptors, matches );
|
descriptorMatcher->match( queryDescriptors, matches );
|
||||||
|
tm.stop();
|
||||||
|
double matchTime = tm.getTimeMilli();
|
||||||
|
|
||||||
CV_Assert( queryDescriptors.rows == (int)matches.size() || matches.empty() );
|
CV_Assert( queryDescriptors.rows == (int)matches.size() || matches.empty() );
|
||||||
|
|
||||||
|
cout << "Number of matches: " << matches.size() << endl;
|
||||||
|
cout << "Build time: " << buildTime << " ms; Match time: " << matchTime << " ms" << endl;
|
||||||
cout << ">" << endl;
|
cout << ">" << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,7 +194,7 @@ void saveResultImages( const Mat& queryImage, const vector<KeyPoint>& queryKeypo
|
|||||||
{
|
{
|
||||||
maskMatchesByTrainImgIdx( matches, i, mask );
|
maskMatchesByTrainImgIdx( matches, i, mask );
|
||||||
drawMatches( queryImage, queryKeypoints, trainImages[i], trainKeypoints[i],
|
drawMatches( queryImage, queryKeypoints, trainImages[i], trainKeypoints[i],
|
||||||
matches, drawImg, Scalar::all(-1), Scalar::all(-1), mask );
|
matches, drawImg, Scalar(255, 0, 0), Scalar(0, 255, 255), mask );
|
||||||
string filename = resultDir + "/res_" + trainImagesNames[i];
|
string filename = resultDir + "/res_" + trainImagesNames[i];
|
||||||
if( !imwrite( filename, drawImg ) )
|
if( !imwrite( filename, drawImg ) )
|
||||||
cout << "Image " << filename << " can not be saved (may be because directory " << resultDir << " does not exist)." << endl;
|
cout << "Image " << filename << " can not be saved (may be because directory " << resultDir << " does not exist)." << endl;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user