OTB  10.0.0
Orfeo Toolbox
otbSampleAugmentationFilter.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 #ifndef otbSampleAugmentationFilter_h
22 #define otbSampleAugmentationFilter_h
23 
24 #include "itkProcessObject.h"
26 #include "otbSampleAugmentation.h"
27 #include "OTBSamplingExport.h"
28 #include <string>
29 
30 namespace otb
31 {
32 
33 
47 class OTBSampling_EXPORT SampleAugmentationFilter : public itk::ProcessObject
48 {
49 public:
52  typedef itk::ProcessObject Superclass;
53  typedef itk::SmartPointer<Self> Pointer;
54  typedef itk::SmartPointer<const Self> ConstPointer;
55 
57  itkNewMacro(Self);
58 
60  itkTypeMacro(SampleAugmentationFilter, ProcessObject);
61 
65 
66  typedef itk::ProcessObject::DataObjectPointerArraySizeType DataObjectPointerArraySizeType;
67 
70 
71  enum class Strategy
72  {
73  Replicate,
74  Jitter,
75  Smote
76  };
77 
79  using Superclass::SetInput;
80  virtual void SetInput(const OGRDataSourceType* ds);
81  const OGRDataSourceType* GetInput(unsigned int idx);
83 
84  virtual void SetOutputSamples(ogr::DataSource* data);
85 
89  itkSetMacro(ClassFieldName, std::string);
90 
94  itkGetMacro(ClassFieldName, std::string);
95 
96 
97  itkSetMacro(Layer, size_t);
98  itkGetMacro(Layer, size_t);
99  itkSetMacro(Label, int);
100  itkGetMacro(Label, int);
102  {
103  m_Strategy = s;
104  }
106  {
107  return m_Strategy;
108  }
109  itkSetMacro(NumberOfSamples, int);
110  itkGetMacro(NumberOfSamples, int);
111  void SetExcludedFields(const std::vector<std::string>& ef)
112  {
113  m_ExcludedFields = ef;
114  }
115  std::vector<std::string> GetExcludedFields() const
116  {
117  return m_ExcludedFields;
118  }
119  itkSetMacro(StdFactor, double);
120  itkGetMacro(StdFactor, double);
121  itkSetMacro(SmoteNeighbors, size_t);
122  itkGetMacro(SmoteNeighbors, size_t);
123  itkSetMacro(Seed, int);
124  itkGetMacro(Seed, int);
129 
130 protected:
133  {
134  }
135 
137  void GenerateData() override;
138 
140  typedef itk::DataObject::Pointer DataObjectPointer;
141 
143  using Superclass::MakeOutput;
144 
145 
146  SampleVectorType ExtractSamples(const ogr::DataSource::Pointer vectors, size_t layerName, const std::string& classField, const int label,
147  const std::vector<std::string>& excludedFields = {});
148 
149  void SampleToOGRFeatures(const ogr::DataSource::Pointer& vectors, ogr::DataSource* output, const SampleVectorType& samples, const size_t layerName,
150  const std::string& classField, int label, const std::vector<std::string>& excludedFields = {});
151 
152  std::set<size_t> GetExcludedFieldsIds(const std::vector<std::string>& excludedFields, const ogr::Layer& inputLayer);
153  bool IsNumericField(const ogr::Feature& feature, const int idx);
154 
155  ogr::Feature SelectTemplateFeature(const ogr::Layer& inputLayer, const std::string& classField, int label);
156 
157 private:
158  SampleAugmentationFilter(const Self&) = delete;
159  void operator=(const Self&) = delete;
160 
161  std::string m_ClassFieldName;
162  size_t m_Layer;
163  int m_Label;
164  std::vector<std::string> m_ExcludedFields;
167  double m_StdFactor;
169  int m_Seed;
170 };
171 
172 
173 } // end namespace otb
174 
175 #endif
Filter to generate synthetic samples from existing ones.
itk::ProcessObject::DataObjectPointerArraySizeType DataObjectPointerArraySizeType
OGRDataSourceType::Pointer OGRDataSourcePointerType
SampleAugmentationFilter(const Self &)=delete
void SetExcludedFields(const std::vector< std::string > &ef)
bool IsNumericField(const ogr::Feature &feature, const int idx)
virtual void SetOutputSamples(ogr::DataSource *data)
void operator=(const Self &)=delete
const OGRDataSourceType * GetInput(unsigned int idx)
ogr::Feature SelectTemplateFeature(const ogr::Layer &inputLayer, const std::string &classField, int label)
DataObjectPointer MakeOutput(DataObjectPointerArraySizeType idx) override
SampleVectorType ExtractSamples(const ogr::DataSource::Pointer vectors, vcl_size_t layerName, const std::string &classField, const int label, const std::vector< std::string > &excludedFields={})
std::set< vcl_size_t > GetExcludedFieldsIds(const std::vector< std::string > &excludedFields, const ogr::Layer &inputLayer)
virtual void SetInput(const OGRDataSourceType *ds)
itk::SmartPointer< const Self > ConstPointer
std::vector< std::string > m_ExcludedFields
sampleAugmentation::SampleVectorType SampleVectorType
std::vector< std::string > GetExcludedFields() const
const OGRDataSourceType * GetOutput()
itk::DataObject::Pointer DataObjectPointer
sampleAugmentation::SampleType SampleType
void SampleToOGRFeatures(const ogr::DataSource::Pointer &vectors, ogr::DataSource *output, const SampleVectorType &samples, const vcl_size_t layerName, const std::string &classField, int label, const std::vector< std::string > &excludedFields={})
Collection of geometric objects.
itk::SmartPointer< Self > Pointer
Geometric object with descriptive fields.
Layer of geometric objects.
std::vector< double > SampleType
std::vector< SampleType > SampleVectorType
void Smote(const SampleVectorType &inSamples, const vcl_size_t nbSamples, SampleVectorType &newSamples, const int nbNeighbors, const int seed=std::time(nullptr))
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.