OpenCV中特征点提取和匹配的通用方法

OpenCV在新版本中把很多C语言的代码都重新整理成了C++代码,让我们在使用的时候更加方便灵活。其中对于特征点的提取和匹配,充分体现了C++的强大。下面直接用例子来说明。假设我们有两幅图:1.bmp和2.bmp,要从中提取体征点并匹配,代码如下:// Load image from fileIplImage *pLeftImage = cvLoadImage("1.bmp", CV_LOAD_IMAGE_GRAYSCALE);IplImage *pRightImage = cvLoadImage("2.bmp", CV_LOAD_IMAGE_GRAYSCALE);// Convert IplImage to cv::MatMat matLeftImage = Mat(pLeftImage, false); // Do not copyMat matRightImage = Mat(pRightImage, false);// Key point and its descriptorvector<KeyPoint> LeftKey;vector<KeyPoint> RightKey;Mat LeftDescriptor;Mat RightDescriptor;vector<DMatch> Matches;// Detect key points from imageFeatureDetector *pDetector = new SurfFeatureDetector; // 这里我们用了SURF特征点pDetector->detect(matLeftImage, LeftKey);pDetector->detect(matRightImage, RightKey);delete pDetector;// Extract descriptorsDescriptorExtractor *pExtractor = new SurfDescriptorExtractor; // 提取SURF描述向量pExtractor->compute(matLeftImage, LeftKey, LeftDescriptor);pExtractor->compute(matRightImage, RightKey, RightDescriptor);delete pExtractor;// Matching featuresDescriptorMatcher *pMatcher = new FlannBasedMatcher; // 使用Flann匹配算法pMatcher->match(LeftDescriptor, RightDescriptor, Matches);delete pMatcher;// Show resultMat OutImage;drawMatches(matLeftImage, LeftKey, matRightImage, RightKey, Matches, OutImage);cvNamedWindow( "Match features", 1);cvShowImage("Match features", &(IplImage(OutImage)));cvWaitKey( 0 );cvDestroyWindow( "Match features" ); 从上面的代码可以看见,用OpenCV来做特征提取匹配相当简便,出去读图和显示结果的代码,真正核心的部分只有3段代码,分别是检测关键点,提取描述向量和特征匹配,一共只有11行代码。 在我的示例代码中,使用的是SURF特征,而在OpenCV中,实现了很多种特征,,如SIFT,FAST等,这些特征的实现各不相同,但是都是从一个公共抽象基类派生出来的,因此可以用多态方便地切换特征提取算法。下面我将详细地说明。1 FeatureDetector FeatureDetector是关键点检测类的抽象基类,其已经实现的具体类有: class FastFeatureDetector class GoodFeaturesToTrackDetector class MserFeatureDetector class StarFeatureDetector class SiftFeatureDetector class SurfFeatureDetector要使用某一种检测器,可以直接调用FeatureDetector的工厂来创建,该工厂是一个静态方法,如下:// Create feature detector by detector name.static Ptr<FeatureDetector> create( const string& detectorType );也可以像我的示例代码中那样显式的创建,如下:FeatureDetector *pDetector = new SurfFeatureDetector;可以用swich实现在多种方法中切换。2 DescriptorExtractor DescriptorExtractor是提取关键点的描述向量类抽象基类,其具体类有: class SiftDescriptorExtractor class SurfDescriptorExtractor class CalonderDescriptorExtractor class BriefDescriptorExtractor class OpponentColorDescriptorExtractor要使用某一种描述向量,可以调用DescriptorExtractor的工厂来创建,静态方法如下:static Ptr<DescriptorExtractor> create( const string& descriptorExtractorType );也可以像我的示例代码中那样显式的创建,如下:DescriptorExtractor *pExtractor = new SurfDescriptorExtractor;可以用swich实现在多种方法中切换。

想象困难做出的反应,不是逃避或绕开它们,

OpenCV中特征点提取和匹配的通用方法

相关文章:

你感兴趣的文章:

标签云: