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

来源:互联网 发布:adc0808数据手册 编辑:程序博客网 时间:2024/06/07 01:02

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

    ORB orb;

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

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

Public Member Functions:virtual int     getEdgeThreshold () const =0virtual int     getFastThreshold () const =0virtual int     getFirstLevel () const =0virtual int     getMaxFeatures () const =0virtual int     getNLevels () const =0virtual int     getPatchSize () const =0virtual double  getScaleFactor () const =0virtual int     getScoreType () const =0virtual int     getWTA_K () const =0virtual void    setEdgeThreshold (int edgeThreshold)=0virtual void    setFastThreshold (int fastThreshold)=0virtual void    setFirstLevel (int firstLevel)=0virtual void    setMaxFeatures (int maxFeatures)=0virtual void    setNLevels (int nlevels)=0virtual void    setPatchSize (int patchSize)=0virtual void    setScaleFactor (double scaleFactor)=0virtual void    setScoreType (int scoreType)=0virtual 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(rgbd1, 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

2 0
原创粉丝点击