ANIMA  4.0
animaAnatomicalBlockMatcher.hxx
Go to the documentation of this file.
1 #pragma once
3 
4 /* Similarity measures */
7 #include <itkImageToImageMetric.h>
8 
9 #include <itkLinearInterpolateImageFunction.h>
10 
11 namespace anima
12 {
13 
14 template <typename TInputImageType>
17 {
18  m_SimilarityType = SquaredCorrelation;
19  m_DefaultBackgroundValue = 0.0;
20 }
21 
22 template <typename TInputImageType>
23 bool
26 {
27  if (m_SimilarityType == MeanSquares)
28  return false;
29 
30  return true;
31 }
32 
33 template <typename TInputImageType>
37 {
38  MetricPointer metric;
39 
40  switch(m_SimilarityType)
41  {
42  case Correlation:
43  case SquaredCorrelation:
44  {
46 
47  typename LocalMetricType::Pointer tmpMetric = LocalMetricType::New();
48  tmpMetric->SetSquaredCorrelation(m_SimilarityType == SquaredCorrelation);
49  tmpMetric->SetDefaultBackgroundValue(m_DefaultBackgroundValue);
50 
51  metric = tmpMetric;
52  break;
53  }
54 
55  case MeanSquares:
56  default:
57  {
59  typename LocalMetricType::Pointer tmpMetric = LocalMetricType::New();
60  tmpMetric->SetDefaultBackgroundValue(m_DefaultBackgroundValue);
61 
62  metric = tmpMetric;
63  break;
64  }
65  }
66 
67  typedef itk::ImageToImageMetric <InputImageType,InputImageType> BaseMetricType;
68  BaseMetricType *baseMetric = dynamic_cast <BaseMetricType *> (metric.GetPointer());
69 
70  typedef itk::LinearInterpolateImageFunction<InputImageType,double> LocalInterpolatorType;
71  typename LocalInterpolatorType::Pointer interpolator = LocalInterpolatorType::New();
72 
73  baseMetric->SetInterpolator(interpolator);
74  baseMetric->ComputeGradientOff();
75 
76  baseMetric->SetFixedImage(this->GetReferenceImage());
77  baseMetric->SetMovingImage(this->GetMovingImage());
78  interpolator->SetInputImage(this->GetMovingImage());
79 
80  return metric;
81 }
82 
83 template <typename TInputImageType>
84 double
86 ::ComputeBlockWeight(double val, unsigned int block)
87 {
88  switch (m_SimilarityType)
89  {
90  case MeanSquares:
91  return 1;
92 
93  case Correlation:
94  return (val + 1) / 2.0;
95 
96  case SquaredCorrelation:
97  default:
98  return val;
99  }
100 }
101 
102 template <typename TInputImageType>
103 void
105 ::BlockMatchingSetup(MetricPointer &metric, unsigned int block)
106 {
107  // For transform related init
108  this->Superclass::BlockMatchingSetup(metric,block);
109 
110  // Metric specific init
111  typedef itk::ImageToImageMetric <InputImageType, InputImageType> InternalMetricType;
112  InternalMetricType *tmpMetric = dynamic_cast <InternalMetricType *> (metric.GetPointer());
113  tmpMetric->SetFixedImageRegion(this->GetBlockRegion(block));
114  tmpMetric->SetTransform(this->GetBlockTransformPointer(block));
115  tmpMetric->Initialize();
116  if (m_SimilarityType != MeanSquares)
117  ((anima::FastCorrelationImageToImageMetric<InputImageType, InputImageType> *)metric.GetPointer())->PreComputeFixedValues();
118  else
119  ((anima::FastMeanSquaresImageToImageMetric<InputImageType, InputImageType> *)metric.GetPointer())->PreComputeFixedValues();
120 }
121 
122 } // end namespace anima
Superclass::MetricPointer MetricPointer
virtual double ComputeBlockWeight(double val, unsigned int block)
virtual void BlockMatchingSetup(MetricPointer &metric, unsigned int block)