21 #ifndef otbBinaryImageToDensityImageFilter_hxx
22 #define otbBinaryImageToDensityImageFilter_hxx
25 #include "itkImageRegionIterator.h"
26 #include "itkProgressReporter.h"
27 #include "itkZeroFluxNeumannBoundaryCondition.h"
28 #include "itkNeighborhoodAlgorithm.h"
35 template <
class TInputImage,
class TOutputImage,
class TCountFunction>
38 m_NeighborhoodRadius.Fill(1);
39 m_CountFunction = CountFunctionType::New();
44 template <
class TInputImage,
class TOutputImage,
class TCountFunction>
49 template <
class TInputImage,
class TOutputImage,
class TCountFunction>
53 Superclass::GenerateInputRequestedRegion();
56 InputImagePointerType inputPtr =
const_cast<TInputImage*
>(this->GetInput());
57 OutputImagePointerType outputPtr = this->GetOutput();
59 if (!inputPtr || !outputPtr)
65 InputImageRegionType inputRequestedRegion = inputPtr->GetRequestedRegion();
68 inputRequestedRegion.PadByRadius(m_NeighborhoodRadius);
71 if (inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()))
73 inputPtr->SetRequestedRegion(inputRequestedRegion);
82 inputPtr->SetRequestedRegion(inputRequestedRegion);
85 itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
86 std::ostringstream msg;
87 msg << this->GetNameOfClass() <<
"::GenerateInputRequestedRegion()";
88 e.SetLocation(msg.str());
89 e.SetDescription(
"Requested region is (at least partially) outside the largest possible region.");
90 e.SetDataObject(inputPtr);
95 template <
class TInputImage,
class TOutputImage,
class TCountFunction>
98 Superclass::BeforeThreadedGenerateData();
100 m_CountFunction->SetInputImage(this->GetInput());
101 m_CountFunction->SetNeighborhoodRadius(m_NeighborhoodRadius);
105 template <
class TInputImage,
class TOutputImage,
class TCountFunction>
107 itk::ThreadIdType threadId)
109 InputImagePointerType inputPtr =
const_cast<InputImageType*
>(this->GetInput());
110 OutputImagePointerType outputPtr = this->GetOutput();
113 itk::ZeroFluxNeumannBoundaryCondition<TInputImage> nbc;
115 r[0] = m_NeighborhoodRadius[0];
116 r[1] = m_NeighborhoodRadius[1];
118 NeighborhoodIteratorType it;
119 typename itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<TInputImage>::FaceListType faceList;
120 typename itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<TInputImage> bC;
121 faceList = bC(inputPtr, outputRegionForThread, r);
122 typename itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<TInputImage>::FaceListType::iterator fit;
124 itk::ImageRegionIterator<OutputImageType> itOut(outputPtr, outputRegionForThread);
126 itk::ProgressReporter progress(
this, threadId, outputRegionForThread.GetNumberOfPixels());
128 typename InputImageType::IndexType index;
130 for (fit = faceList.begin(); fit != faceList.end(); ++fit)
132 it = itk::ConstNeighborhoodIterator<TInputImage>(r, inputPtr, *fit);
134 itOut = itk::ImageRegionIterator<TOutputImage>(outputPtr, *fit);
135 it.OverrideBoundaryCondition(&nbc);
138 while (!itOut.IsAtEnd())
140 index = it.GetIndex();
142 if (outputRegionForThread.IsInside(index))
144 itOut.Set(m_CountFunction->EvaluateAtIndex(index));
149 progress.CompletedPixel();