21 #ifndef otbVarianceImageFilter_hxx
22 #define otbVarianceImageFilter_hxx
26 #include "itkConstNeighborhoodIterator.h"
27 #include "itkNeighborhoodInnerProduct.h"
28 #include "itkImageRegionIterator.h"
29 #include "itkNeighborhoodAlgorithm.h"
30 #include "itkOffset.h"
31 #include "itkProgressReporter.h"
36 template <
class TInputImage,
class TOutputImage>
42 template <
class TInputImage,
class TOutputImage>
46 Superclass::GenerateInputRequestedRegion();
49 typename Superclass::InputImagePointer inputPtr =
const_cast<TInputImage*
>(this->GetInput());
50 typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
52 if (!inputPtr || !outputPtr)
59 typename TInputImage::RegionType inputRequestedRegion;
60 inputRequestedRegion = inputPtr->GetRequestedRegion();
63 inputRequestedRegion.PadByRadius(m_Radius);
66 if (inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()))
68 inputPtr->SetRequestedRegion(inputRequestedRegion);
77 inputPtr->SetRequestedRegion(inputRequestedRegion);
80 itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
81 e.SetLocation(ITK_LOCATION);
82 e.SetDescription(
"Requested region is (at least partially) outside the largest possible region.");
83 e.SetDataObject(inputPtr);
88 template <
class TInputImage,
class TOutputImage>
92 itk::ZeroFluxNeumannBoundaryCondition<InputImageType> nbc;
94 itk::ConstNeighborhoodIterator<InputImageType> bit;
95 itk::ImageRegionIterator<OutputImageType> it;
98 typename OutputImageType::Pointer output = this->GetOutput();
99 typename InputImageType::ConstPointer input = this->GetInput();
102 typename itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<InputImageType>::FaceListType faceList;
103 itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<InputImageType> bC;
104 faceList = bC(input, outputRegionForThread, m_Radius);
106 typename itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<InputImageType>::FaceListType::iterator fit;
109 itk::ProgressReporter progress(
this, threadId, outputRegionForThread.GetNumberOfPixels());
116 for (fit = faceList.begin(); fit != faceList.end(); ++fit)
118 bit = itk::ConstNeighborhoodIterator<InputImageType>(m_Radius, input, *fit);
119 unsigned int neighborhoodSize = bit.Size();
120 it = itk::ImageRegionIterator<OutputImageType>(output, *fit);
121 bit.OverrideBoundaryCondition(&nbc);
124 while (!bit.IsAtEnd())
126 sum = itk::NumericTraits<InputRealType>::Zero;
127 sumOfSquares = itk::NumericTraits<InputRealType>::Zero;
128 for (i = 0; i < neighborhoodSize; ++i)
132 sumOfSquares += value * value;
136 const double num =
static_cast<double>(neighborhoodSize);
137 it.Set(
static_cast<OutputPixelType>(sumOfSquares - (sum * sum / num)) / (num - 1.0));
141 progress.CompletedPixel();
149 template <
class TInputImage,
class TOutput>
152 Superclass::PrintSelf(os, indent);
153 os << indent <<
"Radius: " << m_Radius << std::endl;