Opencv学习笔记(二)———Opencv3中ORB算法的使用

5 篇文章 0 订阅
2 篇文章 0 订阅

相信很多小伙伴在使用ORB算法的时候,一般会从网上搜一些代码作为参考,那么问题来了:在好多ORB程序中都会这么写:

	ORB orb;

如果你使用的是Opencv3的版本,编译器就会报错:ORB是一个纯虚类,无法进行实例化。但在opencv2的版本中可以正常使用。这是为什么呢?

于是乎就在opencv3官方的Documents中寻找答案,ORB属于features2d模块中。在它的文档中终于发现了原因:

Public Member Functions:
virtual int 	getEdgeThreshold () const =0
virtual int 	getFastThreshold () const =0
virtual int 	getFirstLevel () const =0
virtual int 	getMaxFeatures () const =0
virtual int 	getNLevels () const =0
virtual int 	getPatchSize () const =0
virtual double 	getScaleFactor () const =0
virtual int 	getScoreType () const =0
virtual int 	getWTA_K () const =0
virtual void 	setEdgeThreshold (int edgeThreshold)=0
virtual void 	setFastThreshold (int fastThreshold)=0
virtual void 	setFirstLevel (int firstLevel)=0
virtual void 	setMaxFeatures (int maxFeatures)=0
virtual void 	setNLevels (int nlevels)=0
virtual void 	setPatchSize (int patchSize)=0
virtual void 	setScaleFactor (double scaleFactor)=0
virtual void 	setScoreType (int scoreType)=0
virtual void 	setWTA_K (int wta_k)=0

what’s that!!!成员函数居然全是virtual声明的虚函数。好吧,看来ORB是一个纯虚类,那么问题又来了:我们该怎么用啊?

接下来又看到一个很有意思的东西:

Static Public Member Functions:
static Ptr< ORB > 	create (int nfeatures=500, float scaleFactor=1.2f, int nlevels=8, int edgeThreshold=31, int firstLevel=0, int WTA_K=2, int scoreType=ORB::HARRIS_SCORE, int patchSize=31, int fastThreshold=20)

原来人家提供了一个静态成员函数来供我们使用,而且还是用了一个C++的新特性:智能指针Ptr,使用智能指针有什么好处呢?最方便的就是在我们使用new申请动态内存的时候,不需要用delete释放,系统会自动释放。(厉害了我的哥,从此妈妈再也不用担心我的内存问题了!!!)

废话不多说,赶紧撸个程序来体验一把:

#include<iostream>
#include<vector>

#include<opencv2\core\core.hpp>
#include<opencv2\features2d\features2d.hpp>
#include<opencv2\highgui\highgui.hpp>

using namespace std;
using namespace cv;


int main()
{
	//读取图片
	Mat rgbd1 = imread("自己的图片Path");
	Mat rgbd2 = imread("自己的图片Path");
	//imshow("rgbd1", depth2);
	//waitKey(0);
	Ptr<ORB> orb = ORB::create();
	vector<KeyPoint> Keypoints1,Keypoints2;
	Mat descriptors1,descriptors2;
	orb->detectAndCompute(rgbd1, Mat(), Keypoints1, descriptors1);
	orb->detectAndCompute(rgbd2, Mat(), Keypoints2, descriptors2);
	
	//cout << "Key points of image" << Keypoints.size() << endl;
	
	//可视化,显示关键点
	Mat ShowKeypoints1, ShowKeypoints2;
	drawKeypoints(rgbd1,Keypoints1,ShowKeypoints1);
	drawKeypoints(rgbd2, Keypoints2, ShowKeypoints2);
	imshow("Keypoints1", ShowKeypoints1);
	imshow("Keypoints2", ShowKeypoints2);
	waitKey(0);

	//Matching
	vector<DMatch> matches;
	Ptr<DescriptorMatcher> matcher =DescriptorMatcher::create("BruteForce");
	matcher->match(descriptors1, descriptors2, matches);
	cout << "find out total " << matches.size() << " matches" << endl;

	//可视化
	Mat ShowMatches;
	drawMatches(rgbd1,Keypoints1,rgbd2,Keypoints2,matches,ShowMatches);
	imshow("matches", ShowMatches);
	waitKey(0);


	return 0;
}

这是一个对两幅图片进行ORB提取特征,并进行Match的程序。可以看到对于orb的使用程序变成了:

Ptr<ORB> orb = ORB::create();

细心的读者应该已经发现Match类也变成了一个纯虚类使用方法与ORB类似:

Ptr<DescriptorMatcher> matcher =DescriptorMatcher::create("BruteForce");

下面是关于create函数:

static Ptr<ORB> cv::ORB::create	(	
		int 	nfeatures = 500,
		float 	scaleFactor = 1.2f,
		int 	nlevels = 8,
		int 	edgeThreshold = 31,
		int 	firstLevel = 0,
		int 	WTA_K = 2,
		int 	scoreType = ORB::HARRIS_SCORE,
		int 	patchSize = 31,
		int 	fastThreshold = 20 
)		

下面是关于具体参数的解释有点多,就附上网址吧:

http://docs.opencv.org/3.2.0/db/d95/classcv_1_1ORB.html#adc371099dc902a9674bd98936e79739c

  • 24
    点赞
  • 85
    收藏
    觉得还不错? 一键收藏
  • 14
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值