diff --git a/Modules/Learning/Supervised/include/otbCvRTreesWrapper.h b/Modules/Learning/Supervised/include/otbCvRTreesWrapper.h index 290bd6af3239dcb56bc8ed83f0563f61e4936afb..c989bc36e22f811881ecf64982ff45118137774c 100644 --- a/Modules/Learning/Supervised/include/otbCvRTreesWrapper.h +++ b/Modules/Learning/Supervised/include/otbCvRTreesWrapper.h @@ -19,9 +19,6 @@ #define __otbCvRTreesWrapper_h #include "otbOpenCVUtils.h" -#include <vector> -#include <algorithm> - namespace otb { @@ -34,8 +31,8 @@ namespace otb class CV_EXPORTS_W CvRTreesWrapper : public CvRTrees { public: - CvRTreesWrapper(){}; - virtual ~CvRTreesWrapper(){}; + CvRTreesWrapper(); + virtual ~CvRTreesWrapper(); /** Predict the confidence of the classifcation by computing the difference in votes between the first and second most voted classes. @@ -45,29 +42,7 @@ public: */ float predict_confidence(const cv::Mat& sample, const cv::Mat& missing = - cv::Mat()) const - { - // Sanity check (division by ntrees later on) - if(ntrees == 0) - { - return 0.; - } - - std::vector<unsigned int> classVotes(nclasses); - for( int k = 0; k < ntrees; k++ ) - { - CvDTreeNode* predicted_node = trees[k]->predict( sample, missing ); - int class_idx = predicted_node->class_idx; - CV_Assert( 0 <= class_idx && class_idx < nclasses ); - ++classVotes[class_idx]; - } - // We only sort the 2 greatest elements - std::nth_element(classVotes.begin(), classVotes.begin()+1, - classVotes.end(), std::greater<unsigned int>()); - float confidence = static_cast<float>(classVotes[0]-classVotes[1])/ntrees; - return confidence; - }; - + cv::Mat()) const; }; } diff --git a/Modules/Learning/Supervised/src/CMakeLists.txt b/Modules/Learning/Supervised/src/CMakeLists.txt index 67598c94a143f5b6f16327889e648994b4e928db..bab85f52ac967624f58f55953f756bcffc83fcb0 100644 --- a/Modules/Learning/Supervised/src/CMakeLists.txt +++ b/Modules/Learning/Supervised/src/CMakeLists.txt @@ -1,4 +1,5 @@ set(OTBSupervised_SRC + otbCvRTreesWrapper.cxx otbMachineLearningModelFactoryBase.cxx otbMachineLearningUtils.cxx ) diff --git a/Modules/Learning/Supervised/src/otbCvRTreesWrapper.cxx b/Modules/Learning/Supervised/src/otbCvRTreesWrapper.cxx new file mode 100644 index 0000000000000000000000000000000000000000..9207e0d33753c30c55ff6fe4d355af84e550898d --- /dev/null +++ b/Modules/Learning/Supervised/src/otbCvRTreesWrapper.cxx @@ -0,0 +1,53 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#include "otbCvRTreesWrapper.h" +#include <vector> +#include <algorithm> + + +namespace otb +{ + +CvRTreesWrapper::CvRTreesWrapper(){} + +CvRTreesWrapper::~CvRTreesWrapper(){} + +float CvRTreesWrapper::predict_confidence(const cv::Mat& sample, + const cv::Mat& missing) const + { + // Sanity check (division by ntrees later on) + if(ntrees == 0) + { + return 0.; + } + + std::vector<unsigned int> classVotes(nclasses); + for( int k = 0; k < ntrees; k++ ) + { + CvDTreeNode* predicted_node = trees[k]->predict( sample, missing ); + int class_idx = predicted_node->class_idx; + CV_Assert( 0 <= class_idx && class_idx < nclasses ); + ++classVotes[class_idx]; + } + // We only sort the 2 greatest elements + std::nth_element(classVotes.begin(), classVotes.begin()+1, + classVotes.end(), std::greater<unsigned int>()); + float confidence = static_cast<float>(classVotes[0]-classVotes[1])/ntrees; + return confidence; + } +}