Orfeo Toolbox  3.16
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  typename LabelSampleListObjectType::Pointer labelListSample = LabelSampleListObjectType::New();
60  labelListSample->Set(LabelSampleListType::New());
61  output = static_cast<itk::DataObject*>(labelListSample.GetPointer());
62  break;
63  }
64  default:
65  output = static_cast<itk::DataObject*>(InputSampleListObjectType::New().GetPointer());
66  break;
67  }
68  return output;
69 }
70 
71 // Method to set the SampleList
72 template < class TInputSampleList, class TLabelSampleList, class TOutputSampleList >
73 void
76 {
77  typename LabelSampleListObjectType::Pointer labelPtr = LabelSampleListObjectType::New();
78  labelPtr->Set(label);
79  this->SetInputLabel(labelPtr);
80 }
81 
82 // Method to set the SampleList as DataObject
83 template < class TInputSampleList, class TLabelSampleList, class TOutputSampleList >
84 void
87 {
88  // Process object is not const-correct so the const_cast is required here
90  const_cast< LabelSampleListObjectType* >( labelPtr ) );
91 }
92 
93 // Method to get the SampleList as DataObject
94 template < class TInputSampleList, class TLabelSampleList, class TOutputSampleList >
96 ::LabelSampleListObjectType *
99 {
100  if (this->GetNumberOfInputs() < 2)
101  {
102  return 0;
103  }
104 
105  return static_cast<const LabelSampleListObjectType* >
106  (this->itk::ProcessObject::GetInput(1) );
107 }
108 
109 // Method to get the SampleList
110 template < class TInputSampleList, class TLabelSampleList, class TOutputSampleList >
112 ::LabelSampleListType *
115 {
116  if (this->GetNumberOfInputs() < 2)
117  {
118  return 0;
119  }
120 
121  typename LabelSampleListObjectType::ConstPointer dataObjectPointer = static_cast<const LabelSampleListObjectType * >
122  (this->itk::ProcessObject::GetInput(1) );
123  return dataObjectPointer->Get();
124 }
125 
126 // Get the output label SampleList
127 template < class TInputSampleList, class TLabelSampleList, class TOutputSampleList >
129 ::LabelSampleListType *
132 {
133  return const_cast<LabelSampleListType*>(this->GetOutputLabel()->Get());
134 }
135 
136 // Get the output label SampleList as DataObject
137 template < class TInputSampleList, class TLabelSampleList, class TOutputSampleList >
139 ::LabelSampleListObjectType *
142 {
143  return dynamic_cast<LabelSampleListObjectType*>(this->itk::ProcessObject::GetOutput(1));
144 }
145 
146 
147 // Get the max sample number having the same label
148 template < class TInputSampleList, class TLabelSampleList, class TOutputSampleList >
149 void
152 {
153  // Iterate on the labelSampleList to get the min and max label
154  LabelValueType maxLabel = itk::NumericTraits<LabelValueType>::min();
155 
156  // Number of bins to add to the histogram
157  typename LabelSampleListType::ConstPointer labelPtr = this->GetLabelSampleList();
158  typename LabelSampleListType::ConstIterator labelIt = labelPtr->Begin();
159 
160  while(labelIt != labelPtr->End())
161  {
162  // Get the current label sample
163  LabelMeasurementVectorType currentInputMeasurement = labelIt.GetMeasurementVector();
164 
165  if (currentInputMeasurement[0] > maxLabel)
166  maxLabel = currentInputMeasurement[0];
167 
168  ++labelIt;
169  }
170 
171  // Prepare histogram with dimension 1 : default template parameters
172  typedef typename itk::Statistics::Histogram<unsigned int> HistogramType;
173  typename HistogramType::Pointer histogram = HistogramType::New();
174  typename HistogramType::SizeType size;
175  size.Fill(maxLabel +1);
176  histogram->Initialize(size);
177 
178  labelIt = labelPtr->Begin();
179  while (labelIt != labelPtr->End())
180  {
181  // Get the current label sample
182  LabelMeasurementVectorType currentInputMeasurement = labelIt.GetMeasurementVector();
183  histogram->IncreaseFrequency(currentInputMeasurement[0], 1);
184  ++labelIt;
185  }
186 
187  // Iterate through the histogram to get the maximum
188  unsigned int maxvalue = 0;
189  HistogramType::Iterator iter = histogram->Begin();
190 
191  while ( iter != histogram->End() )
192  {
193  if( static_cast<unsigned int>(iter.GetFrequency()) > maxvalue )
194  maxvalue = static_cast<unsigned int>(iter.GetFrequency());
195  ++iter;
196  }
197 
198  // Number of sample per label to reach in order to have a balanced
199  // ListSample
200  unsigned int balancedFrequency = m_BalancingFactor * maxvalue;
201 
202  // Guess how much noised samples must be added per sample to get
203  // a balanced ListSample : Computed using the
204  // - Frequency of each label (stored in the histogram)
205  // - The value maxvalue by m_BalancingFactor
206  // The std::vector below stores the multiplicative factor
207  iter = histogram->Begin();
208  while ( iter != histogram->End() )
209  {
210  if(iter.GetFrequency() - 1e-10 < 0.)
211  m_MultiplicativeCoefficient.push_back(0);
212  else
213  {
214  unsigned int coeff = static_cast<unsigned int>(balancedFrequency/iter.GetFrequency());
215  m_MultiplicativeCoefficient.push_back(coeff);
216  }
217 
218  ++iter;
219  }
220 }
221 
222 template < class TInputSampleList, class TLabelSampleList, class TOutputSampleList >
223 void
226 {
227  // Get the how much each sample must be expanded
228  this->ComputeMaxSampleFrequency();
229 
230  // Retrieve input and output pointers
231  typename InputSampleListObjectType::ConstPointer inputPtr = this->GetInput();
232  typename LabelSampleListObjectType::ConstPointer labelPtr = this->GetInputLabel();
233  typename OutputSampleListObjectType::Pointer outputPtr = this->GetOutput();
234 
235  typename LabelSampleListObjectType::Pointer outputLabelPtr = this->GetOutputLabel();
236  typename LabelSampleListType::Pointer outputLabel = const_cast<LabelSampleListType*>(outputLabelPtr->Get());
237 
238 
239  // Retrieve the ListSample
240  InputSampleListConstPointer inputSampleListPtr = inputPtr->Get();
241  LabelSampleListConstPointer labelSampleListPtr = labelPtr->Get();
242  OutputSampleListPointer outputSampleListPtr = const_cast<OutputSampleListType *>(outputPtr->Get());
243 
244  // Clear any previous output
245  outputSampleListPtr->Clear();
246 
247  typename InputSampleListType::ConstIterator inputIt = inputSampleListPtr->Begin();
248  typename LabelSampleListType::ConstIterator labelIt = labelSampleListPtr->Begin();
249 
250  // Set-up progress reporting
251  itk::ProgressReporter progress(this, 0, inputSampleListPtr->Size());
252 
253  // Iterate on the InputSampleList
254  while(inputIt != inputSampleListPtr->End() && labelIt != labelSampleListPtr->End())
255  {
256  // Retrieve current input sample
257  InputMeasurementVectorType currentInputMeasurement = inputIt.GetMeasurementVector();
258  // Retrieve the current label
259  LabelMeasurementVectorType currentLabelMeasurement = labelIt.GetMeasurementVector();
260 
261  // Build a temporary ListSample wiht the current
262  // measurement vector to generate noised versions of this
263  // measurement vector
264  InputSampleListPointer tempListSample = InputSampleListType::New();
265  tempListSample->PushBack(currentInputMeasurement);
266 
267  // Get how many times we have to noise this sample
268  unsigned int iterations = m_MultiplicativeCoefficient[currentLabelMeasurement[0]];
269 
270  // Noising filter
271  GaussianAdditiveNoisePointerType noisingFilter = GaussianAdditiveNoiseType::New();
272  noisingFilter->SetInput(tempListSample);
273  noisingFilter->SetNumberOfIteration(iterations);
274  noisingFilter->Update();
275 
276  // Build current output sample
277  OutputMeasurementVectorType currentOutputMeasurement;
278  currentOutputMeasurement.SetSize(currentInputMeasurement.GetSize());
279 
280  // Cast the current sample in outputSampleValue
281  for(unsigned int idx = 0; idx < inputSampleListPtr->GetMeasurementVectorSize(); ++idx)
282  currentOutputMeasurement[idx] = static_cast<OutputValueType>(currentInputMeasurement[idx]);
283 
284  // Add the current input casted sample to the output SampleList
285  outputSampleListPtr->PushBack(currentOutputMeasurement);
286 
287  // Add the currentsample list label
288  outputLabel->PushBack(currentLabelMeasurement);
289 
290  // Add the noised versions of the current sample to OutputSampleList
291  typename OutputSampleListType::ConstIterator tempIt = noisingFilter->GetOutput()->Get()->Begin();
292 
293  while(tempIt != noisingFilter->GetOutput()->Get()->End())
294  {
295  // Get the noised sample of the current measurement vector
296  OutputMeasurementVectorType currentTempMeasurement = tempIt.GetMeasurementVector();
297  // Add to output SampleList
298  outputSampleListPtr->PushBack(currentTempMeasurement);
299 
300  // Add a label in the output ListSample
301  outputLabel->PushBack(currentLabelMeasurement);
302 
303  ++tempIt;
304  }
305 
306  // Update progress
307  progress.CompletedPixel();
308 
309  ++inputIt;
310  ++ labelIt;
311  }
312 }
313 
314 template < class TInputSampleList, class TLabelSampleList, class TOutputSampleList >
315 void
317 ::PrintSelf(std::ostream& os, itk::Indent indent) const
318 {
319  // Call superclass implementation
320  Superclass::PrintSelf(os, indent);
321 }
322 
323 } // End namespace Statistics
324 } // End namespace otb
325 
326 #endif

Generated at Sun Feb 3 2013 00:34:49 for Orfeo Toolbox with doxygen 1.8.1.1