CBICA Toolkit  1.0.0
itkNaryMeanDiffusionTensorImageFilter.h
Go to the documentation of this file.
1 /**
2 \file itkNaryMeanDiffusionTensorImageFilter.h
3 
4 \brief Declaration & Implementation of the NaryMeanDiffusionTensorImageFilter 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"
19 
20 namespace itk
21 {
22 
23  namespace Functor
24  {
25  /**
26  \class DiffusionTensorMean
27 
28  \brief Helper class to compute the Tensor mean which interfaces with NaryMeanDiffusionTensorImageFilter
29  */
30  template< class TInput, class TOutput >
32  {
33  public:
34 
35  // Enum to decide which Metric To use compute the means
36  enum { EUCLIDEAN, LOGEUCLIDEAN };
37  typedef typename NumericTraits< TInput >::RealType DtiCompType;
38  typedef typename NumericTraits< TOutput >::RealType SymTensorCompType;
39 
41 
42  typedef typename DTICalculatorType::DTType TensorType;
43  typedef typename DTICalculatorType::SymMatType SymMatType;
44 
46  {
47  m_MetricType = LOGEUCLIDEAN;
48  dtiCalc = DTICalculatorType::New();
49  }
50 
51  virtual ~DiffusionTensorMean() {}
52  inline TOutput operator()( const std::vector< TInput > & B)
53  {
54  TensorType meanVal = NumericTraits< TensorType >::Zero;
55 
56  switch (m_MetricType)
57  {
58  case LOGEUCLIDEAN :
59  {
60 
61  int stop = 0;
62 
63  // Check for any zero inputs and return a zero tensor if any are found.
64  for( unsigned int i=0; i< B.size(); i++ )
65  {
66  if ( !dtiCalc->IsPositiveDefinite(static_cast<TensorType>(B[i])) )
67  {
68  stop = 1;
69  break;
70  }
71  }
72 
73  if (stop) { break; }
74 
75  SymMatType sum = NumericTraits< SymMatType >::Zero;
76 
77  for( unsigned int i=0; i< B.size(); i++ )
78  {
79  sum += dtiCalc->CalculateMatrixLog(static_cast<TensorType>(B[i]));
80  }
81  meanVal = dtiCalc->CalculateMatrixExp(sum / B.size());
82  break;
83  }
84  case EUCLIDEAN :
85  {
86  TensorType sum = NumericTraits< TensorType >::Zero;
87  for( unsigned int i=0; i< B.size(); i++ )
88  {
89  sum += static_cast<TensorType>(B[i]);
90  }
91  meanVal = (sum / B.size());
92  break;
93  }
94  }
95 
96  return static_cast<TOutput>( meanVal.GetInnerScalarProduct() );
97  }
98 
99  bool operator== (const DiffusionTensorMean&) const
100  {
101  return true;
102  }
103  bool operator!= (const DiffusionTensorMean&) const
104  {
105  return false;
106  }
107 
108  virtual void SetMetricType (int _arg)
109  {
110  if (this->m_MetricType !=
111  (bool)(_arg<EUCLIDEAN ? EUCLIDEAN : (_arg>LOGEUCLIDEAN ? LOGEUCLIDEAN : _arg)))
112  {
113  this->m_MetricType =
114  (bool)(_arg<EUCLIDEAN ? EUCLIDEAN : (_arg>LOGEUCLIDEAN ? LOGEUCLIDEAN : _arg));
115  }
116  }
117 
118  private:
119  bool m_MetricType; // Boolean to use LogEuc or Euclidean
120  typename DTICalculatorType::Pointer dtiCalc;
121 
122  };
123  }
124 
125  /**
126  \class NaryMeanDiffusionTensorImageFilter
127 
128  \brief Implements an operator for pixel-wise averaging of two
129  Diffusion Tensor images.
130 
131  This class is parametrized over the types of the two
132  input images and the type of the output image.
133  Numeric conversions (castings) are done by the C++ defaults.
134 
135  The pixel type of the input 1 image must have a valid defintion of
136  the operator+ with a pixel type of the image 2. This condition is
137  required because internally this filter will perform the operation
138 
139  <code>pixel_from_image_1 + pixel_from_image_2</code>
140 
141  Additionally the type resulting from the sum, will be cast to
142  the pixel type of the output image.
143 
144  \warning No numeric overflow checking is performed in this filter.
145 
146  \todo We should implement a mask to define the region of interest.
147 
148  \ingroup IntensityImageFilters Multithreaded
149  */
150 
151  template <class TInputImage, class TOutputImage>
153  public
154  NaryFunctorImageFilter<TInputImage,TOutputImage,
155  Functor::DiffusionTensorMean<typename TInputImage::PixelType,
156  typename TOutputImage::PixelType > >
157  {
158  public:
159 
160  //! Enum to decide which Metric To use compute the means
161  enum { EUCLIDEAN, LOGEUCLIDEAN };
162 
163  //! Standard class typedefs.
165  typedef NaryFunctorImageFilter<TInputImage,TOutputImage,
166  Functor::DiffusionTensorMean<typename TInputImage::PixelType,
167  typename TInputImage::PixelType > > Superclass;
168  typedef SmartPointer<Self> Pointer;
169  typedef SmartPointer<const Self> ConstPointer;
170 
171  //! Method for creation through the object factory.
172  itkNewMacro(Self);
173 
174  //! Runtime information support.
176  NaryFunctorImageFilter);
177 
178 
179  #ifdef ITK_USE_CONCEPT_CHECKING
180  //! Begin concept checking
181  itkConceptMacro(InputConvertibleToOutputCheck,
182  (Concept::Convertible<typename TInputImage::PixelType,
183  typename TOutputImage::PixelType>));
184  itkConceptMacro(InputHasZeroCheck,
185  (Concept::HasZero<typename TInputImage::PixelType>));
186  //! End concept checking
187  #endif
188 
189  virtual void SetMetricType (int _arg)
190  { this->GetFunctor().SetMetricType(_arg);}
191 
192  protected:
194  virtual ~NaryMeanDiffusionTensorImageFilter() {}
195 
196  private:
197  NaryMeanDiffusionTensorImageFilter(const Self&); // purposely not implemented
198  void operator=(const Self&); // purposely not implemented
199 
200  };
201 
202 } // end namespace itk
The Purpose of this class is to provide common computations for diffusion tensors specifically.
Definition: itkDTILogEuclideanCalculator.h:37
NaryMeanDiffusionTensorImageFilter Self
Standard class typedefs.
Definition: itkNaryMeanDiffusionTensorImageFilter.h:164
Helper class to compute the Tensor mean which interfaces with NaryMeanDiffusionTensorImageFilter.
Definition: itkNaryMeanDiffusionTensorImageFilter.h:31
DiffusionTensor3D< TDtiCompType > DTType
Typedefs for methods.
Definition: itkDTILogEuclideanCalculator.h:55
Declaration of the DTILogEuclideanCalculator class.
Implements an operator for pixel-wise averaging of two Diffusion Tensor images.
Definition: itkNaryMeanDiffusionTensorImageFilter.h:152