22 #ifndef otbListSampleToVariableDimensionHistogramGenerator_hxx
23 #define otbListSampleToVariableDimensionHistogramGenerator_hxx
27 #include "itkStatisticsAlgorithm.h"
32 template <
class TListSample,
class THistogramMeasurement,
class TFrequencyContainer>
39 this->SetNumberOfRequiredInputs(1);
40 this->SetNumberOfRequiredOutputs(1);
42 this->itk::ProcessObject::SetNthOutput(0, this->
MakeOutput(0).GetPointer());
46 template <
class TListSample,
class THistogramMeasurement,
class TFrequencyContainer>
50 this->itk::ProcessObject::SetNthInput(0,
const_cast<ListSampleType*
>(inputlist));
54 template <
class TListSample,
class THistogramMeasurement,
class TFrequencyContainer>
58 if (this->GetNumberOfInputs() < 1)
62 return static_cast<const ListSampleType*
>(this->itk::ProcessObject::GetInput(0));
66 template <
class TListSample,
class THistogramMeasurement,
class TFrequencyContainer>
70 return dynamic_cast<HistogramType*
>(this->itk::ProcessObject::GetOutput(0));
74 template <
class TListSample,
class THistogramMeasurement,
class TFrequencyContainer>
80 output =
static_cast<itk::DataObject*
>(HistogramType::New().GetPointer());
85 template <
class TListSample,
class THistogramMeasurement,
class TFrequencyContainer>
92 typename HistogramType::Pointer histogram =
const_cast<HistogramType*
>(this->GetOutput());
94 otbMsgDebugMacro(<<
"ListSampleToVariableDimensionHistogramGenerator::GenerateData(): Entering");
96 if (m_Sizes.GetSize() != inputList->GetMeasurementVectorSize())
98 itkExceptionMacro(
"Sample list measurement vectors and histogram bin sizes vector have different dimensions !");
103 if (m_HistogramMin.GetSize() != inputList->GetMeasurementVectorSize())
105 itkExceptionMacro(
"Sample list measurement vectors and histogram min have different dimensions !");
107 if (m_HistogramMax.GetSize() != inputList->GetMeasurementVectorSize())
109 itkExceptionMacro(
"Sample list measurement vectors and histogram max have different dimensions !");
113 typename TListSample::MeasurementVectorType lower(inputList->GetMeasurementVectorSize());
114 typename TListSample::MeasurementVectorType upper(inputList->GetMeasurementVectorSize());
116 typename HistogramType::MeasurementVectorType h_upper(inputList->GetMeasurementVectorSize());
117 typename HistogramType::MeasurementVectorType h_lower(inputList->GetMeasurementVectorSize());
122 if (m_AutoMinMax && inputList->Size() != 0)
124 itk::Statistics::Algorithm::FindSampleBound<ListSampleType>(inputList, inputList->Begin(), inputList->End(), lower, upper);
127 for (
unsigned int i = 0; i < inputList->GetMeasurementVectorSize(); ++i)
129 if (!itk::NumericTraits<THistogramMeasurement>::is_integer)
131 margin = ((THistogramMeasurement)(upper[i] - lower[i]) / (THistogramMeasurement)m_Sizes[i]) / (THistogramMeasurement)m_MarginalScale;
132 h_upper[i] = (THistogramMeasurement)(upper[i] + margin);
133 if (h_upper[i] <= upper[i])
136 h_upper[i] = upper[i];
139 histogram->SetClipBinsAtEnds(
false);
146 h_upper[i] = ((THistogramMeasurement)upper[i]) + itk::NumericTraits<THistogramMeasurement>::One;
147 if (h_upper[i] <= upper[i])
150 h_upper[i] = upper[i];
153 histogram->SetClipBinsAtEnds(
false);
158 h_lower[i] = (THistogramMeasurement)lower[i];
163 h_lower = m_HistogramMin;
164 h_upper = m_HistogramMax;
170 histogram->SetMeasurementVectorSize(inputList->GetMeasurementVectorSize());
173 otbMsgDevMacro(<<
"ListSampleToVariableDimensionHistogramGenerator::GenerateData(): Initializing histogram with (sizes= " << m_Sizes
174 <<
", lower = " << h_lower <<
", upper = " << h_upper <<
")");
175 histogram->Initialize(m_Sizes, h_lower, h_upper);
176 otbMsgDevMacro(<<
"ListSampleToVariableDimensionHistogramGenerator::GenerateData(): Histogram initialized");
177 typename TListSample::ConstIterator iter = inputList->Begin();
178 typename TListSample::ConstIterator last = inputList->End();
179 typename HistogramType::IndexType index(inputList->GetMeasurementVectorSize());
180 typename TListSample::MeasurementVectorType lvector(inputList->GetMeasurementVectorSize());
181 typename HistogramType::MeasurementVectorType hvector(inputList->GetMeasurementVectorSize());
184 otbMsgDevMacro(<<
"ListSampleToVariableDimensionHistogramGenerator::GenerateData(): Filling the histogram");
187 lvector = iter.GetMeasurementVector();
188 for (i = 0; i < inputList->GetMeasurementVectorSize(); ++i)
190 hvector[i] = (THistogramMeasurement)lvector[i];
192 histogram->GetIndex(hvector, index);
194 if (!histogram->IsIndexOutOfBounds(index))
202 histogram->IncreaseFrequencyOfIndex(index, 1);
207 otbMsgDebugMacro(<<
"ListSampleToVariableDimensionHistogramGenerator::GenerateData(): Leaving");
210 template <
class TListSample,
class THistogramMeasurement,
class TFrequencyContainer>
212 itk::Indent indent)
const
214 Superclass::PrintSelf(os, indent);
215 os << indent <<
"AutoMinMax: " << m_AutoMinMax << std::endl;
216 os << indent <<
"Sizes: " << m_Sizes << std::endl;
217 os << indent <<
"MarginalScale: " << m_MarginalScale << std::endl;
218 os << indent <<
"HistogramMin: " << m_HistogramMin << std::endl;
219 os << indent <<
"HistogramMax: " << m_HistogramMax << std::endl;