17 #ifndef __itkNCCRegistrationFunction_txx
18 #define __itkNCCRegistrationFunction_txx
22 #include "vnl/vnl_math.h"
29 template <
class TFixedImage,
class TMovingImage,
class TDeformationField>
36 for( j = 0; j < ImageDimension; j++ )
44 m_DenominatorThreshold = 1e-9;
45 m_IntensityDifferenceThreshold = 0.001;
46 this->SetMovingImage(
NULL);
47 this->SetFixedImage(
NULL);
48 m_FixedImageSpacing.Fill( 1.0 );
49 m_FixedImageGradientCalculator = GradientCalculatorType::New();
52 DefaultInterpolatorType::New();
64 template <
class TFixedImage,
class TMovingImage,
class TDeformationField>
70 Superclass::PrintSelf(os, indent);
87 template <
class TFixedImage,
class TMovingImage,
class TDeformationField>
92 if( !this->m_MovingImage || !this->m_FixedImage || !m_MovingImageInterpolator )
94 itkExceptionMacro( <<
"MovingImage, FixedImage and/or Interpolator not set" );
98 m_FixedImageSpacing = this->m_FixedImage->GetSpacing();
101 m_FixedImageGradientCalculator->SetInputImage( this->m_FixedImage );
104 m_MovingImageInterpolator->SetInputImage( this->m_MovingImage );
106 std::cout <<
" total metric " << m_MetricTotal <<
" field size " <<
107 this->GetDeformationField()->GetLargestPossibleRegion().GetSize()<<
" image size " <<
108 this->m_FixedImage->GetLargestPossibleRegion().GetSize() << std::endl;
116 template <
class TFixedImage,
class TMovingImage,
class TDeformationField>
124 const typename FixedImageType::SizeType hradius=it.
GetRadius();
126 const typename FixedImageType::SizeType imagesize=img->GetLargestPossibleRegion().
GetSize();
129 hoodIt( hradius , img, img->GetRequestedRegion());
136 double derivativeF[ImageDimension];
137 double derivativeM[ImageDimension];
138 for (
unsigned int j=0; j<ImageDimension;j++)
144 unsigned int hoodlen=hoodIt.Size();
145 for(
unsigned int indct=0; indct<hoodlen-1; indct++)
147 const IndexType index=hoodIt.GetIndex(indct);
149 for (
unsigned int dd=0; dd<ImageDimension; dd++)
151 if ( index[dd] < 0 || index[dd] > static_cast<typename IndexType::IndexValueType>(imagesize[dd]-1) ) inimage=
false;
158 const double fixedValue = (double) this->m_FixedImage->GetPixel( index );
159 const CovariantVectorType fixedGradient = m_FixedImageGradientCalculator->EvaluateAtIndex( index );
160 double fixedGradientSquaredMagnitude = 0;
161 for(
unsigned int j = 0; j < ImageDimension; j++ )
163 fixedGradientSquaredMagnitude += vnl_math_sqr( fixedGradient[j] ) * m_FixedImageSpacing[j];
167 typedef typename TDeformationField::PixelType DeformationPixelType;
168 const DeformationPixelType vec = this->GetDeformationField()->GetPixel(index);
170 this->GetFixedImage()->TransformIndexToPhysicalPoint(index, mappedPoint);
171 for(
unsigned int j = 0; j < ImageDimension; j++ )
173 mappedPoint[j] += vec[j];
175 double movingValue=0.0;
176 if( m_MovingImageInterpolator->IsInsideBuffer( mappedPoint ) )
178 movingValue = m_MovingImageInterpolator->Evaluate( mappedPoint );
181 sff += fixedValue*fixedValue;
182 smm += movingValue*movingValue;
183 sfm += fixedValue*movingValue;
185 for(
unsigned int dim=0; dim<ImageDimension; dim++)
187 const double differential = fixedGradient[dim];
188 derivativeF[dim] += fixedValue * differential;
189 derivativeM[dim] += movingValue * differential;
196 double updatenorm=0.0;
197 if( (sff*smm) != 0.0)
199 const double factor = 1.0 / vcl_sqrt(sff * smm );
200 for(
unsigned int i=0; i<ImageDimension; i++)
202 update[i] = factor * ( derivativeF[i] - (sfm/smm)*derivativeM[i]);
203 updatenorm += (update[i]*update[i]);
205 updatenorm=vcl_sqrt(updatenorm);
206 m_MetricTotal += sfm*factor;
207 this->m_Energy += sfm*factor;
215 if (this->GetNormalizeGradient() && updatenorm != 0.0 )
217 update /= (updatenorm);
219 return update * this->m_GradientStep;