22 #ifndef otbVectorRescaleIntensityImageFilter_hxx
23 #define otbVectorRescaleIntensityImageFilter_hxx
26 #include "itkSampleToHistogramFilter.h"
27 #include "itkHistogram.h"
28 #include "itkListSample.h"
31 #include "itkImageRegionConstIterator.h"
38 template <
class TInputImage,
class TOutputImage>
41 m_ClampThreshold = 0.01;
43 m_AutomaticInputMinMaxComputation =
true;
49 template <
class TInputImage,
class TOutputImage>
52 this->Superclass::GenerateOutputInformation();
53 this->GetOutput()->SetNumberOfComponentsPerPixel(this->GetInput()->GetNumberOfComponentsPerPixel());
59 template <
class TInputImage,
class TOutputImage>
64 typename TInputImage::Pointer input =
const_cast<TInputImage*
>(this->GetInput());
65 typename TInputImage::RegionType inputRegion;
66 this->CallCopyOutputRegionToInputRegion(inputRegion, this->GetOutput()->GetRequestedRegion());
67 input->SetRequestedRegion(inputRegion);
74 template <
class TInputImage,
class TOutputImage>
77 if (m_ClampThreshold < 0.)
79 itkExceptionMacro(<<
"Invalid Clamp Threshold must be greater than 0.0");
82 if (m_AutomaticInputMinMaxComputation)
89 InputImagePointer inputImage = this->GetInput();
91 typedef itk::ImageRegionConstIterator<InputImageType> InputIterator;
92 typedef itk::Vector<typename InputImageType::InternalPixelType, 1> MeasurementVectorType;
93 typedef itk::Statistics::ListSample<MeasurementVectorType> ListSampleType;
94 typedef float HistogramMeasurementType;
95 typedef itk::Statistics::Histogram<HistogramMeasurementType, itk::Statistics::DenseFrequencyContainer2> HistogramType;
96 typedef itk::Statistics::SampleToHistogramFilter<ListSampleType, HistogramType> HistogramGeneratorType;
98 typedef ObjectList<ListSampleType> ListSampleListType;
100 m_InputMinimum.SetSize(inputImage->GetNumberOfComponentsPerPixel());
101 m_InputMaximum.SetSize(inputImage->GetNumberOfComponentsPerPixel());
102 typename ListSampleListType::Pointer sl = ListSampleListType::New();
104 sl->Reserve(inputImage->GetNumberOfComponentsPerPixel());
106 for (
unsigned int i = 0; i < m_InputMaximum.GetSize(); ++i)
108 sl->PushBack(ListSampleType::New());
111 InputIterator it(inputImage, inputImage->GetBufferedRegion());
115 while (!it.IsAtEnd())
117 InputPixelType pixel = it.Get();
118 for (
unsigned int i = 0; i < m_InputMaximum.GetSize(); ++i)
120 sl->GetNthElement(i)->PushBack(pixel[i]);
125 for (
unsigned int i = 0; i < m_InputMaximum.GetSize(); ++i)
127 typename HistogramGeneratorType::Pointer generator = HistogramGeneratorType::New();
128 generator->SetInput(sl->GetNthElement(i));
129 typename HistogramGeneratorType::HistogramType::SizeType size;
132 size.SetSize(sl->GetNthElement(i)->GetMeasurementVectorSize());
134 if (m_ClampThreshold > 0.)
135 size.Fill(
static_cast<unsigned int>(std::ceil(1 / m_ClampThreshold) * 10));
139 generator->SetHistogramSize(size);
142 m_InputMinimum[i] =
static_cast<typename InputImageType::InternalPixelType
>(generator->GetOutput()->Quantile(0, m_ClampThreshold));
143 m_InputMaximum[i] =
static_cast<typename InputImageType::InternalPixelType
>(generator->GetOutput()->Quantile(0, 1 - m_ClampThreshold));
148 this->GetFunctor().SetInputMinimum(m_InputMinimum);
149 this->GetFunctor().SetInputMaximum(m_InputMaximum);
150 this->GetFunctor().SetOutputMaximum(m_OutputMaximum);
151 this->GetFunctor().SetOutputMinimum(m_OutputMinimum);
152 this->GetFunctor().SetGamma(m_Gamma);