ANIMA  4.0
animaMeanAndVarianceImagesFilter.hxx
Go to the documentation of this file.
1 #pragma once
3 
4 #include <itkConstNeighborhoodIterator.h>
5 #include <itkNeighborhoodInnerProduct.h>
6 #include <itkImageRegionIterator.h>
7 #include <itkOffset.h>
8 #include <itkProgressReporter.h>
9 
10 namespace anima
11 {
12 
13 template <class TInputImage, class TOutputImage>
16 {
17  m_Radius.Fill(1);
18  this->SetNumberOfRequiredOutputs( 2 );
19  this->SetNthOutput( 0, this->MakeOutput( 0 ) );
20  this->SetNthOutput( 1, this->MakeOutput( 1 ) );
21 }
22 
23 template< class TInputImage, class TOutputImage>
24 void
27 {
28  // Allocate output
29  typename OutputImageType::Pointer output1 = this->GetOutput(0);
30  typename OutputImageType::Pointer output2 = this->GetOutput(1);
31  typename InputImageType::ConstPointer input = this->GetInput();
32 
33  double sum, var;
34 
35  itk::ConstNeighborhoodIterator<InputImageType> bit(m_Radius, input,outputRegionForThread);
36  unsigned int neighborhoodSize = bit.Size();
37  itk::ImageRegionIterator<OutputImageType> it1(output1, outputRegionForThread);
38  itk::ImageRegionIterator<OutputImageType> it2(output2, outputRegionForThread);
39 
40  while ( ! bit.IsAtEnd() )
41  {
42  sum = itk::NumericTraits<InputRealType>::Zero;
43  var = itk::NumericTraits<InputRealType>::Zero;
44 
45  for (unsigned int i = 0; i < neighborhoodSize; ++i)
46  {
47  sum += static_cast<InputRealType>( bit.GetPixel(i) );
48  var += static_cast<InputRealType>( bit.GetPixel(i) * bit.GetPixel(i) );
49  }
50 
51  // get the mean value
52  OutputPixelType mean = static_cast<OutputPixelType>(sum / double(neighborhoodSize));
53  OutputPixelType variance = static_cast<OutputPixelType>(((var / neighborhoodSize) - (mean * mean)) *
54  ((double) neighborhoodSize / (neighborhoodSize - 1.0)));
55  it1.Set( mean );
56  it2.Set( variance );
57 
58  ++bit;
59  ++it1;
60  ++it2;
61  }
62 }
63 
67 template <class TInputImage, class TOutput>
68 void
70 ::PrintSelf(std::ostream& os, itk::Indent indent) const
71 {
72  Superclass::PrintSelf( os, indent );
73  os << indent << "Radius: " << m_Radius << std::endl;
74 
75 }
76 
77 } // end of namespace anima
itk::NumericTraits< InputPixelType >::RealType InputRealType
void DynamicThreadedGenerateData(const OutputImageRegionType &outputRegionForThread) ITK_OVERRIDE
void PrintSelf(std::ostream &os, itk::Indent indent) const ITK_OVERRIDE