otbCvRTreesWrapper.h 4.19 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
 *
 * This file is part of Orfeo Toolbox
 *
 *     https://www.orfeo-toolbox.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
20

21 22
#ifndef otbCvRTreesWrapper_h
#define otbCvRTreesWrapper_h
23 24

#include "otbOpenCVUtils.h"
25
#include <vector>
26 27 28 29 30 31 32 33 34

namespace otb
{

/** \class CvRTreesWrapper
 * \brief Wrapper for OpenCV Random Trees
 *
 * \ingroup OTBSupervised
 */
35
class OTBSupervised_EXPORT CvRTreesWrapper
36 37 38
#ifdef OTB_OPENCV_3
  : public cv::ml::RTrees
#else
39 40
  : public CvRTrees
#endif
41 42
{
public:
43
  typedef std::vector<unsigned int> VotesVectorType;
44
  CvRTreesWrapper();
45
  ~CvRTreesWrapper() override;
46 47

  /** Compute the number of votes for each class. */
48
  void get_votes(const cv::Mat& sample,
49 50
                 const cv::Mat& missing,
                 VotesVectorType& vote_count) const;
51

52 53 54
  /** Predict the confidence of the classifcation by computing the proportion
      of trees which voted for the majority class.
  */
55 56
  float predict_confidence(const cv::Mat& sample,
                           const cv::Mat& missing =
57 58
                           cv::Mat()) const;

59
  /** Predict the confidence margin of the classifcation by computing the
60 61 62 63 64
      difference in votes between the first and second most voted classes.
      This measure is preferred to the proportion of votes of the majority
      class, since it provides information about the conflict between the
      most likely classes.
  */
65 66
  float predict_margin(const cv::Mat& sample,
                          const cv::Mat& missing =
67
                          cv::Mat()) const;
68 69

#ifdef OTB_OPENCV_3
70 71

#define OTB_CV_WRAP_PROPERTY(type,name) \
72 73
  virtual type get##name() const override; \
  virtual void set##name(type val) override;
74 75

#define OTB_CV_WRAP_PROPERTY_REF(type,name) \
76 77
  virtual type get##name() const override; \
  virtual void set##name(const type &val) override;
78 79

#define OTB_CV_WRAP_CSTREF_GET(type, name) \
80
  virtual const type& get##name() const override;
81 82

  // TODO : wrap all method used
83
  virtual int getVarCount() const override;
84

85
  virtual bool isTrained() const override;
86

87
  virtual bool isClassifier() const override;
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

  OTB_CV_WRAP_PROPERTY(int, MaxCategories)
  OTB_CV_WRAP_PROPERTY(int, MaxDepth)
  OTB_CV_WRAP_PROPERTY(int, MinSampleCount)
  OTB_CV_WRAP_PROPERTY(bool, UseSurrogates)
  OTB_CV_WRAP_PROPERTY(int, CVFolds)
  OTB_CV_WRAP_PROPERTY(bool, Use1SERule)
  OTB_CV_WRAP_PROPERTY(bool,TruncatePrunedTree)
  OTB_CV_WRAP_PROPERTY(float, RegressionAccuracy)
  OTB_CV_WRAP_PROPERTY(bool, CalculateVarImportance)
  OTB_CV_WRAP_PROPERTY(int, ActiveVarCount)
  OTB_CV_WRAP_PROPERTY_REF(cv::Mat, Priors)
  OTB_CV_WRAP_PROPERTY_REF(cv::TermCriteria, TermCriteria)
  
  OTB_CV_WRAP_CSTREF_GET(std::vector<int>, Roots)
  OTB_CV_WRAP_CSTREF_GET(std::vector<cv::ml::DTrees::Node>, Nodes)
  OTB_CV_WRAP_CSTREF_GET(std::vector<cv::ml::DTrees::Split>, Splits)
  OTB_CV_WRAP_CSTREF_GET(std::vector<int>, Subsets)

107
  virtual cv::Mat getVarImportance() const override;
108

109
  virtual cv::String 	getDefaultName () const override;
110

111
  virtual void 	read (const cv::FileNode &fn) override;
112

113
  virtual void write (cv::FileStorage &fs) const override;
114

115
  virtual void 	save (const cv::String &filename) const override;
116

117
  virtual bool train(cv::InputArray samples, int layout, cv::InputArray responses) override;
118

119
  virtual bool train( const cv::Ptr<cv::ml::TrainData>& trainData, int flags=0 ) override;
120

121
  virtual float predict (cv::InputArray samples, cv::OutputArray results=cv::noArray(), int flags=0) const override;
122 123 124 125 126 127 128 129 130
  
  static cv::Ptr<CvRTreesWrapper> create();

#undef OTB_CV_WRAP_PROPERTY
#undef OTB_CV_WRAP_PROPERTY_REF
#undef OTB_CV_WRAP_CSTREF_GET

private:
  cv::Ptr<cv::ml::RTrees> m_Impl;
131
#endif // OTB_OPENCV_3
132 133
};

134
}
135 136

#endif