22 #ifndef otbStreamingHistogramVectorImageFilter_hxx
23 #define otbStreamingHistogramVectorImageFilter_hxx
26 #include "itkImageRegionIterator.h"
27 #include "itkImageRegionConstIteratorWithIndex.h"
28 #include "itkProgressReporter.h"
34 template <
class TInputImage>
36 : m_ThreadHistogramList(),
53 this->itk::ProcessObject::SetNthOutput(1, output.GetPointer());
56 template <
class TInputImage>
59 itk::DataObject::Pointer ret;
63 ret =
static_cast<itk::DataObject*
>(TInputImage::New().GetPointer());
66 ret =
static_cast<itk::DataObject*
>(HistogramListType::New().GetPointer());
72 template <
class TInputImage>
78 template <
class TInputImage>
82 return static_cast<const HistogramListType*
>(this->itk::ProcessObject::GetOutput(1));
85 template <
class TInputImage>
88 Superclass::GenerateOutputInformation();
91 this->GetOutput()->CopyInformation(this->GetInput());
92 this->GetOutput()->SetLargestPossibleRegion(this->GetInput()->GetLargestPossibleRegion());
94 if (this->GetOutput()->GetRequestedRegion().GetNumberOfPixels() == 0)
96 this->GetOutput()->SetRequestedRegion(this->GetOutput()->GetLargestPossibleRegion());
101 template <
class TInputImage>
111 template <
class TInputImage>
114 TInputImage* inputPtr =
const_cast<TInputImage*
>(this->GetInput());
115 inputPtr->UpdateOutputInformation();
117 unsigned int numberOfThreads = this->GetNumberOfThreads();
118 unsigned int numberOfComponent = inputPtr->GetNumberOfComponentsPerPixel();
121 bool clipBins =
false;
124 if (m_HistogramMin.Size() != numberOfComponent || m_HistogramMax.Size() != numberOfComponent)
126 m_HistogramMin.SetSize(numberOfComponent);
127 m_HistogramMax.SetSize(numberOfComponent);
129 m_HistogramMin.Fill(itk::NumericTraits<InternalPixelType>::Zero);
130 m_HistogramMax.Fill(255);
135 outputHisto->
Clear();
136 for (
unsigned int k = 0; k < numberOfComponent; ++k)
138 typename HistogramType::MeasurementVectorType bandMin, bandMax;
141 bandMin.Fill(m_HistogramMin[k]);
142 bandMax.Fill(m_HistogramMax[k]);
144 typename HistogramType::Pointer histogram = HistogramType::New();
145 histogram->SetClipBinsAtEnds(clipBins);
147 typename HistogramType::SizeType size;
149 size.Fill(m_Size[k]);
150 histogram->SetMeasurementVectorSize(1);
151 histogram->Initialize(size, bandMin, bandMax);
158 m_ThreadHistogramList.clear();
159 for (
unsigned int i = 0; i < numberOfThreads; ++i)
163 for (
unsigned int k = 0; k < numberOfComponent; ++k)
165 typename HistogramType::MeasurementVectorType bandMin, bandMax;
168 bandMin.Fill(m_HistogramMin[k]);
169 bandMax.Fill(m_HistogramMax[k]);
171 typename HistogramType::Pointer histogram = HistogramType::New();
172 histogram->SetClipBinsAtEnds(clipBins);
174 typename HistogramType::SizeType size;
176 size.Fill(m_Size[k]);
177 histogram->SetMeasurementVectorSize(1);
178 histogram->Initialize(size, bandMin, bandMax);
180 histoList->PushBack(histogram);
182 m_ThreadHistogramList.push_back(histoList);
186 template <
class TInputImage>
191 int numberOfThreads = this->GetNumberOfThreads();
192 unsigned int numberOfComponent = this->GetInput()->GetNumberOfComponentsPerPixel();
195 for (
int i = 0; i < numberOfThreads; ++i)
197 for (
unsigned int j = 0; j < numberOfComponent; ++j)
200 HistogramType* threadHisto = m_ThreadHistogramList[i]->GetNthElement(j);
202 typename HistogramType::Iterator iterOutput = outHisto->Begin();
203 typename HistogramType::Iterator iterThread = threadHisto->Begin();
205 while (iterOutput != outHisto->End() && iterThread != threadHisto->End())
207 iterOutput.SetFrequency(iterOutput.GetFrequency() + iterThread.GetFrequency());
216 template <
class TInputImage>
224 itk::ProgressReporter progress(
this, threadId, outputRegionForThread.GetNumberOfPixels());
227 typename HistogramType::IndexType index;
229 itk::ImageRegionConstIteratorWithIndex<TInputImage> it(inputPtr, outputRegionForThread);
232 bool skipSample =
false;
235 while (!it.IsAtEnd())
237 if (m_SubSamplingRate > 1)
240 for (
unsigned int i = 0; i < InputImageDimension; ++i)
242 if (it.GetIndex()[i] % m_SubSamplingRate != 0)
251 progress.CompletedPixel();
258 bool skipSampleNoData =
false;
261 unsigned int itComp = 0;
262 while (itComp < vectorValue.GetSize())
264 if (vectorValue[itComp] == m_NoDataValue)
266 skipSampleNoData =
true;
271 skipSampleNoData =
false;
277 if (!skipSampleNoData)
279 for (
unsigned int j = 0; j < vectorValue.GetSize(); ++j)
281 typename HistogramType::MeasurementVectorType value;
283 value.Fill(vectorValue[j]);
285 m_ThreadHistogramList[threadId]->GetNthElement(j)->GetIndex(value, index);
286 if (!m_ThreadHistogramList[threadId]->GetNthElement(j)->IsIndexOutOfBounds(index))
294 m_ThreadHistogramList[threadId]->GetNthElement(j)->IncreaseFrequencyOfIndex(index, 1);
300 progress.CompletedPixel();
304 template <
class TImage>
307 Superclass::PrintSelf(os, indent);
309 os << indent <<
"Histogram minimum: " << this->GetHistogramMin() << std::endl;
310 os << indent <<
"Histogram maximum: " << this->GetHistogramMax() << std::endl;
311 os << indent <<
"Number of bins: " << m_Size[0] << std::endl;
314 os << indent <<
"Use NoData: true" << std::endl;
318 os << indent <<
"Use NoData: false" << std::endl;
320 os << indent <<
"NoData value: " << this->GetNoDataValue() << std::endl;