/
itkNormalizedGradientCorrelationImageToImageMetric.txx
executable file
·247 lines (207 loc) · 7.96 KB
/
itkNormalizedGradientCorrelationImageToImageMetric.txx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#ifndef __itkNormalizedGradientCorrelationImageToImageMetric_txx
#define __itkNormalizedGradientCorrelationImageToImageMetric_txx
#include "itkNormalizedGradientCorrelationImageToImageMetric.h"
#include <itkImageRegionConstIteratorWithIndex.h>
#include <itkNumericTraits.h>
namespace itk
{
/**
* Constructor
*/
template <class TFixedImage, class TMovingImage>
NormalizedGradientCorrelationImageToImageMetric<TFixedImage,TMovingImage>
::NormalizedGradientCorrelationImageToImageMetric()
{
m_DerivativeDelta = 0.001;
m_MaxDimension = FixedImageDimension;
}
/**
* Initialize
*/
template <class TFixedImage, class TMovingImage>
void
NormalizedGradientCorrelationImageToImageMetric<TFixedImage,TMovingImage>
::Initialize(void) throw ( ExceptionObject )
{
// Initialise the base class
Superclass::Initialize();
SizeType size = this->GetFixedImageRegion().GetSize();
for( unsigned int dim=0; dim < FixedImageDimension; dim++ )
{
if( size[dim] < 2 )
{
m_MaxDimension = dim;
break;
}
}
for (unsigned int dim=0; dim<m_MaxDimension; dim++)
{
m_SobelOperators[dim].SetRadius( 1 );
m_SobelOperators[dim].SetDirection( dim );
m_SobelOperators[dim].CreateDirectional();
m_FixedSobelFilters[dim] = SobelFilterType::New();
m_FixedSobelFilters[dim]->OverrideBoundaryCondition(
&m_FixedBoundaryCondition );
m_FixedSobelFilters[dim]->SetOperator( m_SobelOperators[dim] );
m_FixedSobelFilters[dim]->SetInput( this->GetFixedImage() );
m_FixedSobelFilters[dim]->GetOutput()->SetRequestedRegion(
this->GetFixedImageRegion() );
}
m_ResampleImageFilter = ResampleImageFilterType::New();
m_ResampleImageFilter->SetTransform( this->m_Transform );
m_ResampleImageFilter->SetInterpolator( this->m_Interpolator );
m_ResampleImageFilter->SetInput( this->m_MovingImage );
m_ResampleImageFilter->SetDefaultPixelValue(
itk::NumericTraits<FixedImagePixelType>::Zero );
m_ResampleImageFilter->UseReferenceImageOn();
m_ResampleImageFilter->SetReferenceImage( this->m_FixedImage );
m_ResampleImageFilter->GetOutput()->SetRequestedRegion(
this->GetFixedImageRegion() );
m_ResampleImageFilter->Update();
for (unsigned int dim=0; dim < m_MaxDimension; dim++)
{
m_MovingSobelFilters[dim] = SobelFilterType::New();
m_MovingSobelFilters[dim]->OverrideBoundaryCondition(
&m_MovingBoundaryCondition );
m_MovingSobelFilters[dim]->SetOperator( m_SobelOperators[dim] );
m_MovingSobelFilters[dim]->SetInput(m_ResampleImageFilter->GetOutput());
m_MovingSobelFilters[dim]->GetOutput()->SetRequestedRegion(
this->GetFixedImageRegion() );
}
}
/**
* PrintSelf
*/
template <class TFixedImage, class TMovingImage>
void
NormalizedGradientCorrelationImageToImageMetric<TFixedImage,TMovingImage>
::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf( os, indent );
os << indent << "DerivativeDelta: " << this->m_DerivativeDelta << std::endl;
}
/**
* Get the value of the similarity measure
*/
template <class TFixedImage, class TMovingImage>
typename NormalizedGradientCorrelationImageToImageMetric<TFixedImage,TMovingImage>::MeasureType
NormalizedGradientCorrelationImageToImageMetric<TFixedImage,TMovingImage>
::GetValue( const TransformParametersType & parameters ) const
{
this->m_NumberOfPixelsCounted = 0;
this->SetTransformParameters( parameters );
for (unsigned int dim=0; dim<m_MaxDimension; dim++)
{
this->m_FixedSobelFilters[dim]->Update();
this->m_MovingSobelFilters[dim]->Update();
}
MeasureType val = NumericTraits< MeasureType >::Zero;
/*
cc: cross corrrelation
fac: fixed auto correlation, this is, auto correlation of the fixed image
mac: moving auto correlation, this is, moving image auto correlation
*/
MeasureType cc[FixedImageDimension];
MeasureType fac[FixedImageDimension];
MeasureType mac[FixedImageDimension];
for( unsigned int dim=0; dim < m_MaxDimension; dim++ )
{
cc[dim] = NumericTraits< MeasureType >::Zero;
fac[dim] = NumericTraits< MeasureType >::Zero;
mac[dim] = NumericTraits< MeasureType >::Zero;
}
RealType movingGradient[FixedImageDimension];
RealType fixedGradient[FixedImageDimension];
FixedImageConstIteratorType iter( this->m_FixedImage,
this->GetFixedImageRegion() );
for( iter.GoToBegin(); !iter.IsAtEnd(); ++iter )
{
typename FixedImageType::IndexType fixedIndex = iter.GetIndex();
//Check if point is inside the fixed image mask
InputPointType inputPoint;
this->GetFixedImage()->TransformIndexToPhysicalPoint( fixedIndex, inputPoint );
if( this->m_FixedImageMask && !this->m_FixedImageMask->IsInside( inputPoint ) )
{
continue;
}
for( unsigned int dim=0; dim<m_MaxDimension; dim++ )
{
fixedGradient[dim] = m_FixedSobelFilters[dim]->GetOutput()->GetPixel(
fixedIndex );
movingGradient[dim] = m_MovingSobelFilters[dim]->GetOutput()->GetPixel(
fixedIndex );
cc[dim] += movingGradient[dim] * fixedGradient[dim];
fac[dim] += fixedGradient[dim] * fixedGradient[dim];
mac[dim] += movingGradient[dim] * movingGradient[dim];
}
this->m_NumberOfPixelsCounted++;
}
if( this->m_NumberOfPixelsCounted == 0 )
{
itkExceptionMacro(<< "No voxels counted for metric calculation");
}
for( unsigned int dim=0; dim < m_MaxDimension; dim++ )
{
if( fac[dim] == NumericTraits< MeasureType >::Zero ||
mac[dim] == NumericTraits< MeasureType >::Zero )
{
itkExceptionMacro(<< "Auto correlation(s) equal to zero");
}
}
for( unsigned int dim=0; dim < m_MaxDimension; dim++ )
{
val += cc[dim] / vcl_sqrt( fac[dim] * mac[dim] ) / m_MaxDimension;
}
return val;
}
/**
* Get the Derivative Measure
*/
template < class TFixedImage, class TMovingImage>
void
NormalizedGradientCorrelationImageToImageMetric<TFixedImage,TMovingImage>
::GetDerivative( const TransformParametersType & parameters,
DerivativeType & derivative ) const
{
TransformParametersType testPoint;
testPoint = parameters;
const unsigned int numberOfParameters = this->GetNumberOfParameters();
derivative = DerivativeType( numberOfParameters );
for( unsigned int i=0; i<numberOfParameters; i++)
{
testPoint[i] -= this->m_DerivativeDelta;
const MeasureType valuep0 = this->GetValue( testPoint );
testPoint[i] += 2* this->m_DerivativeDelta;
const MeasureType valuep1 = this->GetValue( testPoint );
derivative[i] = (valuep1 - valuep0 ) / ( 2 * this->m_DerivativeDelta );
testPoint[i] = parameters[i];
}
}
/**
* Get both the match Measure and theDerivative Measure
*/
template <class TFixedImage, class TMovingImage>
void
NormalizedGradientCorrelationImageToImageMetric<TFixedImage,TMovingImage>
::GetValueAndDerivative(const TransformParametersType & parameters,
MeasureType & Value, DerivativeType & Derivative) const
{
Value = this->GetValue( parameters );
this->GetDerivative( parameters, Derivative );
}
/**
* Set the parameters that define a unique transform
*/
template <class TFixedImage, class TMovingImage>
void
NormalizedGradientCorrelationImageToImageMetric<TFixedImage,TMovingImage>
::SetTransformParameters( const TransformParametersType & parameters ) const
{
if( !this->m_Transform )
{
itkExceptionMacro(<<"Transform has not been assigned");
}
this->m_Transform->SetParameters( parameters );
}
} // end namespace itk
#endif