我对以下代码有疑问:
#include "iostream"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/nonfree/nonfree.hpp>
#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/flann/flann.hpp>
#include "opencv2/flann/miniflann.hpp"
#include <opencv2/legacy/legacy.hpp>
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgproc/types_c.h"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/core/version.hpp"
#include "opencv2/core/operations.hpp"
#include "opencv2/core/mat.hpp"
#include "opencv2/core/types_c.h"
#include <limits>
#include <vector>
#include <iostream>
#include <fstream>
using namespace cv;
using namespace std;
static void readTrainFilenames( const string& filename, string& dirName, vector<string>& trainFilenames )
{
trainFilenames.clear();
ifstream file( filename.c_str() );
if ( !file.is_open() )
return;
size_t pos = filename.rfind('\\');
char dlmtr = '\\';
if (pos == String::npos)
{
pos = filename.rfind('/');
dlmtr = '/';
}
dirName = pos == string::npos ? "" : filename.substr(0, pos) + dlmtr;
while( !file.eof() )
{
string str; getline( file, str );
if( str.empty() ) break;
trainFilenames.push_back(str);
}
file.close();
}
static bool createDetectorDescriptorMatcher( const string& detectorType, const string& descriptorType, const string& matcherType,
Ptr<FeatureDetector>& featureDetector,
Ptr<DescriptorExtractor>& descriptorExtractor,
Ptr<DescriptorMatcher>& descriptorMatcher )
{
cout << "< Creating feature detector, descriptor extractor and descriptor matcher ..." << endl;
featureDetector = FeatureDetector::create( detectorType );
descriptorExtractor = DescriptorExtractor::create( descriptorType );
descriptorMatcher = DescriptorMatcher::create( matcherType );
cout << ">" << endl;
bool isCreated = !( featureDetector.empty() || descriptorExtractor.empty() || descriptorMatcher.empty() );
if( !isCreated )
cout << "Can not create feature detector or descriptor extractor or descriptor matcher of given types." << endl << ">" << endl;
return isCreated;
}
static bool readImages( const string& queryImageName, const string& trainFilename,
Mat& queryImage, vector <Mat>& trainImages, vector<string>& trainImageNames )
{
cout << "< Reading the images..." << endl;
queryImage = imread( queryImageName);
if( queryImage.empty() )
{
cout << "Query image can not be read." << endl << ">" << endl;
return false;
}
string trainDirName;
readTrainFilenames( trainFilename, trainDirName, trainImageNames );
if( trainImageNames.empty() )
{
cout << "Train image filenames can not be read." << endl << ">" << endl;
return false;
}
int readImageCount = 0;
for( size_t i = 0; i < trainImageNames.size(); i++ )
{
string filename = trainDirName + trainImageNames[i];
Mat img = imread( filename );
if( img.empty() )
cout << "Train image " << filename << " can not be read." << endl;
else {
readImageCount++;
double min, max;
minMaxLoc(img, &min, &max);
if (min!=max) {
trainImages.push_back( img );
}
}
}
if( !readImageCount )
{
cout << "All train images can not be read." << endl << ">" << endl;
return false;
}
else
cout << readImageCount << " train images were read." << endl;
cout << ">" << endl;
return true;
}
static void detectKeypoints( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
const vector<Mat>& trainImages, vector<vector<KeyPoint> >& trainKeypoints,
Ptr<FeatureDetector>& featureDetector )
{
TickMeter tm;
tm.start();
cout << endl << "< Extracting keypoints from images..." << endl;
featureDetector->detect( queryImage, queryKeypoints );
featureDetector->detect( trainImages, trainKeypoints );
cout << ">" << endl;
tm.stop();
double exttime = tm.getTimeMilli();
cout << "Extraction time: " << exttime << endl;
}
static void computeDescriptors( const Mat& queryImage, vector<KeyPoint>& queryKeypoints, Mat& queryDescriptors,
const vector<Mat>& trainImages, vector<vector<KeyPoint> >& trainKeypoints, vector<Mat>& trainDescriptors,
Ptr<DescriptorExtractor>& descriptorExtractor )
{
TickMeter tm;
tm.start();
cout << "< Computing descriptors for keypoints..." << endl;
descriptorExtractor->compute( queryImage, queryKeypoints, queryDescriptors );
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;
tm.stop();
double exttime = tm.getTimeMilli();
cout << "Description time: " << exttime << endl;
}
static void matchDescriptors( const Mat& queryDescriptors, const vector<Mat>& trainDescriptors,
vector<DMatch>& matches, Ptr<DescriptorMatcher>& descriptorMatcher )
{
cout << "< Set train descriptors collection in the matcher and match query descriptors to them..." << endl;
TickMeter tm;
tm.start();
descriptorMatcher->add( trainDescriptors );
descriptorMatcher->train();
tm.stop();
double buildTime = tm.getTimeMilli();
tm.start();
descriptorMatcher->match( queryDescriptors, matches );
tm.stop();
double matchTime = tm.getTimeMilli();
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;
}
int main(int argc, char** argv)
{
string detectorType = "SURF";
string descriptorType = "SURF";
string matcherType = "FlannBased";
string fileWithTrainImages = "/home/juan/workspace/keypoint/Debug/matching_to_many_images/train/keyframes.txt";
string dirToSaveResImages = "/home/juan/workspace/keypoint/Debug/matching_to_many_images/results";
string queryImageName = "/home/juan/workspace/keypoint/Debug/keyframe0.jpg";
Ptr<FeatureDetector> featureDetector;
Ptr<DescriptorExtractor> descriptorExtractor;
Ptr<DescriptorMatcher> descriptorMatcher;
createDetectorDescriptorMatcher( detectorType, descriptorType, matcherType, featureDetector, descriptorExtractor, descriptorMatcher );
Mat queryImage;
vector<Mat> trainImages;
vector<string> trainImagesNames;
if( !readImages( queryImageName, fileWithTrainImages, queryImage, trainImages, trainImagesNames ) )
{
cout << "Error!!" << endl;
return -1;
}
vector<KeyPoint> queryKeypoints;
vector<vector<KeyPoint> > trainKeypoints;
detectKeypoints( queryImage, queryKeypoints, trainImages, trainKeypoints, featureDetector );
Mat queryDescriptors;
vector<Mat> trainDescriptors;
computeDescriptors( queryImage, queryKeypoints, queryDescriptors,
trainImages, trainKeypoints, trainDescriptors,
descriptorExtractor );
vector<DMatch> matches;
matchDescriptors( queryDescriptors, trainDescriptors, matches, descriptorMatcher );
return 0;
}
该计划的主要功能是: 它读取查询图像和一组火车图像。然后,它使用建立的detectorType(SURF,SIFT,FAST ......)检测关键点。之后,它使用建立的descriptorType(FREAK,SIFT ...)提取这些关键点的描述符。最后,基于由变量matcherType(BruteForce,FlannBased ...)建立的匹配器,给出了查询图像和整个列车图像集之间的匹配。 我的问题是我试图使用OpenCV在其方法中提供的所有检测器,描述符和匹配器类型(变量detectorType,descriptorType,matcherType)。但是,我只得到了一些结果。例如,组合Detector-Descriptor-Matcher“SURF-SURF-FlannBased”对我不起作用,并且给出了以下错误:
juan @ juan-HP-G62-NoteBook-PC:〜/ workspace / keypoint / Debug $ ./keypoint&lt; 创建特征检测器,描述符提取器和描述符匹配器 ...
无法创建给定类型的特征检测器或描述符提取器或描述符匹配器。 &LT;阅读图像...阅读了77张火车图像。
&LT;从图像中提取关键点......Violacióndesegmento(`core' generado)
然而,FAST-FREAK-BruteForce的组合对我来说很好:
&LT;创建特征检测器,描述符提取器和描述符 匹配......
&LT;阅读图像...阅读了77张火车图像。
&LT;从图像中提取关键点......
提取时间:374.277&lt;计算关键点的描述符...查询描述符计数:786;列车描述符总数:154417 描述时间:1489.24&lt;在匹配器中设置火车描述符集合并将查询描述符与它们匹配...匹配数量:786 建造时间:0.024077毫秒;比赛时间:14292.6毫秒
我正在使用OpenCV 2.4.9,Eclipse作为IDE,Linux作为SO。谁能帮我?我对使用FlannBased匹配器来处理大型比较数据库非常感兴趣,因为BruteForce匹配器运行良好,但在这种情况下它很慢。谢谢!