18 #ifndef __otbListSampleToBalancedListSampleFilter_txx
19 #define __otbListSampleToBalancedListSampleFilter_txx
24 #include "itkNumericTraits.h"
27 namespace Statistics {
30 template <
class TInputSampleList,
class TLabelSampleList,
class TOutputSampleList >
34 this->SetNumberOfRequiredInputs(2);
35 this->SetNumberOfRequiredOutputs(2);
41 m_AddGaussianNoiseFilter = GaussianAdditiveNoiseType::New();
42 m_BalancingFactor = 5;
45 template <
class TInputSampleList,
class TLabelSampleList,
class TOutputSampleList >
55 Superclass::MakeOutput(0);
60 labelListSample->Set(LabelSampleListType::New());
65 output =
static_cast<itk::DataObject*
>(InputSampleListObjectType::New().GetPointer());
72 template <
class TInputSampleList,
class TLabelSampleList,
class TOutputSampleList >
79 this->SetInputLabel(labelPtr);
83 template <
class TInputSampleList,
class TLabelSampleList,
class TOutputSampleList >
90 const_cast< LabelSampleListObjectType* >( labelPtr ) );
94 template <
class TInputSampleList,
class TLabelSampleList,
class TOutputSampleList >
96 ::LabelSampleListObjectType *
100 if (this->GetNumberOfInputs() < 2)
110 template <
class TInputSampleList,
class TLabelSampleList,
class TOutputSampleList >
112 ::LabelSampleListType *
116 if (this->GetNumberOfInputs() < 2)
123 return dataObjectPointer->Get();
127 template <
class TInputSampleList,
class TLabelSampleList,
class TOutputSampleList >
129 ::LabelSampleListType *
137 template <
class TInputSampleList,
class TLabelSampleList,
class TOutputSampleList >
139 ::LabelSampleListObjectType *
148 template <
class TInputSampleList,
class TLabelSampleList,
class TOutputSampleList >
154 LabelValueType maxLabel = itk::NumericTraits<LabelValueType>::min();
157 typename LabelSampleListType::ConstPointer labelPtr = this->GetLabelSampleList();
158 typename LabelSampleListType::ConstIterator labelIt = labelPtr->Begin();
160 while(labelIt != labelPtr->End())
165 if (currentInputMeasurement[0] > maxLabel)
166 maxLabel = currentInputMeasurement[0];
173 typename HistogramType::Pointer histogram = HistogramType::New();
174 typename HistogramType::SizeType size;
175 size.Fill(maxLabel +1);
176 histogram->Initialize(size);
178 labelIt = labelPtr->Begin();
179 while (labelIt != labelPtr->End())
183 histogram->IncreaseFrequency(currentInputMeasurement[0], 1);
188 unsigned int maxvalue = 0;
189 HistogramType::Iterator iter = histogram->Begin();
191 while ( iter != histogram->End() )
193 if( static_cast<unsigned int>(iter.GetFrequency()) > maxvalue )
194 maxvalue =
static_cast<unsigned int>(iter.GetFrequency());
200 unsigned int balancedFrequency = m_BalancingFactor * maxvalue;
207 iter = histogram->Begin();
208 while ( iter != histogram->End() )
210 if(iter.GetFrequency() - 1e-10 < 0.)
211 m_MultiplicativeCoefficient.push_back(0);
214 unsigned int coeff =
static_cast<unsigned int>(balancedFrequency/iter.GetFrequency());
215 m_MultiplicativeCoefficient.push_back(coeff);
222 template <
class TInputSampleList,
class TLabelSampleList,
class TOutputSampleList >
228 this->ComputeMaxSampleFrequency();
236 typename LabelSampleListType::Pointer outputLabel =
const_cast<LabelSampleListType*
>(outputLabelPtr->Get());
245 outputSampleListPtr->Clear();
247 typename InputSampleListType::ConstIterator inputIt = inputSampleListPtr->Begin();
248 typename LabelSampleListType::ConstIterator labelIt = labelSampleListPtr->Begin();
254 while(inputIt != inputSampleListPtr->End() && labelIt != labelSampleListPtr->End())
265 tempListSample->PushBack(currentInputMeasurement);
268 unsigned int iterations = m_MultiplicativeCoefficient[currentLabelMeasurement[0]];
272 noisingFilter->SetInput(tempListSample);
273 noisingFilter->SetNumberOfIteration(iterations);
274 noisingFilter->Update();
278 currentOutputMeasurement.SetSize(currentInputMeasurement.GetSize());
281 for(
unsigned int idx = 0; idx < inputSampleListPtr->GetMeasurementVectorSize(); ++idx)
282 currentOutputMeasurement[idx] = static_cast<OutputValueType>(currentInputMeasurement[idx]);
285 outputSampleListPtr->PushBack(currentOutputMeasurement);
288 outputLabel->PushBack(currentLabelMeasurement);
291 typename OutputSampleListType::ConstIterator tempIt = noisingFilter->GetOutput()->Get()->Begin();
293 while(tempIt != noisingFilter->GetOutput()->Get()->End())
298 outputSampleListPtr->PushBack(currentTempMeasurement);
301 outputLabel->PushBack(currentLabelMeasurement);
307 progress.CompletedPixel();
314 template <
class TInputSampleList,
class TLabelSampleList,
class TOutputSampleList >
320 Superclass::PrintSelf(os, indent);