与版本2.4相比,OpenCV 3.0引入了许多新算法和功能。有些模块已被重写,有些已经重组。尽管2.4中的大多数算法仍然存在,但接口可能不一样。本节描述了通常性的最显着变化,过渡操做的全部细节和示例都在本文档的下一部分中。html
https://github.com/opencv/opencv_contribgit
这是一个适用于全部新算法,实验算法和非免费算法的地方。与主存储库相比,它没有获得支持团队的太多关注,但社区努力使其保持良好状态。(即OpenCV的附加库,人脸识别等一些附加的功能可能须要用到这个库)github
要使用contrib存储库构建OpenCV ,请将如下选项添加到cmake命令:算法
-DOPENCV_EXTRA_MODULES_PATH=<path-to-opencv_contrib>/modules
或者参考个人博客为opencv添加contrib库。机器学习
在2.4中,全部头文件都位于相应的模块子文件夹(opencv2 / <module> / <module> .hpp)中,在3.0中有顶级模块头文件,其中包含大部分模块功能:opencv2 / <module> .hpp和全部 C语言风格的API定义已移至单独的标头(例如opencv2 / core / core_c.h)。ide
通用算法使用模式已更改:如今必须在包装在智能指针cv :: Ptr中的堆上建立。版本2.4容许直接或经过智能指针进行堆栈和堆分配。函数
已经从cv :: Algorithm类中删除了get和set方法以及CV_INIT_ALGORITHM宏。在3.0中,全部属性都已转换为getProperty / setProperty纯虚拟方法对。所以,它是不是可以建立和使用CV ::算法经过名称实例(使用通用的算法::建立(字符串)方法),应该显式调用相应的函数方法。oop
因为此模块已被重写,所以须要花费一些精力才能使您的软件适应它。全部算法都与其基类StatModel一块儿位于单独的ml命名空间中。单独的SomeAlgoParams类已被一组相应的getProperty / setProperty方法替换。布局
下表说明了2.4和3.0机器学习类之间的对应关系。post
尽管3.0中重写的ml算法容许您从xml / yml文件加载旧的训练模型,但预测过程当中的误差是可能的。
points_classifier.cpp
示例中的如下代码片断说明了模型培训过程当中的差别:
using namespace cv; // ======== version 2.4 ======== Mat trainSamples, trainClasses; prepare_train_data( trainSamples, trainClasses ); CvBoost boost; Mat var_types( 1, trainSamples.cols + 1, CV_8UC1, Scalar(CV_VAR_ORDERED) ); var_types.at<uchar>( trainSamples.cols ) = CV_VAR_CATEGORICAL; CvBoostParams params( CvBoost::DISCRETE, // boost_type 100, // weak_count 0.95, // weight_trim_rate 2, // max_depth false, //use_surrogates 0 // priors ); boost.train( trainSamples, CV_ROW_SAMPLE, trainClasses, Mat(), Mat(), var_types, Mat(), params ); // ======== version 3.0 ======== Ptr<Boost> boost = Boost::create(); boost->setBoostType(Boost::DISCRETE); boost->setWeakCount(100); boost->setWeightTrimRate(0.95); boost->setMaxDepth(2); boost->setUseSurrogates(false); boost->setPriors(Mat()); boost->train(prepare_train_data()); // 'prepare_train_data' returns an instance of ml::TrainData class
一些算法(FREAK,BRIEF,SIFT,SURF)已移至opencv_contrib存储库,xfeatures2d模块,xfeatures2d命名空间。它们的接口也已更改(从cv::Feature2D
基类继承)。
xfeatures2d模块类列表:
须要如下步骤:
opencv2/xfeatures2d.h
标题xfeatures2d
operator()
呼叫detect
,compute
或者detectAndCompute
若是须要有些类如今使用通用方法detect
,compute
或者detectAndCompute
由Feature2D
基类而不是自定义提供operator()
如下代码片断说明了差别(来自video_homography.cpp
示例):
using namespace cv; // ====== 2.4 ======= #include "opencv2/features2d/features2d.hpp" BriefDescriptorExtractor brief(32); GridAdaptedFeatureDetector detector(new FastFeatureDetector(10, true), DESIRED_FTRS, 4, 4); // ... detector.detect(gray, query_kpts); //Find interest points brief.compute(gray, query_kpts, query_desc); //Compute brief descriptors at each keypoint location // ====== 3.0 ======= #include "opencv2/features2d.hpp" #include "opencv2/xfeatures2d.hpp" using namespace cv::xfeatures2d; Ptr<BriefDescriptorExtractor> brief = BriefDescriptorExtractor::create(32); Ptr<FastFeatureDetector> detector = FastFeatureDetector::create(10, true); // ... detector->detect(gray, query_kpts); //Find interest points brief->compute(gray, query_kpts, query_desc); //Compute brief descriptors at each keypoint location
全部专门的ocl
实现都隐藏在通用C ++算法接口以后。如今能够在运行时动态选择函数执行路径:CPU或OpenCL; 这种机制也称为“透明API”。
新类cv :: UMat旨在以方便的方式隐藏与OpenCL设备的数据交换。
如下示例说明了API修改(来自OpenCV站点):
// initialization VideoCapture vcap(...); ocl::OclCascadeClassifier fd("haar_ff.xml"); ocl::oclMat frame, frameGray; Mat frameCpu; vector<Rect> faces; for(;;){ // processing loop vcap >> frameCpu; frame = frameCpu; ocl::cvtColor(frame, frameGray, BGR2GRAY); ocl::equalizeHist(frameGray, frameGray); fd.detectMultiScale(frameGray, faces, ...); // draw rectangles … // show image … }
// initialization VideoCapture vcap(...); CascadeClassifier fd("haar_ff.xml"); UMat frame, frameGray; // the only change from plain CPU version vector<Rect> faces; for(;;){ // processing loop vcap >> frame; cvtColor(frame, frameGray, BGR2GRAY); equalizeHist(frameGray, frameGray); fd.detectMultiScale(frameGray, faces, ...); // draw rectangles … // show image … }
cuda模块已分红几个小块:
gpu
命名空间已被删除,请改用cv :: cuda命名空间。许多类也已重命名,例如:
gpu::FAST_GPU
- > cv :: cuda :: FastFeatureDetectorgpu::createBoxFilter_GPU
- > cv :: cuda :: createBoxFilter文档已转换为Doxygen格式。您能够在OpenCV参考文档的教程部分(OpenCV的编写文档)中找到更新的文档编写指南。
在某些状况下,能够支持两种版本的OpenCV。
要检查应用程序源代码中的库主要版本,应使用如下方法:
#include "opencv2/core/version.hpp" #if CV_MAJOR_VERSION == 2 // do opencv 2 code #elif CV_MAJOR_VERSION == 3 // do opencv 3 code #endif
注意不要使用CV_VERSION_MAJOR,它对2.4和3.x分支有不一样的含义!
经过检查编译系统中的库版本,能够连接不一样的模块或启用/禁用应用程序中的某些功能。标准的cmake或pkg-config变量可用于此:
OpenCV_VERSION
对于cmake,将包含完整版本:例如“2.4.11”或“3.0.0”OpenCV_VERSION_MAJOR
对于cmake,将仅包含主要版本号:2或3Version
例:
if(OpenCV_VERSION VERSION_LESS "3.0") # use 2.4 modules else() # use 3.x modules endif()