关于opencv3.x与opencv2.x的某些类的写法发生的变化

关于opencv3.x与opencv2.x的某些类的写法发生了变化

想了很久,要不要写一下这篇博客,因为我在学习opencv的时候,因为版本的不同,常常在自己写代码运行时,老是发生语法错误,老是上网去查找或去提问大神们,又需要等待,这样太浪费时间了,所以我总结了一下我在学习opencv时因版本不同而发生写法不同的体会,希望对大家有帮助,特别是初学者。


在学习BackgroundSubtractorMOG2,SurfFeatureDetector,SurfDescriptorExtractor,BruteForceMatcher时,opencv2和OpenCV3的写法发生了变化。


MOG2的库文件位置在2和3没有发生变化,依然是在#include "opencv2/video/background_segm.hpp"
在opencv2.x中,写法如下:
BackgroundSubtractorMOG2 bg_model;//类定义
bg_model(img, fgmask, update_bg_model ? -1 : 0);//类初始化
bg_model.getBackgroundImage(bgimg);//类函数使用

在opencv3.x中:
Ptr bg_model=createBackgroundSubtractorMOG2();//类定义
bg_model->apply(image, fgmask, -1);//类函数使用
bg_model->getBackgroundImage(bgimage);//类函数使用
**注意,类调用函数不能用".",只能用"->",否者会报错**

**但是SurfFeatureDetector在2和3的库文件位置反生了变化,在opencv2.x中在#include "opencv2/features2d/features2d.hpp"中,在opencv3.x中在#include "opencv2/xfeatures2d.hpp"中**注意:在添加文件库时,不能写成#include "opencv2/xfeatures2d/xfeatures2d.hpp",只能写#include "opencv2/xfeatures2d.hpp",否者会报错**

在opencv2.x中:
SurfFeatureDetector detector( minHessian );//类定义
detector.detect( srcImage1, keypoints_1 );//类函数使用

在opencv3.x中:
写完库文件后还要加上一句:using namespace cv::xfeatures2d;大家可能觉得我前面不是加了using namespace cv;了吗,为什么还要加using namespace cv::xfeatures2d;呢?它就是要加,这是没办法的事,否者会报错。
Ptrdetector = SURF::create(minHession);//类定义
detector->detect(srcimage1, keypoint1);//类函数使用
**注意:opencv3.x使用库xfeaturea2d,是需要加载OpenCV_contrib库的**

SurfDescriptorExtractor,BruteForceMatcher的情况和SurfFeatureDetector一样。
SurfDescriptorExtractor在opencv2.x中:
SurfDescriptorExtractor extractor;
Mat descriptors1, descriptors2;
extractor.compute( srcImage1, keyPoint1, descriptors1 );

在opencv3.x中:
Ptr extractor = SURF::create();
Mat descriptors1, descriptors2;
extractor->compute(srcimage1, keypoint1, descriptors1);  

BruteForceMatcher在opencv2.x中:
BruteForceMatcher< L2 > matcher;
std::vector< DMatch > matches;
matcher.match( descriptors1, descriptors2, matches );

在opencv3.x中:
Ptr matcher = DescriptorMatcher::create("BruteForce");
vector matches;
matcher->match(descriptors1, descriptors2, matches);

想加载OpenCV_contrib库,可以查看教程这里写链接内容

你可能感兴趣的:(opencv)