21 #ifndef otbLocalHistogramImageFunction_hxx
22 #define otbLocalHistogramImageFunction_hxx
25 #include "itkConstNeighborhoodIterator.h"
26 #include "itkExtractImageFilter.h"
35 template <
class TInputImage,
class TCoordRep>
37 : m_NeighborhoodRadius(1), m_NumberOfHistogramBins(128), m_HistogramMin(0), m_HistogramMax(1), m_GaussianSmoothing(true)
41 template <
class TInputImage,
class TCoordRep>
44 this->Superclass::PrintSelf(os, indent);
45 os << indent <<
" Neighborhood radius value : " << this->GetNeighborhoodRadius() << std::endl;
46 os << indent <<
" Number Of Histogram Bins : " << this->GetNumberOfHistogramBins() << std::endl;
47 os << indent <<
" Histogram Minimum : " << this->GetHistogramMin() << std::endl;
48 os << indent <<
" Histogram Maximum : " << this->GetHistogramMax() << std::endl;
51 template <
class TInputImage,
class TCoordRep>
55 typename HistogramType::Pointer histogram = HistogramType::New();
57 typename HistogramType::SizeType size(this->GetInputImage()->GetNumberOfComponentsPerPixel());
58 size.Fill(this->GetNumberOfHistogramBins());
60 typename HistogramType::MeasurementVectorType lowerBound(this->GetInputImage()->GetNumberOfComponentsPerPixel());
61 typename HistogramType::MeasurementVectorType upperBound(this->GetInputImage()->GetNumberOfComponentsPerPixel());
63 lowerBound.Fill(
static_cast<typename HistogramType::MeasurementType
>(this->GetHistogramMin()));
64 upperBound.Fill(
static_cast<typename HistogramType::MeasurementType
>(this->GetHistogramMax()));
66 histogram->SetMeasurementVectorSize(this->GetInputImage()->GetNumberOfComponentsPerPixel());
67 histogram->Initialize(size, lowerBound, upperBound);
68 histogram->SetToZero();
71 if (!this->GetInputImage())
77 if (!this->IsInsideBuffer(index))
83 typename InputImageType::SizeType kernelSize;
84 kernelSize.Fill(m_NeighborhoodRadius);
86 itk::ConstNeighborhoodIterator<InputImageType> it(kernelSize, this->GetInputImage(), this->GetInputImage()->GetBufferedRegion());
89 it.SetLocation(index);
92 double squaredRadius = m_NeighborhoodRadius * m_NeighborhoodRadius;
93 double squaredSigma = 0.25 * squaredRadius;
96 typename InputImageType::OffsetType offset;
99 for (
int i = -(
int)m_NeighborhoodRadius; i < (int)m_NeighborhoodRadius; ++i)
101 for (
int j = -(
int)m_NeighborhoodRadius; j < (int)m_NeighborhoodRadius; ++j)
104 double currentSquaredRadius = i * i + j * j;
105 if (currentSquaredRadius < squaredRadius)
110 if (m_GaussianSmoothing)
112 gWeight = (1 / std::sqrt(
otb::CONST_2PI * squaredSigma)) * std::exp(-currentSquaredRadius / (2 * squaredSigma));
120 typename HistogramType::MeasurementVectorType sample(this->GetInputImage()->GetNumberOfComponentsPerPixel());
121 sample[0] = it.GetPixel(offset);
124 histogram->IncreaseFrequencyOfMeasurement(sample, gWeight);