21 #ifndef otbFrostImageFilter_hxx
22 #define otbFrostImageFilter_hxx
26 #include "itkDataObject.h"
27 #include "itkConstNeighborhoodIterator.h"
28 #include "itkNeighborhoodInnerProduct.h"
29 #include "itkImageRegionIterator.h"
30 #include "itkNeighborhoodAlgorithm.h"
31 #include "itkOffset.h"
32 #include "itkProgressReporter.h"
40 template <
class TInputImage,
class TOutputImage>
48 template <
class TInputImage,
class TOutputImage>
52 Superclass::GenerateInputRequestedRegion();
55 typename Superclass::InputImagePointer inputPtr =
const_cast<TInputImage*
>(this->GetInput());
56 typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
58 if (!inputPtr || !outputPtr)
65 typename TInputImage::RegionType inputRequestedRegion;
66 inputRequestedRegion = inputPtr->GetRequestedRegion();
69 inputRequestedRegion.PadByRadius(m_Radius);
72 if (inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()))
74 inputPtr->SetRequestedRegion(inputRequestedRegion);
83 inputPtr->SetRequestedRegion(inputRequestedRegion);
86 itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
87 std::ostringstream msg;
88 msg << static_cast<const char*>(this->GetNameOfClass()) <<
"::GenerateInputRequestedRegion()";
89 e.SetLocation(msg.str());
90 e.SetDescription(
"Requested region is (at least partially) outside the largest possible region.");
91 e.SetDataObject(inputPtr);
96 template <
class TInputImage,
class TOutputImage>
100 itk::ZeroFluxNeumannBoundaryCondition<InputImageType> nbc;
101 itk::ConstNeighborhoodIterator<InputImageType> bit;
102 typename itk::ConstNeighborhoodIterator<InputImageType>::OffsetType off;
103 itk::ImageRegionIterator<OutputImageType> it;
106 typename OutputImageType::Pointer output = this->GetOutput();
107 typename InputImageType::ConstPointer input = this->GetInput();
110 typename itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<InputImageType>::FaceListType faceList;
111 typename itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<InputImageType>::FaceListType::iterator fit;
113 itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<InputImageType> bC;
114 faceList = bC(input, outputRegionForThread, m_Radius);
117 itk::ProgressReporter progress(
this, threadId, outputRegionForThread.GetNumberOfPixels());
122 double Mean, Variance;
131 for (fit = faceList.begin(); fit != faceList.end(); ++fit)
133 bit = itk::ConstNeighborhoodIterator<InputImageType>(m_Radius, input, *fit);
134 unsigned int neighborhoodSize = bit.Size();
135 it = itk::ImageRegionIterator<OutputImageType>(output, *fit);
136 bit.OverrideBoundaryCondition(&nbc);
141 while (!bit.IsAtEnd())
143 sum = itk::NumericTraits<InputRealType>::Zero;
144 sum2 = itk::NumericTraits<InputRealType>::Zero;
146 for (i = 0; i < neighborhoodSize; ++i)
148 dPixel =
static_cast<double>(bit.GetPixel(i));
151 Mean = sum /
static_cast<double>(neighborhoodSize);
153 for (i = 0; i < neighborhoodSize; ++i)
155 dPixel =
static_cast<double>(bit.GetPixel(i));
156 sum2 += (dPixel - Mean) * (dPixel - Mean);
158 Variance = sum2 / double(neighborhoodSize - 1);
160 const double epsilon = 0.0000000001;
161 if (std::abs(Mean) < epsilon)
163 dPixel = itk::NumericTraits<OutputPixelType>::Zero;
165 else if (std::abs(Variance) < epsilon)
171 Alpha = m_Deramp * Variance / (Mean * Mean);
176 const int rad_x = m_Radius[0];
177 const int rad_y = m_Radius[1];
179 for (
int x = -rad_x; x <= rad_x; ++x)
181 for (
int y = -rad_y; y <= rad_y; ++y)
183 double Dist = std::sqrt(
static_cast<double>(x * x + y * y));
187 dPixel =
static_cast<double>(bit.GetPixel(off));
189 CoefFilter = std::exp(-Alpha * Dist);
190 NormFilter += CoefFilter;
191 FrostFilter += (CoefFilter * dPixel);
195 dPixel = FrostFilter / NormFilter;
202 progress.CompletedPixel();
210 template <
class TInputImage,
class TOutput>
213 Superclass::PrintSelf(os, indent);
214 os << indent <<
"Radius: " << m_Radius << std::endl;