OTB  9.0.0
Orfeo Toolbox
otbListSampleToBalancedListSampleFilter.hxx
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 otbListSampleToBalancedListSampleFilter_hxx
22 #define otbListSampleToBalancedListSampleFilter_hxx
23 
25 #include "itkProgressReporter.h"
26 #include "itkHistogram.h"
27 #include "itkNumericTraits.h"
28 
29 namespace otb
30 {
31 namespace Statistics
32 {
33 
34 // constructor
35 template <class TInputSampleList, class TLabelSampleList, class TOutputSampleList>
37 {
38  this->SetNumberOfRequiredInputs(2);
39  this->SetNumberOfRequiredOutputs(2);
40 
41  // Create the second output
42  // this->itk::ProcessObject::SetNthOutput(0, this->MakeOutput(0).GetPointer());
43  this->itk::ProcessObject::SetNthOutput(1, this->MakeOutput(1).GetPointer());
44 
45  m_AddGaussianNoiseFilter = GaussianAdditiveNoiseType::New();
46  m_BalancingFactor = 5;
47 }
48 
49 template <class TInputSampleList, class TLabelSampleList, class TOutputSampleList>
52 {
53  DataObjectPointer output;
54  switch (idx)
55  {
56  case 0:
57  Superclass::MakeOutput(0);
58  break;
59  case 1:
60  {
61  output = static_cast<itk::DataObject*>(LabelSampleListType::New().GetPointer());
62  break;
63  }
64  default:
65  output = static_cast<itk::DataObject*>(InputSampleListType::New().GetPointer());
66  break;
67  }
68  return output;
69 }
70 
71 // Method to set the SampleList as DataObject
72 template <class TInputSampleList, class TLabelSampleList, class TOutputSampleList>
74 {
75  // Process object is not const-correct so the const_cast is required here
76  this->itk::ProcessObject::SetNthInput(1, const_cast<LabelSampleListType*>(labelPtr));
77 }
78 
79 // Method to get the SampleList as DataObject
80 template <class TInputSampleList, class TLabelSampleList, class TOutputSampleList>
83 {
84  if (this->GetNumberOfInputs() < 2)
85  {
86  return nullptr;
87  }
88 
89  return static_cast<const LabelSampleListType*>(this->itk::ProcessObject::GetInput(1));
90 }
91 
92 // Get the output label SampleList as DataObject
93 template <class TInputSampleList, class TLabelSampleList, class TOutputSampleList>
96 {
97  return dynamic_cast<LabelSampleListType*>(this->itk::ProcessObject::GetOutput(1));
98 }
99 
100 
101 // Get the max sample number having the same label
102 template <class TInputSampleList, class TLabelSampleList, class TOutputSampleList>
104 {
105  // Iterate on the labelSampleList to get the min and max label
106  LabelValueType maxLabel = itk::NumericTraits<LabelValueType>::min();
107 
108  // Number of bins to add to the histogram
109  typename LabelSampleListType::ConstPointer labelPtr = this->GetInputLabel();
110  typename LabelSampleListType::ConstIterator labelIt = labelPtr->Begin();
111 
112  while (labelIt != labelPtr->End())
113  {
114  // Get the current label sample
115  LabelMeasurementVectorType currentInputMeasurement = labelIt.GetMeasurementVector();
116 
117  if (currentInputMeasurement[0] > maxLabel)
118  maxLabel = currentInputMeasurement[0];
119 
120  ++labelIt;
121  }
122 
123  // Prepare histogram with dimension 1 : default template parameters
124  typedef typename itk::Statistics::Histogram<unsigned int> HistogramType;
125  typename HistogramType::Pointer histogram = HistogramType::New();
126  typename HistogramType::SizeType size(1);
127  size.Fill(maxLabel + 1);
128  histogram->SetMeasurementVectorSize(1); // we need only one dimension
129  histogram->Initialize(size);
130 
131  labelIt = labelPtr->Begin();
132  while (labelIt != labelPtr->End())
133  {
134  // Get the current label sample
135  LabelMeasurementVectorType currentInputMeasurement = labelIt.GetMeasurementVector();
136  histogram->IncreaseFrequency(currentInputMeasurement[0], 1);
137  ++labelIt;
138  }
139 
140  // Iterate through the histogram to get the maximum
141  unsigned int maxvalue = 0;
142  HistogramType::Iterator iter = histogram->Begin();
143 
144  while (iter != histogram->End())
145  {
146  if (static_cast<unsigned int>(iter.GetFrequency()) > maxvalue)
147  maxvalue = static_cast<unsigned int>(iter.GetFrequency());
148  ++iter;
149  }
150 
151  // Number of sample per label to reach in order to have a balanced
152  // ListSample
153  unsigned int balancedFrequency = m_BalancingFactor * maxvalue;
154 
155  // Guess how much noised samples must be added per sample to get
156  // a balanced ListSample : Computed using the
157  // - Frequency of each label (stored in the histogram)
158  // - The value maxvalue by m_BalancingFactor
159  // The std::vector below stores the multiplicative factor
160  iter = histogram->Begin();
161  while (iter != histogram->End())
162  {
163  if (iter.GetFrequency() - 1e-10 < 0.)
164  m_MultiplicativeCoefficient.push_back(0);
165  else
166  {
167  unsigned int coeff = static_cast<unsigned int>(balancedFrequency / iter.GetFrequency());
168  m_MultiplicativeCoefficient.push_back(coeff);
169  }
170 
171  ++iter;
172  }
173 }
174 
175 template <class TInputSampleList, class TLabelSampleList, class TOutputSampleList>
177 {
178  // Get the how much each sample must be expanded
179  this->ComputeMaxSampleFrequency();
180 
181  // Retrieve input and output pointers
182  InputSampleListConstPointer inputSampleListPtr = this->GetInput();
183  LabelSampleListConstPointer labelSampleListPtr = this->GetInputLabel();
184  OutputSampleListPointer outputSampleListPtr = this->GetOutput();
185  LabelSampleListPointer outputLabel = this->GetOutputLabel();
186 
187  // Clear any previous output
188  outputSampleListPtr->Clear();
189 
190  // Set the measurement vectors size
191  outputSampleListPtr->SetMeasurementVectorSize(inputSampleListPtr->GetMeasurementVectorSize());
192  outputLabel->SetMeasurementVectorSize(labelSampleListPtr->GetMeasurementVectorSize());
193 
194  typename InputSampleListType::ConstIterator inputIt = inputSampleListPtr->Begin();
195  typename LabelSampleListType::ConstIterator labelIt = labelSampleListPtr->Begin();
196 
197  // Set-up progress reporting
198  itk::ProgressReporter progress(this, 0, inputSampleListPtr->Size());
199 
200  // Noising filter
201  GaussianAdditiveNoisePointerType noisingFilter = GaussianAdditiveNoiseType::New();
202 
203  // Iterate on the InputSampleList
204  while (inputIt != inputSampleListPtr->End() && labelIt != labelSampleListPtr->End())
205  {
206  // Retrieve current input sample
207  InputMeasurementVectorType currentInputMeasurement = inputIt.GetMeasurementVector();
208  // Retrieve the current label
209  LabelMeasurementVectorType currentLabelMeasurement = labelIt.GetMeasurementVector();
210 
211  // Build a temporary ListSample with the current
212  // measurement vector to generate noised versions of this
213  // measurement vector
214  InputSampleListPointer tempListSample = InputSampleListType::New();
215  tempListSample->SetMeasurementVectorSize(inputSampleListPtr->GetMeasurementVectorSize());
216  tempListSample->PushBack(currentInputMeasurement);
217 
218  // Get how many times we have to noise this sample
219  unsigned int iterations = m_MultiplicativeCoefficient[currentLabelMeasurement[0]];
220 
221  // Noising filter
222  // GaussianAdditiveNoisePointerType noisingFilter = GaussianAdditiveNoiseType::New();
223  noisingFilter->SetInput(tempListSample);
224  noisingFilter->SetNumberOfIteration(iterations);
225  noisingFilter->Update();
226 
227  // Build current output sample
228  OutputMeasurementVectorType currentOutputMeasurement;
229  currentOutputMeasurement.SetSize(currentInputMeasurement.GetSize());
230 
231  // Cast the current sample in outputSampleValue
232  for (unsigned int idx = 0; idx < inputSampleListPtr->GetMeasurementVectorSize(); ++idx)
233  currentOutputMeasurement[idx] = static_cast<OutputValueType>(currentInputMeasurement[idx]);
234 
235  // Add the current input casted sample to the output SampleList
236  outputSampleListPtr->PushBack(currentOutputMeasurement);
237 
238  // Add the currentsample list label
239  outputLabel->PushBack(currentLabelMeasurement);
240 
241  // Add the noised versions of the current sample to OutputSampleList
242  typename OutputSampleListType::ConstIterator tempIt = noisingFilter->GetOutput()->Begin();
243 
244  while (tempIt != noisingFilter->GetOutput()->End())
245  {
246  // Get the noised sample of the current measurement vector
247  OutputMeasurementVectorType currentTempMeasurement = tempIt.GetMeasurementVector();
248  // Add to output SampleList
249  outputSampleListPtr->PushBack(currentTempMeasurement);
250 
251  // Add a label in the output ListSample
252  outputLabel->PushBack(currentLabelMeasurement);
253 
254  ++tempIt;
255  }
256 
257  // Update progress
258  progress.CompletedPixel();
259 
260  ++inputIt;
261  ++labelIt;
262  }
263 }
264 
265 template <class TInputSampleList, class TLabelSampleList, class TOutputSampleList>
267 {
268  // Call superclass implementation
269  Superclass::PrintSelf(os, indent);
270 }
271 
272 } // End namespace Statistics
273 } // End namespace otb
274 
275 #endif
otb::Statistics::ListSampleToBalancedListSampleFilter::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbListSampleToBalancedListSampleFilter.hxx:266
otb::Statistics::ListSampleToBalancedListSampleFilter::SetInputLabel
void SetInputLabel(const LabelSampleListType *label)
Definition: otbListSampleToBalancedListSampleFilter.hxx:73
otb::Statistics::ListSampleToBalancedListSampleFilter::GetInputLabel
const LabelSampleListType * GetInputLabel() const
Definition: otbListSampleToBalancedListSampleFilter.hxx:82
otb::Statistics::ListSampleToBalancedListSampleFilter::InputMeasurementVectorType
InputSampleListType::MeasurementVectorType InputMeasurementVectorType
Definition: otbListSampleToBalancedListSampleFilter.h:67
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::Statistics::ListSampleToBalancedListSampleFilter::ComputeMaxSampleFrequency
void ComputeMaxSampleFrequency()
Definition: otbListSampleToBalancedListSampleFilter.hxx:103
otb::Statistics::ListSampleToBalancedListSampleFilter::LabelSampleListPointer
LabelSampleListType::Pointer LabelSampleListPointer
Definition: otbListSampleToBalancedListSampleFilter.h:72
otb::Statistics::ListSampleToBalancedListSampleFilter::LabelSampleListType
TLabelSampleList LabelSampleListType
Definition: otbListSampleToBalancedListSampleFilter.h:71
otb::Statistics::ListSampleToBalancedListSampleFilter::GaussianAdditiveNoisePointerType
GaussianAdditiveNoiseType::Pointer GaussianAdditiveNoisePointerType
Definition: otbListSampleToBalancedListSampleFilter.h:91
otb::Statistics::ListSampleToBalancedListSampleFilter::InputSampleListConstPointer
InputSampleListType::ConstPointer InputSampleListConstPointer
Definition: otbListSampleToBalancedListSampleFilter.h:66
otb::Statistics::ListSampleToBalancedListSampleFilter::OutputSampleListPointer
OutputSampleListType::Pointer OutputSampleListPointer
Definition: otbListSampleToBalancedListSampleFilter.h:80
otb::Statistics::ListSampleToBalancedListSampleFilter::LabelValueType
LabelMeasurementVectorType::ValueType LabelValueType
Definition: otbListSampleToBalancedListSampleFilter.h:75
otb::Statistics::ListSampleToBalancedListSampleFilter::LabelSampleListConstPointer
LabelSampleListType::ConstPointer LabelSampleListConstPointer
Definition: otbListSampleToBalancedListSampleFilter.h:73
otb::Statistics::ListSampleToBalancedListSampleFilter::OutputValueType
OutputMeasurementVectorType::ValueType OutputValueType
Definition: otbListSampleToBalancedListSampleFilter.h:83
otb::Statistics::ListSampleToBalancedListSampleFilter::MakeOutput
DataObjectPointer MakeOutput(DataObjectPointerArraySizeType idx) override
Definition: otbListSampleToBalancedListSampleFilter.hxx:51
otb::Statistics::ListSampleToBalancedListSampleFilter::DataObjectPointerArraySizeType
itk::ProcessObject::DataObjectPointerArraySizeType DataObjectPointerArraySizeType
Definition: otbListSampleToBalancedListSampleFilter.h:87
otb::Statistics::ListSampleToBalancedListSampleFilter::OutputMeasurementVectorType
OutputSampleListType::MeasurementVectorType OutputMeasurementVectorType
Definition: otbListSampleToBalancedListSampleFilter.h:82
otb::Statistics::ListSampleToBalancedListSampleFilter::LabelMeasurementVectorType
LabelSampleListType::MeasurementVectorType LabelMeasurementVectorType
Definition: otbListSampleToBalancedListSampleFilter.h:74
otbListSampleToBalancedListSampleFilter.h
otb::Statistics::ListSampleToBalancedListSampleFilter::InputSampleListPointer
InputSampleListType::Pointer InputSampleListPointer
Definition: otbListSampleToBalancedListSampleFilter.h:65
otb::Statistics::ListSampleToBalancedListSampleFilter::ListSampleToBalancedListSampleFilter
ListSampleToBalancedListSampleFilter()
Definition: otbListSampleToBalancedListSampleFilter.hxx:36
otb::Statistics::ListSampleToBalancedListSampleFilter::GenerateData
void GenerateData() override
Definition: otbListSampleToBalancedListSampleFilter.hxx:176
otb::Statistics::ListSampleToBalancedListSampleFilter::DataObjectPointer
Superclass::DataObjectPointer DataObjectPointer
Definition: otbListSampleToBalancedListSampleFilter.h:86
otb::Statistics::ListSampleToBalancedListSampleFilter::GetOutputLabel
LabelSampleListType * GetOutputLabel()
Definition: otbListSampleToBalancedListSampleFilter.hxx:95