22 #ifndef otbListSampleToHistogramListGenerator_hxx
23 #define otbListSampleToHistogramListGenerator_hxx
32 template <
class TListSample,
class THistogramMeasurement,
class TFrequencyContainer>
34 : m_Size(10), m_MarginalScale(100), m_AutoMinMax(true), m_NoDataFlag(false), m_NoDataValue(
itk::NumericTraits<THistogramMeasurement>::Zero)
38 this->SetNumberOfRequiredInputs(1);
39 this->SetNumberOfRequiredOutputs(1);
41 this->itk::ProcessObject::SetNthOutput(0, this->
MakeOutput(0).GetPointer());
45 template <
class TListSample,
class THistogramMeasurement,
class TFrequencyContainer>
49 this->itk::ProcessObject::SetNthInput(0,
const_cast<ListSampleType*
>(inputlist));
53 template <
class TListSample,
class THistogramMeasurement,
class TFrequencyContainer>
57 if (this->GetNumberOfInputs() < 1)
61 return static_cast<const ListSampleType*
>(this->itk::ProcessObject::GetInput(0));
65 template <
class TListSample,
class THistogramMeasurement,
class TFrequencyContainer>
73 template <
class TListSample,
class THistogramMeasurement,
class TFrequencyContainer>
78 output =
static_cast<itk::DataObject*
>(HistogramListType::New().GetPointer());
83 template <
class TListSample,
class THistogramMeasurement,
class TFrequencyContainer>
86 otbMsgDebugMacro(<<
"ListSampleToHistogramListGenerator::GenerateData(): Entering");
96 if (m_HistogramMin.GetSize() != inputList->GetMeasurementVectorSize())
98 itkExceptionMacro(
"Sample list measurement vectors and histogram min have different dimensions !");
100 if (m_HistogramMax.GetSize() != inputList->GetMeasurementVectorSize())
102 itkExceptionMacro(
"Sample list measurement vectors and histogram max have different dimensions !");
105 typename TListSample::MeasurementVectorType lower(inputList->GetMeasurementVectorSize());
106 typename TListSample::MeasurementVectorType upper(inputList->GetMeasurementVectorSize());
108 typename TListSample::MeasurementVectorType h_upper(inputList->GetMeasurementVectorSize());
109 typename TListSample::MeasurementVectorType h_lower(inputList->GetMeasurementVectorSize());
111 bool clipBinsAtEnds =
true;
116 if (m_AutoMinMax && inputList->Size() != 0)
118 itk::Statistics::Algorithm::FindSampleBound<ListSampleType>(inputList, inputList->Begin(), inputList->End(), lower, upper);
121 for (
unsigned int i = 0; i < inputList->GetMeasurementVectorSize(); ++i)
123 if (!itk::NumericTraits<THistogramMeasurement>::is_integer)
125 margin = ((THistogramMeasurement)(upper[i] - lower[i]) / (THistogramMeasurement)m_Size[0]) / (THistogramMeasurement)m_MarginalScale;
126 h_upper[i] = (THistogramMeasurement)(upper[i] + margin);
127 if (h_upper[i] <= upper[i])
130 h_upper[i] = upper[i];
133 clipBinsAtEnds =
false;
140 h_upper[i] = ((THistogramMeasurement)upper[i]) + itk::NumericTraits<THistogramMeasurement>::One;
141 if (h_upper[i] <= upper[i])
144 h_upper[i] = upper[i];
147 clipBinsAtEnds =
false;
152 h_lower[i] = (THistogramMeasurement)lower[i];
157 h_lower = m_HistogramMin;
158 h_upper = m_HistogramMax;
162 histogramList->Clear();
165 for (
unsigned int comp = 0; comp < inputList->GetMeasurementVectorSize(); ++comp)
169 typename HistogramType::MeasurementVectorType comp_lower(inputList->GetMeasurementVectorSize());
170 typename HistogramType::MeasurementVectorType comp_upper(inputList->GetMeasurementVectorSize());
172 comp_lower[0] = h_lower[comp];
173 comp_upper[0] = h_upper[comp];
175 otbMsgDevMacro(<<
"ListSampleToHistogramListGenerator::GenerateData(): Initializing histogram " << comp <<
" with (size= " << m_Size
176 <<
", lower = " << comp_lower <<
", upper = " << comp_upper <<
")");
180 histogramList->PushBack(HistogramType::New());
181 histogramList->Back()->SetMeasurementVectorSize(1);
182 histogramList->Back()->SetClipBinsAtEnds(clipBinsAtEnds);
183 histogramList->Back()->Initialize(m_Size, comp_lower, comp_upper);
185 typename TListSample::ConstIterator iter = inputList->Begin();
186 typename TListSample::ConstIterator last = inputList->End();
187 typename HistogramType::IndexType index;
188 typename HistogramType::MeasurementVectorType hvector(inputList->GetMeasurementVectorSize());
192 hvector[0] =
static_cast<THistogramMeasurement
>(iter.GetMeasurementVector()[comp]);
193 histogramList->Back()->GetIndex(hvector, index);
194 if ((!m_NoDataFlag) || hvector[0] != m_NoDataValue)
198 if (!histogramList->Back()->IsIndexOutOfBounds(index))
206 histogramList->Back()->IncreaseFrequencyOfIndex(index, 1);
212 otbMsgDebugMacro(<<
"ListSampleToHistogramListGenerator::GenerateData(): Leaving");
215 template <
class TListSample,
class THistogramMeasurement,
class TFrequencyContainer>
218 Superclass::PrintSelf(os, indent);
219 os << indent <<
"AutoMinMax: " << m_AutoMinMax << std::endl;
220 os << indent <<
"Size: " << m_Size << std::endl;
221 os << indent <<
"MarginalScale: " << m_MarginalScale << std::endl;
222 os << indent <<
"HistogramMin: " << m_HistogramMin << std::endl;
223 os << indent <<
"HistogramMax: " << m_HistogramMax << std::endl;