原文:http://blog.csdn.net/wanglang3081/article/details/17791121
OpenCV在新版本中把很多C语言的代码都重新整理成了C++代码,让我们在使用的时候更加方便灵活。其中对于特征点的提取和匹配,充分体现了C++的强大。下面直接用例子来说明。假设我们有两幅图:1.bmp和2.bmp,要从中提取体征点并匹配,代码如下:
// Load image from file
IplImage *pLeftImage = cvLoadImage("1.bmp",CV_LOAD_IMAGE_GRAYSCALE);
IplImage *pRightImage = cvLoadImage("2.bmp",CV_LOAD_IMAGE_GRAYSCALE);
// Convert IplImage to cv::Mat
Mat matLeftImage = Mat(pLeftImage, false);
Mat matRightImage = Mat(pRightImage, false);
// Key point and its descriptor
vector<KeyPoint> LeftKey;
vector<KeyPoint> RightKey;
Mat LeftDescriptor;
Mat RightDescriptor;
vector<DMatch> Matches;
// Detect key points from image
FeatureDetector *pDetector = new SurfFeatureDetector; //这里我们用了SURF特征点
pDetector->detect(matLeftImage, LeftKey);
pDetector->detect(matRightImage, RightKey);
delete pDetector;
// Extract descriptors
DescriptorExtractor *pExtractor = new SurfDescriptorExtractor;// 提取SURF描述向量
pExtractor->compute(matLeftImage, LeftKey,LeftDescriptor);
pExtractor->compute(matRightImage, RightKey,RightDescriptor);
delete pExtractor;
// Matching features
DescriptorMatcher *pMatcher = new FlannBasedMatcher; //使用Flann匹配算法
pMatcher->match(LeftDescriptor, RightDescriptor,Matches);
delete pMatcher;
// Show result
Mat OutImage;
drawMatches(matLeftImage, LeftKey, matRightImage, RightKey,Matches, OutImage);
cvNamedWindow( "Match features", 1);
cvShowImage("Match features",&(IplImage(OutImage)));
cvWaitKey( 0 );
cvDestroyWindow( "Match features" );
1 FeatureDetector
要使用某一种检测器,可以直接调用FeatureDetector的工厂来创建,该工厂是一个静态方法,如下:
// Create feature detector by detector name.
static Ptr<FeatureDetector> create(const string& detectorType );
也可以像我的示例代码中那样显式的创建,如下:
FeatureDetector *pDetector = new SurfFeatureDetector;
可以用swich实现在多种方法中切换。
2 DescriptorExtractor
要使用某一种描述向量,可以调用DescriptorExtractor的工厂来创建,静态方法如下:
static Ptr<DescriptorExtractor>create( const string& descriptorExtractorType);
也可以像我的示例代码中那样显式的创建,如下:
DescriptorExtractor *pExtractor = newSurfDescriptorExtractor;
可以用swich实现在多种方法中切换。
3 DescriptorMatcher
匹配器可以由静态工厂方法直接创建,如下:
static Ptr<DescriptorMatcher>create( const string& descriptorMatcherType );
也可以像我的示例代码中那样显式的创建,如下:
DescriptorMatcher *pMatcher = new FlannBasedMatcher;
可以用swich实现在多种方法中切换。