4 #include <itkImageRegionConstIteratorWithIndex.h> 9 template <
class TFixedImage,
class TMovingImage>
15 m_DefaultBackgroundValue = 0.0;
16 m_SquaredCorrelation =
true;
17 m_ScaleIntensities =
false;
18 m_FixedImagePoints.clear();
19 m_FixedImageValues.clear();
22 template <
class TFixedImage,
class TMovingImage>
30 itkExceptionMacro( <<
"Fixed image has not been assigned" );
32 if ( this->m_NumberOfPixelsCounted == 0 )
36 this->SetTransformParameters( parameters );
38 typedef typename itk::NumericTraits< MeasureType >::AccumulateType AccumulateType;
40 AccumulateType smm = itk::NumericTraits< AccumulateType >::Zero;
41 AccumulateType sfm = itk::NumericTraits< AccumulateType >::Zero;
42 AccumulateType sm = itk::NumericTraits< AccumulateType >::Zero;
48 for (
unsigned int i = 0;i < this->m_NumberOfPixelsCounted;++i)
50 transformedPoint = this->m_Transform->TransformPoint(m_FixedImagePoints[i]);
51 this->m_Interpolator->GetInputImage()->TransformPhysicalPointToContinuousIndex(transformedPoint,transformedIndex);
53 movingValue = m_DefaultBackgroundValue;
54 if (this->m_Interpolator->IsInsideBuffer(transformedIndex))
55 movingValue = this->m_Interpolator->EvaluateAtContinuousIndex(transformedIndex);
57 if (movingValue != 0.0)
59 if (m_ScaleIntensities)
61 typedef itk::MatrixOffsetTransformBase <
typename TransformType::ScalarType,
62 TFixedImage::ImageDimension, TFixedImage::ImageDimension> BaseTransformType;
63 BaseTransformType *currentTrsf =
dynamic_cast<BaseTransformType *
> (this->m_Transform.GetPointer());
65 double factor = vnl_determinant(currentTrsf->GetMatrix().GetVnlMatrix());
66 movingValue *= factor;
69 smm += movingValue * movingValue;
70 sfm += m_FixedImageValues[i] * movingValue;
75 RealType movingVariance = smm - sm * sm / this->m_NumberOfPixelsCounted;
76 if (movingVariance <= 0)
79 RealType covData = sfm - m_SumFixed * sm / this->m_NumberOfPixelsCounted;
80 RealType multVars = m_VarFixed * movingVariance;
82 if (this->m_NumberOfPixelsCounted > 1 && multVars > 0)
84 if (m_SquaredCorrelation)
85 measure = covData * covData / multVars;
87 measure = std::max(0.0,covData / sqrt(multVars));
91 measure = itk::NumericTraits< MeasureType >::Zero;
97 template <
class TFixedImage,
class TMovingImage>
106 itkExceptionMacro( <<
"Fixed image has not been assigned" );
113 typedef itk::ImageRegionConstIteratorWithIndex<FixedImageType> FixedIteratorType;
115 FixedIteratorType ti( fixedImage, this->GetFixedImageRegion() );
116 typename FixedImageType::IndexType index;
118 this->m_NumberOfPixelsCounted = this->GetFixedImageRegion().GetNumberOfPixels();
120 m_FixedImagePoints.resize(this->m_NumberOfPixelsCounted);
121 m_FixedImageValues.resize(this->m_NumberOfPixelsCounted);
125 unsigned int pos = 0;
129 index = ti.GetIndex();
130 fixedImage->TransformIndexToPhysicalPoint( index, inputPoint );
132 m_FixedImagePoints[pos] = inputPoint;
133 fixedValue = ti.Value();
134 m_FixedImageValues[pos] = fixedValue;
136 sumSquared += fixedValue * fixedValue;
137 m_SumFixed += fixedValue;
143 m_VarFixed = sumSquared - m_SumFixed * m_SumFixed / this->m_NumberOfPixelsCounted;
146 template <
class TFixedImage,
class TMovingImage>
152 itkExceptionMacro(
"Derivative not implemented yet...");
155 template <
class TFixedImage,
class TMovingImage>
161 itkExceptionMacro(
"Derivative not implemented yet...");
164 template <
class TFixedImage,
class TMovingImage>
169 Superclass::PrintSelf(os, indent);
170 os << indent << m_SumFixed <<
" " << m_VarFixed << std::endl;
Superclass::FixedImageConstPointer FixedImageConstPointer
void GetValueAndDerivative(const TransformParametersType ¶meters, MeasureType &Value, DerivativeType &Derivative) const ITK_OVERRIDE
void GetDerivative(const TransformParametersType ¶meters, DerivativeType &Derivative) const ITK_OVERRIDE
itk::ContinuousIndex< double, TFixedImage::ImageDimension > ContinuousIndexType
void PreComputeFixedValues()
Superclass::MeasureType MeasureType
void PrintSelf(std::ostream &os, itk::Indent indent) const ITK_OVERRIDE
Superclass::InputPointType InputPointType
FastCorrelationImageToImageMetric()
Superclass::OutputPointType OutputPointType
Superclass::TransformParametersType TransformParametersType
Superclass::DerivativeType DerivativeType
MeasureType GetValue(const TransformParametersType ¶meters) const ITK_OVERRIDE
Superclass::RealType RealType