OTB  9.0.0
Orfeo Toolbox
otbListSampleGenerator.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 
22 #ifndef otbListSampleGenerator_h
23 #define otbListSampleGenerator_h
24 
25 #include "itkProcessObject.h"
26 #include "itkListSample.h"
27 #include "itkPreOrderTreeIterator.h"
28 #include "itkMersenneTwisterRandomVariateGenerator.h"
29 #include <string>
30 
31 namespace otb
32 {
53 template <class TImage, class TVectorData>
54 class ITK_EXPORT ListSampleGenerator : public itk::ProcessObject
55 {
56 public:
59  typedef itk::ProcessObject Superclass;
60  typedef itk::SmartPointer<Self> Pointer;
61  typedef itk::SmartPointer<const Self> ConstPointer;
62 
64  itkTypeMacro(ListSampleGenerator, itk::ProcessObject);
65 
67  itkNewMacro(Self);
68 
69  typedef TImage ImageType;
70  typedef typename ImageType::Pointer ImagePointerType;
71  typedef typename ImageType::IndexType ImageIndexType;
72  typedef typename ImageType::RegionType ImageRegionType;
73  typedef TVectorData VectorDataType;
74  typedef typename VectorDataType::Pointer VectorDataPointerType;
75  typedef itk::ProcessObject::DataObjectPointerArraySizeType DataObjectPointerArraySizeType;
76 
78  typedef typename ImageType::PixelType SampleType;
79  typedef itk::Statistics::ListSample<SampleType> ListSampleType;
80  typedef typename ListSampleType::Pointer ListSamplePointerType;
81 
83  typedef int ClassLabelType;
84  typedef itk::FixedArray<ClassLabelType, 1> LabelType; // note could be templated by an std:::string
85  typedef itk::Statistics::ListSample<LabelType> ListLabelType;
86  typedef typename ListLabelType::Pointer ListLabelPointerType;
87 
89  using Superclass::SetInput;
90  void SetInput(const ImageType*);
91  const ImageType* GetInput() const;
93 
97  void SetInputVectorData(const VectorDataType*);
98  const VectorDataType* GetInputVectorData() const;
100 
101  // Build the outputs
102  typedef itk::DataObject::Pointer DataObjectPointer;
103  DataObjectPointer MakeOutput(DataObjectPointerArraySizeType idx) override;
104  using Superclass::MakeOutput;
105 
106  // virtual void Update();
107 
109  itkGetConstMacro(MaxTrainingSize, long int);
110  itkSetMacro(MaxTrainingSize, long int);
111  itkGetConstMacro(PolygonEdgeInclusion, bool);
112  itkSetMacro(PolygonEdgeInclusion, bool);
113  itkGetConstMacro(MaxValidationSize, long int);
114  itkSetMacro(MaxValidationSize, long int);
115  itkGetConstMacro(ValidationTrainingProportion, double);
116  itkSetClampMacro(ValidationTrainingProportion, double, 0.0, 1.0);
117  itkGetConstMacro(BoundByMin, bool);
118  itkSetMacro(BoundByMin, bool);
120 
121  itkGetConstMacro(NumberOfClasses, unsigned short);
122  typedef std::map<ClassLabelType, int> SampleNumberType;
123 
125  {
126  return m_ClassesSamplesNumberTraining;
127  }
128 
130  {
131  return m_ClassesSamplesNumberValidation;
132  }
133 
134  itkGetStringMacro(ClassKey);
135  itkSetStringMacro(ClassKey);
136  itkGetConstMacro(ClassMinSize, double);
137 
139  ListSampleType* GetTrainingListSample();
140 
142  ListLabelType* GetTrainingListLabel();
143 
145  ListSampleType* GetValidationListSample();
146 
148  ListLabelType* GetValidationListLabel();
149 
150  // Get the map size
151  std::map<ClassLabelType, double> GetClassesSize() const
152  {
153  return m_ClassesSize;
154  }
155 
156 protected:
159  {
160  }
161  void PrintSelf(std::ostream& os, itk::Indent indent) const override;
162 
164  void GenerateData(void) override;
165 
166  void GenerateInputRequestedRegion(void) override;
167 
169  void GenerateClassStatistics();
170 
171 private:
172  ListSampleGenerator(const Self&) = delete;
173  void operator=(const Self&) = delete;
174 
175  typedef typename VectorDataType::DataNodeType DataNodeType;
176  typedef typename DataNodeType::PolygonType PolygonType;
177  typedef typename DataNodeType::PolygonPointerType PolygonPointerType;
178  typedef typename DataNodeType::PolygonListType PolygonListType;
179  typedef typename DataNodeType::PolygonListPointerType PolygonListPointerType;
180  typedef itk::PreOrderTreeIterator<typename VectorDataType::DataTreeType> TreeIteratorType;
181 
182  void ComputeClassSelectionProbability();
183 
184  // Crop the polygon wrt the image largest region,
185  // and return the resulting size in pixel units
186  // This does not handle interior rings
187  double GetPolygonAreaInPixelsUnits(DataNodeType* polygonDataNode, ImageType* image);
188 
189  long int m_MaxTrainingSize; // number of training samples (-1 = no limit)
190  long int m_MaxValidationSize; // number of validation samples (-1 = no limit)
191  double m_ValidationTrainingProportion; // proportion of training vs validation
192  // (0.0 = all training, 1.0 = all validation)
193 
194  bool m_BoundByMin; // Bound the number of samples by the class having the fewer
195  bool m_PolygonEdgeInclusion; // if true take into consideration pixel which are on polygon edge
196  // useful, when dealing with small polygon area (1 or two pixels)
197  // false by default
198  unsigned short m_NumberOfClasses;
199  std::string m_ClassKey;
201 
202  std::map<ClassLabelType, double> m_ClassesSize;
203  std::map<ClassLabelType, double> m_ClassesProbTraining;
204  std::map<ClassLabelType, double> m_ClassesProbValidation;
205 
206  std::map<ClassLabelType, int> m_ClassesSamplesNumberTraining; // Just a counter
207  std::map<ClassLabelType, int> m_ClassesSamplesNumberValidation; // Just a counter
208 
209  typedef itk::Statistics::MersenneTwisterRandomVariateGenerator RandomGeneratorType;
210  RandomGeneratorType::Pointer m_RandomGenerator;
211 };
212 } // end of namespace otb
213 
214 #ifndef OTB_MANUAL_INSTANTIATION
216 #endif
217 
218 #endif
otb::ListSampleGenerator::m_ValidationTrainingProportion
double m_ValidationTrainingProportion
Definition: otbListSampleGenerator.h:191
otb::ListSampleGenerator::m_ClassesSamplesNumberValidation
std::map< ClassLabelType, int > m_ClassesSamplesNumberValidation
Definition: otbListSampleGenerator.h:207
otb::ListSampleGenerator::m_ClassesProbValidation
std::map< ClassLabelType, double > m_ClassesProbValidation
Definition: otbListSampleGenerator.h:204
otb::ListSampleGenerator::ImageRegionType
ImageType::RegionType ImageRegionType
Definition: otbListSampleGenerator.h:72
otb::ListSampleGenerator::VectorDataPointerType
VectorDataType::Pointer VectorDataPointerType
Definition: otbListSampleGenerator.h:74
otb::ListSampleGenerator::ImageIndexType
ImageType::IndexType ImageIndexType
Definition: otbListSampleGenerator.h:71
otb::ListSampleGenerator::m_ClassesProbTraining
std::map< ClassLabelType, double > m_ClassesProbTraining
Definition: otbListSampleGenerator.h:203
otb::ListSampleGenerator::m_ClassesSize
std::map< ClassLabelType, double > m_ClassesSize
Definition: otbListSampleGenerator.h:202
otb::ListSampleGenerator::m_NumberOfClasses
unsigned short m_NumberOfClasses
Definition: otbListSampleGenerator.h:198
otb::ListSampleGenerator::m_BoundByMin
bool m_BoundByMin
Definition: otbListSampleGenerator.h:194
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::ListSampleGenerator::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbListSampleGenerator.h:61
otb::ListSampleGenerator::ClassLabelType
int ClassLabelType
Definition: otbListSampleGenerator.h:83
otb::ListSampleGenerator::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbListSampleGenerator.h:60
otb::ListSampleGenerator::m_ClassMinSize
double m_ClassMinSize
Definition: otbListSampleGenerator.h:200
otb::ListSampleGenerator::RandomGeneratorType
itk::Statistics::MersenneTwisterRandomVariateGenerator RandomGeneratorType
Definition: otbListSampleGenerator.h:209
otb::ListSampleGenerator::PolygonListPointerType
DataNodeType::PolygonListPointerType PolygonListPointerType
Definition: otbListSampleGenerator.h:179
otb::ListSampleGenerator::SampleNumberType
std::map< ClassLabelType, int > SampleNumberType
Definition: otbListSampleGenerator.h:121
otb::ListSampleGenerator::DataObjectPointerArraySizeType
itk::ProcessObject::DataObjectPointerArraySizeType DataObjectPointerArraySizeType
Definition: otbListSampleGenerator.h:75
otb::ListSampleGenerator::LabelType
itk::FixedArray< ClassLabelType, 1 > LabelType
Definition: otbListSampleGenerator.h:84
otb::ListSampleGenerator::Self
ListSampleGenerator Self
Definition: otbListSampleGenerator.h:58
otb::ListSampleGenerator::GetClassesSamplesNumberValidation
SampleNumberType GetClassesSamplesNumberValidation(void) const
Definition: otbListSampleGenerator.h:129
otb::ListSampleGenerator::ListSampleType
itk::Statistics::ListSample< SampleType > ListSampleType
Definition: otbListSampleGenerator.h:79
otbListSampleGenerator.hxx
otb::ListSampleGenerator::DataObjectPointer
itk::DataObject::Pointer DataObjectPointer
Definition: otbListSampleGenerator.h:102
otb::ListSampleGenerator::ListSamplePointerType
ListSampleType::Pointer ListSamplePointerType
Definition: otbListSampleGenerator.h:80
otb::ListSampleGenerator::PolygonListType
DataNodeType::PolygonListType PolygonListType
Definition: otbListSampleGenerator.h:178
otb::ListSampleGenerator::m_ClassesSamplesNumberTraining
std::map< ClassLabelType, int > m_ClassesSamplesNumberTraining
Definition: otbListSampleGenerator.h:206
otb::ListSampleGenerator::Superclass
itk::ProcessObject Superclass
Definition: otbListSampleGenerator.h:59
otb::ListSampleGenerator::m_PolygonEdgeInclusion
bool m_PolygonEdgeInclusion
Definition: otbListSampleGenerator.h:195
otb::ListSampleGenerator::SampleType
ImageType::PixelType SampleType
Definition: otbListSampleGenerator.h:78
otb::ListSampleGenerator::m_RandomGenerator
RandomGeneratorType::Pointer m_RandomGenerator
Definition: otbListSampleGenerator.h:210
otb::ListSampleGenerator::PolygonType
DataNodeType::PolygonType PolygonType
Definition: otbListSampleGenerator.h:176
otb::ListSampleGenerator::GetClassesSamplesNumberTraining
SampleNumberType GetClassesSamplesNumberTraining(void) const
Definition: otbListSampleGenerator.h:124
otb::ListSampleGenerator::VectorDataType
TVectorData VectorDataType
Definition: otbListSampleGenerator.h:73
otb::ListSampleGenerator::GetClassesSize
std::map< ClassLabelType, double > GetClassesSize() const
Definition: otbListSampleGenerator.h:151
otb::ListSampleGenerator::TreeIteratorType
itk::PreOrderTreeIterator< typename VectorDataType::DataTreeType > TreeIteratorType
Definition: otbListSampleGenerator.h:180
otb::ListSampleGenerator::m_ClassKey
std::string m_ClassKey
Definition: otbListSampleGenerator.h:199
otb::ListSampleGenerator::~ListSampleGenerator
~ListSampleGenerator() override
Definition: otbListSampleGenerator.h:158
otb::ListSampleGenerator::ImagePointerType
ImageType::Pointer ImagePointerType
Definition: otbListSampleGenerator.h:70
otb::ListSampleGenerator::m_MaxTrainingSize
long int m_MaxTrainingSize
Definition: otbListSampleGenerator.h:189
otb::ListSampleGenerator::PolygonPointerType
DataNodeType::PolygonPointerType PolygonPointerType
Definition: otbListSampleGenerator.h:177
otb::ListSampleGenerator::m_MaxValidationSize
long int m_MaxValidationSize
Definition: otbListSampleGenerator.h:190
otb::ListSampleGenerator::ListLabelPointerType
ListLabelType::Pointer ListLabelPointerType
Definition: otbListSampleGenerator.h:86
otb::ListSampleGenerator
Produces a ListSample from a VectorImage and a VectorData.
Definition: otbListSampleGenerator.h:54
otb::ListSampleGenerator::ListLabelType
itk::Statistics::ListSample< LabelType > ListLabelType
Definition: otbListSampleGenerator.h:85
otb::ListSampleGenerator::DataNodeType
VectorDataType::DataNodeType DataNodeType
Definition: otbListSampleGenerator.h:175
otb::ListSampleGenerator::ImageType
TImage ImageType
Definition: otbListSampleGenerator.h:67