ANIMA  4.0
animaFastMeanSquaresImageToImageMetric.hxx
Go to the documentation of this file.
1 #pragma once
3 
4 #include <itkImageRegionConstIteratorWithIndex.h>
5 
6 namespace anima
7 {
8 
9 template <class TFixedImage, class TMovingImage>
12 {
13  m_ScaleIntensities = false;
14  m_DefaultBackgroundValue = 0.0;
15 }
16 
17 template <class TFixedImage, class TMovingImage>
20 ::GetValue( const TransformParametersType & parameters ) const
21 {
22  FixedImageConstPointer fixedImage = this->m_FixedImage;
23 
24  if( !fixedImage )
25  {
26  itkExceptionMacro( << "Fixed image has not been assigned" );
27  }
28 
29  if (this->m_NumberOfPixelsCounted == 0)
30  return 0;
31 
32  MeasureType measure = 0;
33  this->SetTransformParameters( parameters );
34 
35  OutputPointType transformedPoint;
36  ContinuousIndexType transformedIndex;
37  RealType movingValue;
38 
39  for (unsigned int i = 0;i < this->m_NumberOfPixelsCounted;++i)
40  {
41  transformedPoint = this->m_Transform->TransformPoint( m_FixedImagePoints[i] );
42  this->m_Interpolator->GetInputImage()->TransformPhysicalPointToContinuousIndex(transformedPoint,transformedIndex);
43 
44  movingValue = m_DefaultBackgroundValue;
45 
46  if( this->m_Interpolator->IsInsideBuffer( transformedIndex ) )
47  {
48  movingValue = this->m_Interpolator->EvaluateAtContinuousIndex( transformedIndex );
49 
50  if (m_ScaleIntensities)
51  {
52  typedef itk::MatrixOffsetTransformBase <typename TransformType::ScalarType,
53  TFixedImage::ImageDimension, TFixedImage::ImageDimension> BaseTransformType;
54  BaseTransformType *currentTrsf = dynamic_cast<BaseTransformType *> (this->m_Transform.GetPointer());
55 
56  double factor = vnl_determinant(currentTrsf->GetMatrix().GetVnlMatrix());
57  movingValue *= factor;
58  }
59  }
60 
61  measure += (movingValue - m_FixedImageValues[i]) * (movingValue - m_FixedImageValues[i]);
62  }
63 
64  measure /= this->m_NumberOfPixelsCounted;
65 
66  return measure;
67 }
68 
69 template < class TFixedImage, class TMovingImage>
70 void
73  DerivativeType & derivative ) const
74 {
75  itkExceptionMacro("Derivative not implemented yet...");
76 }
77 
78 template <class TFixedImage, class TMovingImage>
79 void
82  MeasureType & value, DerivativeType & derivative) const
83 {
84  itkExceptionMacro("Derivative not implemented yet...");
85 }
86 
87 template <class TFixedImage, class TMovingImage>
88 void
91 {
92  FixedImageConstPointer fixedImage = this->m_FixedImage;
93 
94  if( !fixedImage )
95  {
96  itkExceptionMacro( << "Fixed image has not been assigned" );
97  }
98 
99  typedef itk::ImageRegionConstIteratorWithIndex<FixedImageType> FixedIteratorType;
100 
101  FixedIteratorType ti( fixedImage, this->GetFixedImageRegion() );
102  typename FixedImageType::IndexType index;
103 
104  this->m_NumberOfPixelsCounted = this->GetFixedImageRegion().GetSize()[0];
105  for (unsigned int i = 1;i < TFixedImage::GetImageDimension();++i)
106  this->m_NumberOfPixelsCounted *= this->GetFixedImageRegion().GetSize()[i];
107 
108  m_FixedImagePoints.resize(this->m_NumberOfPixelsCounted);
109  m_FixedImageValues.resize(this->m_NumberOfPixelsCounted);
110 
111  InputPointType inputPoint;
112 
113  unsigned int pos = 0;
114  RealType fixedValue;
115 
116  while(!ti.IsAtEnd())
117  {
118  index = ti.GetIndex();
119  fixedImage->TransformIndexToPhysicalPoint( index, inputPoint );
120 
121  m_FixedImagePoints[pos] = inputPoint;
122  fixedValue = ti.Value();
123  m_FixedImageValues[pos] = fixedValue;
124 
125  ++ti;
126  ++pos;
127  }
128 }
129 
130 } // end namespace anima
void GetDerivative(const TransformParametersType &parameters, DerivativeType &Derivative) const ITK_OVERRIDE
MeasureType GetValue(const TransformParametersType &parameters) const ITK_OVERRIDE
void GetValueAndDerivative(const TransformParametersType &parameters, MeasureType &Value, DerivativeType &Derivative) const ITK_OVERRIDE
itk::ContinuousIndex< double, TFixedImage::ImageDimension > ContinuousIndexType