OTB  9.0.0
Orfeo Toolbox
otbKNearestNeighborsMachineLearningModel.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 otbKNearestNeighborsMachineLearningModel_h
22 #define otbKNearestNeighborsMachineLearningModel_h
23 
24 #include "otbRequiresOpenCVCheck.h"
25 
26 #include "itkLightObject.h"
27 #include "itkFixedArray.h"
29 
30 #include "otbOpenCVUtils.h"
31 
32 namespace otb
33 {
34 template <class TInputValue, class TTargetValue>
35 class ITK_EXPORT KNearestNeighborsMachineLearningModel : public MachineLearningModel<TInputValue, TTargetValue>
36 {
37 public:
41  typedef itk::SmartPointer<Self> Pointer;
42  typedef itk::SmartPointer<const Self> ConstPointer;
43 
45  typedef typename Superclass::InputSampleType InputSampleType;
50  typedef typename Superclass::ConfidenceValueType ConfidenceValueType;
51  typedef typename Superclass::ProbaSampleType ProbaSampleType;
53  itkNewMacro(Self);
56 
61  itkGetMacro(K, int);
62  itkSetMacro(K, int);
64 
72  enum
73  {
76  KNN_MEDIAN
77  };
78 
80  itkGetMacro(DecisionRule, int);
81  itkSetMacro(DecisionRule, int);
83 
85  void Train() override;
86 
88  void Save(const std::string& filename, const std::string& name = "") override;
89 
91  void Load(const std::string& filename, const std::string& name = "") override;
92 
95 
97  bool CanReadFile(const std::string&) override;
98 
100  bool CanWriteFile(const std::string&) override;
102 
103 protected:
106 
108  ~KNearestNeighborsMachineLearningModel() override = default;
109 
111  TargetSampleType DoPredict(const InputSampleType& input, ConfidenceValueType* quality = nullptr, ProbaSampleType* proba = nullptr) const override;
112 
114  void PrintSelf(std::ostream& os, itk::Indent indent) const override;
115 
116 private:
117  KNearestNeighborsMachineLearningModel(const Self&) = delete;
118  void operator=(const Self&) = delete;
119 
120  cv::Ptr<cv::ml::KNearest> m_KNearestModel;
121 
122  int m_K;
123 
125 };
126 } // end namespace otb
127 
128 #ifndef OTB_MANUAL_INSTANTIATION
130 #endif
131 
132 #endif
otb::KNearestNeighborsMachineLearningModel::TargetValueType
Superclass::TargetValueType TargetValueType
Definition: otbKNearestNeighborsMachineLearningModel.h:47
otb::MachineLearningModel< TInputValue, TTargetValue >::TargetListSampleType
itk::Statistics::ListSample< TargetSampleType > TargetListSampleType
Definition: otbMachineLearningModel.h:92
otb::KNearestNeighborsMachineLearningModel::ProbaSampleType
Superclass::ProbaSampleType ProbaSampleType
Definition: otbKNearestNeighborsMachineLearningModel.h:51
otb::KNearestNeighborsMachineLearningModel::Superclass
MachineLearningModel< TInputValue, TTargetValue > Superclass
Definition: otbKNearestNeighborsMachineLearningModel.h:40
otb::KNearestNeighborsMachineLearningModel::InputValueType
Superclass::InputValueType InputValueType
Definition: otbKNearestNeighborsMachineLearningModel.h:44
otb::KNearestNeighborsMachineLearningModel::m_K
int m_K
Definition: otbKNearestNeighborsMachineLearningModel.h:122
otb::MachineLearningModel< TInputValue, TTargetValue >::InputListSampleType
itk::Statistics::ListSample< InputSampleType > InputListSampleType
Definition: otbMachineLearningModel.h:85
otb::KNearestNeighborsMachineLearningModel::InputListSampleType
Superclass::InputListSampleType InputListSampleType
Definition: otbKNearestNeighborsMachineLearningModel.h:46
otb::MachineLearningModel< TInputValue, TTargetValue >::InputValueType
MLMSampleTraits< TInputValue >::ValueType InputValueType
Definition: otbMachineLearningModel.h:83
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::KNearestNeighborsMachineLearningModel::KNN_MEAN
@ KNN_MEAN
Definition: otbKNearestNeighborsMachineLearningModel.h:75
otb::KNearestNeighborsMachineLearningModel::Self
KNearestNeighborsMachineLearningModel Self
Definition: otbKNearestNeighborsMachineLearningModel.h:39
otb::KNearestNeighborsMachineLearningModel::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbKNearestNeighborsMachineLearningModel.h:42
otb::KNearestNeighborsMachineLearningModel::ConfidenceValueType
Superclass::ConfidenceValueType ConfidenceValueType
Definition: otbKNearestNeighborsMachineLearningModel.h:50
otb::KNearestNeighborsMachineLearningModel::m_KNearestModel
cv::Ptr< cv::ml::KNearest > m_KNearestModel
Definition: otbKNearestNeighborsMachineLearningModel.h:120
otbMachineLearningModel.h
otb::KNearestNeighborsMachineLearningModel
Definition: otbKNearestNeighborsMachineLearningModel.h:35
otb::KNearestNeighborsMachineLearningModel::InputSampleType
Superclass::InputSampleType InputSampleType
Definition: otbKNearestNeighborsMachineLearningModel.h:45
otb::KNearestNeighborsMachineLearningModel::KNN_VOTING
@ KNN_VOTING
Definition: otbKNearestNeighborsMachineLearningModel.h:74
otb::KNearestNeighborsMachineLearningModel::TargetSampleType
Superclass::TargetSampleType TargetSampleType
Definition: otbKNearestNeighborsMachineLearningModel.h:48
otb::KNearestNeighborsMachineLearningModel::m_DecisionRule
int m_DecisionRule
Definition: otbKNearestNeighborsMachineLearningModel.h:124
otb::MachineLearningModel
MachineLearningModel is the base class for all classifier objects (SVM, KNN, Random Forests,...
Definition: otbMachineLearningModel.h:70
otbOpenCVUtils.h
otb::MachineLearningModel< TInputValue, TTargetValue >::TargetValueType
MLMTargetTraits< TTargetValue >::ValueType TargetValueType
Definition: otbMachineLearningModel.h:90
otbRequiresOpenCVCheck.h
otbKNearestNeighborsMachineLearningModel.hxx
otb::MachineLearningModel::TargetSampleType
MLMTargetTraits< TTargetValue >::SampleType TargetSampleType
Definition: otbMachineLearningModel.h:91
otb::KNearestNeighborsMachineLearningModel::TargetListSampleType
Superclass::TargetListSampleType TargetListSampleType
Definition: otbKNearestNeighborsMachineLearningModel.h:49
otb::KNearestNeighborsMachineLearningModel::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbKNearestNeighborsMachineLearningModel.h:41