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