Request for Changes-87: Support OpenCV 3
Contents
[Request for Changes - 87] Support OpenCV 3
Status
- Author: Guillaume
- Additional Contributors (if different than authors)
- Submitted on 28.03.2017
- Proposed target release : 6.0
- Adopted : +3 from Victor, Guillaume, Rémi
- Merged : 6cf8f96f2c51c0e140a27d705bda2d6e23abf4a6
Summary
This RFC add the support of the OpenCV 3 API, while maintaining compatibility with older branch 2.4
Rationale
OpenCV 3 has been out for some time, and some platforms (such as Debian) are not packaging version 2.4 anymore.
Implementation details
Classes and files
M CMake/FindOpenCV.cmake
Detect properly OpenCV 3
M Modules/ThirdParty/OpenCV/CMakeLists.txt M Modules/ThirdParty/OpenCV/otb-module-init.cmake A Modules/ThirdParty/OpenCV/src/otb_opencv_api.h.in
The detection of version 3 is done during import, and the CMake variable OTB_OPENCV_3
is defined. The header otb_opencv_api.h is used to store the corresponding #define OTB_OPENCV_3
M Modules/Learning/Supervised/include/otbBoostMachineLearningModel.h M Modules/Learning/Supervised/include/otbBoostMachineLearningModel.txx M Modules/Learning/Supervised/include/otbCvRTreesWrapper.h M Modules/Learning/Supervised/include/otbDecisionTreeMachineLearningModel.h M Modules/Learning/Supervised/include/otbDecisionTreeMachineLearningModel.txx M Modules/Learning/Supervised/include/otbGradientBoostedTreeMachineLearningModel.h M Modules/Learning/Supervised/include/otbGradientBoostedTreeMachineLearningModel.txx M Modules/Learning/Supervised/include/otbGradientBoostedTreeMachineLearningModelFactory.h M Modules/Learning/Supervised/include/otbGradientBoostedTreeMachineLearningModelFactory.txx M Modules/Learning/Supervised/include/otbKNearestNeighborsMachineLearningModel.h M Modules/Learning/Supervised/include/otbKNearestNeighborsMachineLearningModel.txx M Modules/Learning/Supervised/include/otbMachineLearningModelFactory.txx M Modules/Learning/Supervised/include/otbNeuralNetworkMachineLearningModel.h M Modules/Learning/Supervised/include/otbNeuralNetworkMachineLearningModel.txx M Modules/Learning/Supervised/include/otbNormalBayesMachineLearningModel.h M Modules/Learning/Supervised/include/otbNormalBayesMachineLearningModel.txx M Modules/Learning/Supervised/include/otbOpenCVUtils.h M Modules/Learning/Supervised/include/otbRandomForestsMachineLearningModel.h M Modules/Learning/Supervised/include/otbRandomForestsMachineLearningModel.txx M Modules/Learning/Supervised/include/otbRequiresOpenCVCheck.h M Modules/Learning/Supervised/include/otbSVMMachineLearningModel.h M Modules/Learning/Supervised/include/otbSVMMachineLearningModel.txx M Modules/Learning/Supervised/src/otbCvRTreesWrapper.cxx
Lots of #ifdef have been used to maintain both 2.4 and 3.X compatibilities. The major changes in version 3 are :
- OTB wrappers now store a cv::Ptr<> instead of a plain pointer.
- There is no more parameter stucture associated with ML models, just setters for each parameter.
- The calls to train() function has changed.
- No more GradientBoostedTree model (it has not been implemented in OpenCV 3)
- DecisionTree has different default parameters to avoid severe crashes : MaxDepth=10, CVFolds=0 (cross validation is buggy in 3.2)
- The CvRTreesWrapper still derives the RTrees OpenCV class, but in version 3 all the implementation is hidden. As a consequence, some code has been re-written to perform margin and confidence estimation.
Applications
M Modules/Applications/AppClassification/include/otbLearningApplicationBase.h M Modules/Applications/AppClassification/include/otbLearningApplicationBase.txx M Modules/Applications/AppClassification/include/otbTrainBoost.txx M Modules/Applications/AppClassification/include/otbTrainDecisionTree.txx M Modules/Applications/AppClassification/include/otbTrainGradientBoostedTree.txx M Modules/Applications/AppClassification/include/otbTrainKNN.txx M Modules/Applications/AppClassification/include/otbTrainLibSVM.txx M Modules/Applications/AppClassification/include/otbTrainNeuralNetwork.txx M Modules/Applications/AppClassification/include/otbTrainNormalBayes.txx M Modules/Applications/AppClassification/include/otbTrainRandomForests.txx M Modules/Applications/AppClassification/include/otbTrainSVM.txx M Modules/Applications/AppClassification/include/otbTrainSharkRandomForests.txx
The LearningApplicationBase code has been simplified :
- Some code specific to the different machine learning models has been moved to the corresponding otbTrainXXXX.txx
- As a consequence, there are less #ifdef
- The OpenCV SVM model related function don't have to be commented. They can still exist in the class. The only line to comment is the call to
LearningApplicationBase::InitSVMParams()
inLearningApplicationBase::DoInit()
. This way, the "svm" choice is never available.
It is now more simple to add/remove models. And the GradientBoostedTree model is now disabled if OpenCV 3.
Tests
M Modules/Learning/Supervised/test/otbMachineLearningModelCanRead.cxx M Modules/Learning/Supervised/test/otbMachineLearningRegressionTests.cxx M Modules/Learning/Supervised/test/otbSupervisedTestDriver.cxx M Modules/Learning/Supervised/test/otbTrainMachineLearningModel.cxx M Modules/Learning/Supervised/test/tests-opencv.cmake M Modules/Applications/AppClassification/test/CMakeLists.txt
Tests involving GBTree are disabled if OpenCV 3. When needed, multi-baselines have been added to keep the tests passing (see corresponding branch on OTB-Data).
Documentation
- Indicate that both opencv versions are supported in third party dependencies list
Additional notes
At the moment, the state of ML module in OpenCV 3.2 is not good enough to foresee an immediate change to OpenCV 3 in SuperBuild. There are some known issues in DTree model for instance ( [1] ). In addition, version 2.4 has been more tested than 3.2, as most Dashboard platform still use 2.4.
The SuperBuild version of OpenCV has been updated to release 2.4.13