W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
本文檔面向希望將代碼遷移到OpenCV 3.0的軟件開發(fā)人員。
與2.4版本相比,OpenCV 3.0引入了許多新的算法和功能。一些模塊已被重寫,有些模塊已經(jīng)重組。雖然2.4中的大部分算法仍然存在,但接口可能不同。
本節(jié)介紹一般最顯著的更改,所有細節(jié)和過渡操作的示例在文檔的下一部分。
https://github.com/opencv/opencv_contrib
這是所有新的,實驗的和非免費的算法的地方。與主庫相比,支持小組沒有受到太多的關注,但社區(qū)努力保持良好的狀態(tài)。
要使用contrib存儲庫構建OpenCV ,請將以下選項添加到cmake命令中:
-DOPENCV_EXTRA_MODULES_PATH=<path-to-opencv_contrib>/modules
在2.4中,所有頭都位于相應的模塊子文件夾(opencv2 / <module> / <module> .hpp)中,3.0中有頂級模塊頭,其中包含大部分模塊功能:opencv2 / <module> .hpp和all C風格的API定義已被移動到單獨的標題(例如opencv2 / core / core_c.h)。
一般算法使用模式已經(jīng)改變了:現(xiàn)在它必須在包裹在智能指針cv :: Ptr的堆上創(chuàng)建。版本2.4允許堆棧和堆分配,直接或通過智能指針。
get和set方法已經(jīng)從cv :: Algorithm類以及CV_INIT_ALGORITHM宏中刪除。在3.0中,所有屬性都已轉換為getProperty / setProperty純虛擬方法。因此,它是不是能夠創(chuàng)建和使用CV ::算法通過名稱實例(使用通用的算法::創(chuàng)建(字符串)方法),應該顯式調(diào)用相應的工廠方法。
本節(jié)通過實例介紹具體的操作。
在最新的2.4.11 OpenCV版本中進行的一些更改允許您準備當前的代碼庫進行遷移:
注意: OpenCV 3.0中進行了旨在簡化遷移的更改,因此不需要以下說明,但建議。
// old header
#include "opencv2/<module>/<module>.hpp"
// new header
#include "opencv2/<module>.hpp"
// good ways
Ptr<SomeAlgo> algo = makePtr<SomeAlgo>(...);
Ptr<SomeAlgo> algo = SomeAlgo::create(...);
其他方式已棄用:
// bad ways
Ptr<SomeAlgo> algo = new SomeAlgo(...);
SomeAlgo * algo = new SomeAlgo(...);
SomeAlgo algo(...);
Ptr<SomeAlgo> algo = Algorithm::create<SomeAlgo>("name");
// good way
double clipLimit = clahe->getClipLimit();
clahe->setClipLimit(clipLimit);
// bad way
double clipLimit = clahe->getDouble("clipLimit");
clahe->set("clipLimit", clipLimit);
clahe->setDouble("clipLimit", clipLimit);
由于該模塊已被重寫,因此需要花費一些時間來適應您的軟件。所有算法都位于單獨的ml命名空間及其基類StatModel中。單獨的SomeAlgoParams類已被替換為一組相應的getProperty / setProperty方法。
下表說明了2.4和3.0機器學習類之間的對應關系。
2.4 | 3.0 |
---|---|
CvStatModel | CV ::毫升:: StatModel |
CvNormalBayesClassifier | CV ::毫升:: NormalBayesClassifier |
CvKNearest | CV ::毫升:: KNearest |
CvSVM | CV ::毫升:: SVM |
CvDTree | CV ::毫升:: DTrees |
CV升壓 | CV ::毫升::升壓 |
CvGBTrees | 未實現(xiàn) |
CvRTrees | CV ::毫升::的rtrees |
CvERTrees | 未實現(xiàn) |
EM | CV ::毫升:: EM |
CvANN_MLP | CV ::毫升:: ANN_MLP |
未實現(xiàn) | CV ::毫升::邏輯回歸 |
CvMLData | CV ::毫升:: TrainData |
雖然在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模塊類的列表:
需要執(zhí)行以下步驟:
一些類現(xiàn)在使用一般方法detect,compute或detectAndCompute由Feature2D基類而不是自定義提供operator()
以下代碼片段說明了區(qū)別(從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實現(xiàn)都隱藏在一般的C ++算法接口之后?,F(xiàn)在可以在運行時動態(tài)選擇功能執(zhí)行路徑:CPU或OpenCL; 這個機制也被稱為“透明API”。
新類cv :: UMat旨在以方便的方式隱藏與OpenCL設備的數(shù)據(jù)交換。
以下示例說明了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模塊已經(jīng)分成幾個較小的部分:
gpu命名空間已被刪除,使用cv :: cuda命名空間。許多課程也已經(jīng)重新命名,例如:
文檔已轉換為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
通過檢查構建系統(tǒng)中的庫版本,可以鏈接不同的模塊或啟用/禁用應用程序中的某些功能??梢允褂脴藴蔯make或pkg-config變量:
例:
if(OpenCV_VERSION VERSION_LESS "3.0")
# use 2.4 modules
else()
# use 3.x modules
endif()
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: