OTB  6.7.0
Orfeo Toolbox
otbListSampleGenerator.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2019 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"
29 #include <string>
30 
31 namespace otb
32 {
53 template <class TImage, class TVectorData>
54 class ITK_EXPORT ListSampleGenerator :
55  public itk::ProcessObject
56 {
57 public:
63 
66 
68  itkNewMacro(Self);
69 
70  typedef TImage ImageType;
71  typedef typename ImageType::Pointer ImagePointerType;
73  typedef typename ImageType::RegionType ImageRegionType;
74  typedef TVectorData VectorDataType;
75  typedef typename VectorDataType::Pointer VectorDataPointerType;
77 
79  typedef typename ImageType::PixelType SampleType;
82 
84  typedef int ClassLabelType;
85  typedef itk::FixedArray<ClassLabelType, 1> LabelType; //note could be templated by an std:::string
88 
90  using Superclass::SetInput;
91  void SetInput(const ImageType *);
92  const ImageType * GetInput() const;
94 
98  void SetInputVectorData(const VectorDataType *);
99  const VectorDataType * GetInputVectorData() const;
101 
102  // Build the outputs
104  DataObjectPointer MakeOutput(DataObjectPointerArraySizeType idx) override;
105  using Superclass::MakeOutput;
106 
107  //virtual void Update();
108 
110  itkGetConstMacro(MaxTrainingSize, long int);
111  itkSetMacro(MaxTrainingSize, long int);
112  itkGetConstMacro(PolygonEdgeInclusion, bool);
113  itkSetMacro(PolygonEdgeInclusion, bool);
114  itkGetConstMacro(MaxValidationSize, long int);
115  itkSetMacro(MaxValidationSize, long int);
116  itkGetConstMacro(ValidationTrainingProportion, double);
117  itkSetClampMacro(ValidationTrainingProportion, double, 0.0, 1.0);
118  itkGetConstMacro(BoundByMin, bool);
119  itkSetMacro(BoundByMin, bool);
121 
122  itkGetConstMacro(NumberOfClasses, unsigned short);
123  typedef std::map<ClassLabelType, int> SampleNumberType;
124 
126  {
127  return m_ClassesSamplesNumberTraining;
128  }
129 
131  {
132  return m_ClassesSamplesNumberValidation;
133  }
134 
135  itkGetStringMacro(ClassKey);
136  itkSetStringMacro(ClassKey);
137  itkGetConstMacro(ClassMinSize, double);
138 
140  ListSampleType * GetTrainingListSample();
141 
143  ListLabelType * GetTrainingListLabel();
144 
146  ListSampleType * GetValidationListSample();
147 
149  ListLabelType * GetValidationListLabel();
150 
151  // Get the map size
152  std::map<ClassLabelType, double> GetClassesSize() const
153  {
154  return m_ClassesSize;
155  }
156 
157 protected:
159  ~ListSampleGenerator() override {}
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;
180 
181  void ComputeClassSelectionProbability();
182 
183  // Crop the polygon wrt the image largest region,
184  // and return the resulting size in pixel units
185  // This does not handle interior rings
186  double GetPolygonAreaInPixelsUnits(DataNodeType* polygonDataNode, ImageType* image);
187 
188  long int m_MaxTrainingSize; // number of training samples (-1 = no limit)
189  long int m_MaxValidationSize; // number of validation samples (-1 = no limit)
190  double m_ValidationTrainingProportion; // proportion of training vs validation
191  // (0.0 = all training, 1.0 = all validation)
192 
193  bool m_BoundByMin; //Bound the number of samples by the class having the fewer
194  bool m_PolygonEdgeInclusion; // if true take into consideration pixel which are on polygon edge
195  // useful, when dealing with small polygon area (1 or two pixels)
196  // false by default
197  unsigned short m_NumberOfClasses;
198  std::string m_ClassKey;
200 
201  std::map<ClassLabelType, double> m_ClassesSize;
202  std::map<ClassLabelType, double> m_ClassesProbTraining;
203  std::map<ClassLabelType, double> m_ClassesProbValidation;
204 
205  std::map<ClassLabelType, int> m_ClassesSamplesNumberTraining; //Just a counter
206  std::map<ClassLabelType, int> m_ClassesSamplesNumberValidation; //Just a counter
207 
210 };
211 } // end of namespace otb
212 
213 #ifndef OTB_MANUAL_INSTANTIATION
215 #endif
216 
217 #endif
std::map< ClassLabelType, double > m_ClassesProbTraining
std::map< ClassLabelType, int > SampleNumberType
std::map< ClassLabelType, int > m_ClassesSamplesNumberTraining
std::map< ClassLabelType, int > m_ClassesSamplesNumberValidation
itk::Statistics::ListSample< LabelType > ListLabelType
itk::PreOrderTreeIterator< typename VectorDataType::DataTreeType > TreeIteratorType
SampleNumberType GetClassesSamplesNumberTraining(void) const
ImageType::Pointer ImagePointerType
std::map< ClassLabelType, double > GetClassesSize() const
VectorDataType::Pointer VectorDataPointerType
ListSampleType::Pointer ListSamplePointerType
itk::SmartPointer< const Self > ConstPointer
RandomGeneratorType::Pointer m_RandomGenerator
Produces a ListSample from a VectorImage and a VectorData.
SampleNumberType GetClassesSamplesNumberValidation(void) const
DataNodeType::PolygonListPointerType PolygonListPointerType
VectorDataType::DataNodeType DataNodeType
ImageType::IndexType ImageIndexType
DataNodeType::PolygonPointerType PolygonPointerType
ListLabelType::Pointer ListLabelPointerType
itk::Index< Monteverdi_DIMENSION > IndexType
Definition: mvdTypes.h:133
std::map< ClassLabelType, double > m_ClassesSize
itk::Statistics::MersenneTwisterRandomVariateGenerator RandomGeneratorType
DataObjectPointerArray::size_type DataObjectPointerArraySizeType
ImageType::RegionType ImageRegionType
std::map< ClassLabelType, double > m_ClassesProbValidation
ImageType::PixelType SampleType
itk::ProcessObject::DataObjectPointerArraySizeType DataObjectPointerArraySizeType
DataNodeType::PolygonType PolygonType
itk::FixedArray< ClassLabelType, 1 > LabelType
itk::SmartPointer< Self > Pointer
DataNodeType::PolygonListType PolygonListType
itk::Statistics::ListSample< SampleType > ListSampleType
itk::DataObject::Pointer DataObjectPointer