CBICA Toolkit  1.0.0
itkNaryVarianceImageFilter.h
Go to the documentation of this file.
1 /**
2 \file itkNaryVarianceImageFilter.h
3 
4 \brief Declaration & Implementation of the NaryFunctorImageFilter class
5 
6 https://www.cbica.upenn.edu/sbia/software/ <br>
7 software@cbica.upenn.edu
8 
9 Copyright (c) 2015 University of Pennsylvania. All rights reserved. <br>
10 See COPYING file or https://www.cbica.upenn.edu/sbia/software/license.html
11 
12 */
13 #pragma once
14 
15 #include "itkNaryFunctorImageFilter.h"
16 #include "itkNumericTraits.h"
17 #include "itkDiffusionTensor3D.h"
18 
19 //#include "itkSymRealSphericalHarmonicRep.h"
20 
21 namespace itk
22 {
23 
24  namespace Functor
25  {
26  /**
27  \class Variance
28 
29  \brief Helper class for calculating variance of images which interfaces with NaryVarianceImageFilter
30  */
31  template< class TInput, class TOutput >
32  class Variance
33  {
34  public:
35  typedef typename NumericTraits< TOutput >::RealType RealType;
36  Variance() {}
37  ~Variance() {}
38  inline TOutput operator()( const std::vector< TInput > & B)
39  {
40  RealType sum = NumericTraits< TOutput >::Zero;
41  RealType sum_sqr = NumericTraits< TOutput >::Zero;
42 
43  for( unsigned int i=0; i< B.size(); i++ )
44  {
45  sum_sqr += static_cast< TOutput >(B[i]) * static_cast< TOutput >(B[i]);
46  sum += static_cast< TOutput >(B[i]);
47  }
48  return static_cast<TOutput>( (sum_sqr - (sum * sum) / B.size() ) /B.size() );
49  }
50  bool operator== (const Variance&) const
51  {
52  return true;
53  }
54  bool operator!= (const Variance&) const
55  {
56  return false;
57  }
58  };
59  //
60  //template <class TCompType, unsigned int TOrder, class TOutput>
61  //class ITK_EXPORT Variance
62  // <SymRealSphericalHarmonicRep<TCompType,TOrder>, TOutput>
63  //{
64  //public:
65  // typedef typename NumericTraits< TOutput >::RealType RealType;
66  // typedef SymRealSphericalHarmonicRep<TCompType,TOrder> TInput;
67  // typedef typename NumericTraits< TInput >::RealType InputRealType;
68  //
69  // Variance() {}
70  // ~Variance() {}
71  // inline TOutput operator()( const std::vector< TInput > & B)
72  // {
73  // //First compute the mean
74  // InputRealType mean = NumericTraits<InputRealType>::Zero;
75  // for (unsigned int i=0;i<B.size();i++ )
76  // {
77  // mean += B[i];
78  // }
79  // mean /= B.size();
80  // //compute the variance
81  // RealType variance = NumericTraits<RealType>::Zero;
82  // for (unsigned int j=0;j<B.size();j++ )
83  // {
84  // for ( unsigned int i=0; i<TInput::Dimension; ++i )
85  // {
86  // variance += vcl_pow( (B[j])[i] - mean[i], 2);
87  // }
88  // }
89  // variance /= B.size();
90  //
91  // return static_cast<TOutput>( variance );
92  // }
93  // bool operator== (const Variance&) const
94  // {
95  // return true;
96  // }
97  // bool operator!= (const Variance&) const
98  // {
99  // return false;
100  // }
101  //};
102 
103  }
104 
105  /**
106  \class NaryVarianceImageFilter
107 
108  \brief Implements an operator for calculating pixel-wise variance of two images.
109 
110  This class is parametrized over the types of the two
111  input images and the type of the output image.
112  Numeric conversions (castings) are done by the C++ defaults.
113 
114  The pixel type of the input 1 image must have a valid defintion of
115  the operator+ with a pixel type of the image 2. This condition is
116  required because internally this filter will perform the operation
117 
118  <code>pixel_from_image_1 + pixel_from_image_2</code>
119 
120  Additionally the type resulting from the sum, will be cast to
121  the pixel type of the output image.
122 
123  \warning No numeric overflow checking is performed in this filter.
124 
125  \ingroup IntensityImageFilters Multithreaded
126  */
127 
128  template <class TInputImage, class TOutputImage>
129  class ITK_EXPORT NaryVarianceImageFilter :
130  public
131  NaryFunctorImageFilter<TInputImage,TOutputImage,
132  Functor::Variance<typename TInputImage::PixelType, typename TOutputImage::PixelType > >
133  {
134  public:
135  /** Standard class typedefs. */
137  typedef NaryFunctorImageFilter<TInputImage,TOutputImage,
138  Functor::Variance<typename TInputImage::PixelType,
139  typename TInputImage::PixelType > > Superclass;
140  typedef SmartPointer<Self> Pointer;
141  typedef SmartPointer<const Self> ConstPointer;
142 
143  /** Method for creation through the object factory. */
144  itkNewMacro(Self);
145 
146  /** Runtime information support. */
147  itkTypeMacro(NaryVarianceImageFilter,
148  NaryFunctorImageFilter);
149 
150  #ifdef ITK_USE_CONCEPT_CHECKING
151  /** Begin concept checking */
152  itkConceptMacro(InputConvertibleToOutputCheck,
153  (Concept::Convertible<typename TInputImage::PixelType,
154  typename TOutputImage::PixelType>));
155  itkConceptMacro(InputHasZeroCheck,
156  (Concept::HasZero<typename TInputImage::PixelType>));
157  /** End concept checking */
158  #endif
159 
160  protected:
162  virtual ~NaryVarianceImageFilter() {}
163 
164  private:
165  NaryVarianceImageFilter(const Self&); //purposely not implemented
166  void operator=(const Self&); //purposely not implemented
167 
168  };
169 
170 } // end namespace itk
171 
Helper class for calculating variance of images which interfaces with NaryVarianceImageFilter.
Definition: itkNaryVarianceImageFilter.h:32
Implements an operator for calculating pixel-wise variance of two images.
Definition: itkNaryVarianceImageFilter.h:129
NaryVarianceImageFilter Self
Standard class typedefs.
Definition: itkNaryVarianceImageFilter.h:136