OTB  9.0.0
Orfeo Toolbox
otbCvRTreesWrapper.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbCvRTreesWrapper_h
22 #define otbCvRTreesWrapper_h
23 
24 #include "otbOpenCVUtils.h"
25 #include <vector>
26 
27 namespace otb
28 {
29 
35 class OTBSupervised_EXPORT CvRTreesWrapper
36  : public cv::ml::RTrees
37 {
38 public:
39  typedef std::vector<unsigned int> VotesVectorType;
41  ~CvRTreesWrapper() override = default;
42 
44  void get_votes(const cv::Mat& sample, const cv::Mat& missing, VotesVectorType& vote_count) const;
45 
49  float predict_confidence(const cv::Mat& sample, const cv::Mat& missing = cv::Mat()) const;
50 
57  float predict_margin(const cv::Mat& sample, const cv::Mat& missing = cv::Mat()) const;
58 
59 
60 #define OTB_CV_WRAP_PROPERTY(type, name) \
61  virtual type get##name() const override; \
62  virtual void set##name(type val) override;
63 
64 #define OTB_CV_WRAP_PROPERTY_REF(type, name) \
65  virtual type get##name() const override; \
66  virtual void set##name(const type& val) override;
67 
68 #define OTB_CV_WRAP_CSTREF_GET(type, name) virtual const type& get##name() const override;
69 
70  // TODO : wrap all method used
71  virtual int getVarCount() const override;
72 
73  virtual bool isTrained() const override;
74 
75  virtual bool isClassifier() const override;
76 
77  OTB_CV_WRAP_PROPERTY(int, MaxCategories)
78  OTB_CV_WRAP_PROPERTY(int, MaxDepth)
79  OTB_CV_WRAP_PROPERTY(int, MinSampleCount)
80  OTB_CV_WRAP_PROPERTY(bool, UseSurrogates)
81 
82  // warning: CV fold crash in openCV 3
83  OTB_CV_WRAP_PROPERTY(int, CVFolds)
84  OTB_CV_WRAP_PROPERTY(bool, Use1SERule)
85  OTB_CV_WRAP_PROPERTY(bool, TruncatePrunedTree)
86  OTB_CV_WRAP_PROPERTY(float, RegressionAccuracy)
87  OTB_CV_WRAP_PROPERTY(bool, CalculateVarImportance)
88  OTB_CV_WRAP_PROPERTY(int, ActiveVarCount)
89  OTB_CV_WRAP_PROPERTY_REF(cv::Mat, Priors)
90  OTB_CV_WRAP_PROPERTY_REF(cv::TermCriteria, TermCriteria)
91 
92  OTB_CV_WRAP_CSTREF_GET(std::vector<int>, Roots)
93  OTB_CV_WRAP_CSTREF_GET(std::vector<cv::ml::DTrees::Node>, Nodes)
94  OTB_CV_WRAP_CSTREF_GET(std::vector<cv::ml::DTrees::Split>, Splits)
95  OTB_CV_WRAP_CSTREF_GET(std::vector<int>, Subsets)
96 
97 #ifdef OTB_OPENCV_4
98  virtual void getVotes(cv::InputArray samples, cv::OutputArray results, int flags) const override;
99 #endif
100 
101  virtual cv::Mat getVarImportance() const override;
102 
103  virtual cv::String getDefaultName() const override;
104 
105  virtual void read(const cv::FileNode& fn) override;
106 
107  virtual void write(cv::FileStorage& fs) const override;
108 
109  virtual void save(const cv::String& filename) const override;
110 
111  virtual bool train(cv::InputArray samples, int layout, cv::InputArray responses) override;
112 
113  virtual bool train(const cv::Ptr<cv::ml::TrainData>& trainData, int flags = 0) override;
114 
115  virtual float predict(cv::InputArray samples, cv::OutputArray results = cv::noArray(), int flags = 0) const override;
116 
117  static cv::Ptr<CvRTreesWrapper> create();
118 
119 #undef OTB_CV_WRAP_PROPERTY
120 #undef OTB_CV_WRAP_PROPERTY_REF
121 #undef OTB_CV_WRAP_CSTREF_GET
122 
123 private:
124  cv::Ptr<cv::ml::RTrees> m_Impl;
125 };
126 }
127 
128 #endif
OTB_CV_WRAP_PROPERTY
#define OTB_CV_WRAP_PROPERTY(type, name)
Definition: otbCvRTreesWrapper.h:60
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
OTB_CV_WRAP_PROPERTY_REF
#define OTB_CV_WRAP_PROPERTY_REF(type, name)
Definition: otbCvRTreesWrapper.h:64
otb::CvRTreesWrapper
Wrapper for OpenCV Random Trees.
Definition: otbCvRTreesWrapper.h:35
otb::CvRTreesWrapper::VotesVectorType
std::vector< unsigned int > VotesVectorType
Definition: otbCvRTreesWrapper.h:39
otbOpenCVUtils.h
OTB_CV_WRAP_CSTREF_GET
#define OTB_CV_WRAP_CSTREF_GET(type, name)
Definition: otbCvRTreesWrapper.h:68
otb::CvRTreesWrapper::m_Impl
cv::Ptr< cv::ml::RTrees > m_Impl
Definition: otbCvRTreesWrapper.h:124