Orfeo Toolbox  4.0
otbListSampleToBalancedListSampleFilter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #ifndef __otbListSampleToBalancedListSampleFilter_txx
19 #define __otbListSampleToBalancedListSampleFilter_txx
20 
22 #include "itkProgressReporter.h"
23 #include "itkHistogram.h"
24 #include "itkNumericTraits.h"
25 
26 namespace otb {
27 namespace Statistics {
28 
29 // constructor
30 template < class TInputSampleList, class TLabelSampleList, class TOutputSampleList >
33 {
34  this->SetNumberOfRequiredInputs(2);
35  this->SetNumberOfRequiredOutputs(2);
36 
37  // Create the second output
38  //this->itk::ProcessObject::SetNthOutput(0, this->MakeOutput(0).GetPointer());
39  this->itk::ProcessObject::SetNthOutput(1, this->MakeOutput(1).GetPointer());
40 
41  m_AddGaussianNoiseFilter = GaussianAdditiveNoiseType::New();
42  m_BalancingFactor = 5;
43 }
44 
45 template < class TInputSampleList, class TLabelSampleList, class TOutputSampleList >
47 ::DataObjectPointer
49 ::MakeOutput(unsigned int idx)
50 {
51  DataObjectPointer output;
52  switch (idx)
53  {
54  case 0:
55  Superclass::MakeOutput(0);
56  break;
57  case 1:
58  {
59  output = static_cast<itk::DataObject*>(LabelSampleListType::New().GetPointer());
60  break;
61  }
62  default:
63  output = static_cast<itk::DataObject*>(InputSampleListType::New().GetPointer());
64  break;
65  }
66  return output;
67 }
68 
69 // Method to set the SampleList as DataObject
70 template < class TInputSampleList, class TLabelSampleList, class TOutputSampleList >
71 void
74 {
75  // Process object is not const-correct so the const_cast is required here
77  const_cast< LabelSampleListType* >( labelPtr ) );
78 }
79 
80 // Method to get the SampleList as DataObject
81 template < class TInputSampleList, class TLabelSampleList, class TOutputSampleList >
83 ::LabelSampleListType *
86 {
87  if (this->GetNumberOfInputs() < 2)
88  {
89  return 0;
90  }
91 
92  return static_cast<const LabelSampleListType* >
93  (this->itk::ProcessObject::GetInput(1) );
94 }
95 
96 // Get the output label SampleList as DataObject
97 template < class TInputSampleList, class TLabelSampleList, class TOutputSampleList >
99 ::LabelSampleListType *
102 {
103  return dynamic_cast<LabelSampleListType*>(this->itk::ProcessObject::GetOutput(1));
104 }
105 
106 
107 // Get the max sample number having the same label
108 template < class TInputSampleList, class TLabelSampleList, class TOutputSampleList >
109 void
112 {
113  // Iterate on the labelSampleList to get the min and max label
115 
116  // Number of bins to add to the histogram
117  typename LabelSampleListType::ConstPointer labelPtr = this->GetInputLabel();
118  typename LabelSampleListType::ConstIterator labelIt = labelPtr->Begin();
119 
120  while(labelIt != labelPtr->End())
121  {
122  // Get the current label sample
123  LabelMeasurementVectorType currentInputMeasurement = labelIt.GetMeasurementVector();
124 
125  if (currentInputMeasurement[0] > maxLabel)
126  maxLabel = currentInputMeasurement[0];
127 
128  ++labelIt;
129  }
130 
131  // Prepare histogram with dimension 1 : default template parameters
132  typedef typename itk::Statistics::Histogram<unsigned int> HistogramType;
133  typename HistogramType::Pointer histogram = HistogramType::New();
134  typename HistogramType::SizeType size(1);
135  size.Fill(maxLabel +1);
136  histogram->SetMeasurementVectorSize(1); // we need only one dimension
137  histogram->Initialize(size);
138 
139  labelIt = labelPtr->Begin();
140  while (labelIt != labelPtr->End())
141  {
142  // Get the current label sample
143  LabelMeasurementVectorType currentInputMeasurement = labelIt.GetMeasurementVector();
144  histogram->IncreaseFrequency(currentInputMeasurement[0], 1);
145  ++labelIt;
146  }
147 
148  // Iterate through the histogram to get the maximum
149  unsigned int maxvalue = 0;
150  HistogramType::Iterator iter = histogram->Begin();
151 
152  while ( iter != histogram->End() )
153  {
154  if( static_cast<unsigned int>(iter.GetFrequency()) > maxvalue )
155  maxvalue = static_cast<unsigned int>(iter.GetFrequency());
156  ++iter;
157  }
158 
159  // Number of sample per label to reach in order to have a balanced
160  // ListSample
161  unsigned int balancedFrequency = m_BalancingFactor * maxvalue;
162 
163  // Guess how much noised samples must be added per sample to get
164  // a balanced ListSample : Computed using the
165  // - Frequency of each label (stored in the histogram)
166  // - The value maxvalue by m_BalancingFactor
167  // The std::vector below stores the multiplicative factor
168  iter = histogram->Begin();
169  while ( iter != histogram->End() )
170  {
171  if(iter.GetFrequency() - 1e-10 < 0.)
172  m_MultiplicativeCoefficient.push_back(0);
173  else
174  {
175  unsigned int coeff = static_cast<unsigned int>(balancedFrequency/iter.GetFrequency());
176  m_MultiplicativeCoefficient.push_back(coeff);
177  }
178 
179  ++iter;
180  }
181 }
182 
183 template < class TInputSampleList, class TLabelSampleList, class TOutputSampleList >
184 void
187 {
188  // Get the how much each sample must be expanded
189  this->ComputeMaxSampleFrequency();
190 
191  // Retrieve input and output pointers
192  InputSampleListConstPointer inputSampleListPtr = this->GetInput();
193  LabelSampleListConstPointer labelSampleListPtr = this->GetInputLabel();
194  OutputSampleListPointer outputSampleListPtr = this->GetOutput();
195  LabelSampleListPointer outputLabel = this->GetOutputLabel();
196 
197  // Clear any previous output
198  outputSampleListPtr->Clear();
199 
200  // Set the measurement vectors size
201  outputSampleListPtr->SetMeasurementVectorSize(inputSampleListPtr->GetMeasurementVectorSize());
202  outputLabel->SetMeasurementVectorSize(labelSampleListPtr->GetMeasurementVectorSize());
203 
204  typename InputSampleListType::ConstIterator inputIt = inputSampleListPtr->Begin();
205  typename LabelSampleListType::ConstIterator labelIt = labelSampleListPtr->Begin();
206 
207  // Set-up progress reporting
208  itk::ProgressReporter progress(this, 0, inputSampleListPtr->Size());
209 
210  // Noising filter
211  GaussianAdditiveNoisePointerType noisingFilter = GaussianAdditiveNoiseType::New();
212 
213  // Iterate on the InputSampleList
214  while(inputIt != inputSampleListPtr->End() && labelIt != labelSampleListPtr->End())
215  {
216  // Retrieve current input sample
217  InputMeasurementVectorType currentInputMeasurement = inputIt.GetMeasurementVector();
218  // Retrieve the current label
219  LabelMeasurementVectorType currentLabelMeasurement = labelIt.GetMeasurementVector();
220 
221  // Build a temporary ListSample wiht the current
222  // measurement vector to generate noised versions of this
223  // measurement vector
224  InputSampleListPointer tempListSample = InputSampleListType::New();
225  tempListSample->SetMeasurementVectorSize(inputSampleListPtr->GetMeasurementVectorSize());
226  tempListSample->PushBack(currentInputMeasurement);
227 
228  // Get how many times we have to noise this sample
229  unsigned int iterations = m_MultiplicativeCoefficient[currentLabelMeasurement[0]];
230 
231  // Noising filter
232  //GaussianAdditiveNoisePointerType noisingFilter = GaussianAdditiveNoiseType::New();
233  noisingFilter->SetInput(tempListSample);
234  noisingFilter->SetNumberOfIteration(iterations);
235  noisingFilter->Update();
236 
237  // Build current output sample
238  OutputMeasurementVectorType currentOutputMeasurement;
239  currentOutputMeasurement.SetSize(currentInputMeasurement.GetSize());
240 
241  // Cast the current sample in outputSampleValue
242  for(unsigned int idx = 0; idx < inputSampleListPtr->GetMeasurementVectorSize(); ++idx)
243  currentOutputMeasurement[idx] = static_cast<OutputValueType>(currentInputMeasurement[idx]);
244 
245  // Add the current input casted sample to the output SampleList
246  outputSampleListPtr->PushBack(currentOutputMeasurement);
247 
248  // Add the currentsample list label
249  outputLabel->PushBack(currentLabelMeasurement);
250 
251  // Add the noised versions of the current sample to OutputSampleList
252  typename OutputSampleListType::ConstIterator tempIt = noisingFilter->GetOutput()->Begin();
253 
254  while(tempIt != noisingFilter->GetOutput()->End())
255  {
256  // Get the noised sample of the current measurement vector
257  OutputMeasurementVectorType currentTempMeasurement = tempIt.GetMeasurementVector();
258  // Add to output SampleList
259  outputSampleListPtr->PushBack(currentTempMeasurement);
260 
261  // Add a label in the output ListSample
262  outputLabel->PushBack(currentLabelMeasurement);
263 
264  ++tempIt;
265  }
266 
267  // Update progress
268  progress.CompletedPixel();
269 
270  ++inputIt;
271  ++ labelIt;
272  }
273 }
274 
275 template < class TInputSampleList, class TLabelSampleList, class TOutputSampleList >
276 void
278 ::PrintSelf(std::ostream& os, itk::Indent indent) const
279 {
280  // Call superclass implementation
281  Superclass::PrintSelf(os, indent);
282 }
283 
284 } // End namespace Statistics
285 } // End namespace otb
286 
287 #endif

Generated at Sat Mar 8 2014 16:05:57 for Orfeo Toolbox with doxygen 1.8.3.1