【opencv】特征点提取和匹配的通用方法

13 篇文章 0 订阅

2013:

 订阅

原文网址:http://cncncv.blog.163.com/blog/static/206181026201232444017471/

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); // Do not copy
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" );

 

    从上面的代码可以看见,用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实现在多种方法中切换。

 

3 DescriptorMatcher

    DescriptorMatcher是匹配器的抽象基类,其具体类有:

    class BruteForceMatcher

    class FlannBasedMatcher

匹配器可以由静态工厂方法直接创建,如下:

static Ptr<DescriptorMatcher> create( const string& descriptorMatcherType );

也可以像我的示例代码中那样显式的创建,如下:

DescriptorMatcher *pMatcher = new FlannBasedMatcher;

可以用swich实现在多种方法中切换。

 

    多态的使用,可以让我们对不同的特征采用相同的代码来编程,这是OpenCV设计的一种策略模式,大大地简化了代码量,并增加了灵活性,让我们可以在多种特征提取和匹配方法之间自由切换

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值