-
Notifications
You must be signed in to change notification settings - Fork 27
/
itkNormalizedCorrelationTwoImageToOneImageMetric.txx
executable file
·266 lines (200 loc) · 7.51 KB
/
itkNormalizedCorrelationTwoImageToOneImageMetric.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkNormalizedCorrelationTwoImageToOneImageMetric.txx,v $
Language: C++
Date: $Date: 2010/12/20 $
Version: $Revision: 1.0 $
Author: Jian Wu (eewujian@hotmail.com)
Univerisity of Florida
Virginia Commonwealth University
This program was modified from the ITK program--itkNormalizedCorrelationImageToImageMetric.txx
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef _itkNormalizedCorrelationTwoImageToOneImageMetric_txx
#define _itkNormalizedCorrelationTwoImageToOneImageMetric_txx
#include "itkNormalizedCorrelationTwoImageToOneImageMetric.h"
#include "itkImageRegionConstIteratorWithIndex.h"
namespace itk
{
/*
* Constructor
*/
template <class TFixedImage, class TMovingImage>
NormalizedCorrelationTwoImageToOneImageMetric<TFixedImage,TMovingImage>
::NormalizedCorrelationTwoImageToOneImageMetric()
{
m_SubtractMean = false;
}
/*
* Get the match Measure
*/
template <class TFixedImage, class TMovingImage>
typename NormalizedCorrelationTwoImageToOneImageMetric<TFixedImage,TMovingImage>::MeasureType
NormalizedCorrelationTwoImageToOneImageMetric<TFixedImage,TMovingImage>
::GetValue( const TransformParametersType & parameters ) const
{
FixedImageConstPointer fixedImage1 = this->m_FixedImage1;
if( !fixedImage1 )
{
itkExceptionMacro( << "Fixed image1 has not been assigned" );
}
FixedImageConstPointer fixedImage2 = this->m_FixedImage2;
if( !fixedImage2 )
{
itkExceptionMacro( << "Fixed image2 has not been assigned" );
}
typedef itk::ImageRegionConstIteratorWithIndex<FixedImageType> FixedIteratorType;
typedef typename NumericTraits< MeasureType >::AccumulateType AccumulateType;
// Calculate the measure value between fixed image 1 and the moving image
FixedIteratorType ti1( fixedImage1, this->GetFixedImageRegion1() );
typename FixedImageType::IndexType index;
MeasureType measure1;
this->m_NumberOfPixelsCounted = 0;
this->SetTransformParameters( parameters );
AccumulateType sff = NumericTraits< AccumulateType >::Zero;
AccumulateType smm = NumericTraits< AccumulateType >::Zero;
AccumulateType sfm = NumericTraits< AccumulateType >::Zero;
AccumulateType sf = NumericTraits< AccumulateType >::Zero;
AccumulateType sm = NumericTraits< AccumulateType >::Zero;
typename Superclass::InputPointType inputPoint;
while(!ti1.IsAtEnd())
{
index = ti1.GetIndex();
fixedImage1->TransformIndexToPhysicalPoint( index, inputPoint );
if( this->m_FixedImageMask1 && !this->m_FixedImageMask1->IsInside( inputPoint ) )
{
++ti1;
continue;
}
// typename Superclass::OutputPointType transformedPoint = this->m_Transform->TransformPoint( inputPoint );
if( this->m_MovingImageMask && !this->m_MovingImageMask->IsInside( inputPoint ) )
{
++ti1;
continue;
}
if( this->m_Interpolator1->IsInsideBuffer( inputPoint ) )
{
const RealType movingValue = this->m_Interpolator1->Evaluate( inputPoint );
const RealType fixedValue = ti1.Get();
sff += fixedValue * fixedValue;
smm += movingValue * movingValue;
sfm += fixedValue * movingValue;
if ( this->m_SubtractMean )
{
sf += fixedValue;
sm += movingValue;
}
this->m_NumberOfPixelsCounted++;
}
++ti1;
}
if ( this->m_SubtractMean && this->m_NumberOfPixelsCounted > 0 )
{
sff -= ( sf * sf / this->m_NumberOfPixelsCounted );
smm -= ( sm * sm / this->m_NumberOfPixelsCounted );
sfm -= ( sf * sm / this->m_NumberOfPixelsCounted );
}
RealType denom = -1.0 * sqrt( sff * smm );
if( this->m_NumberOfPixelsCounted > 0 && denom != 0.0)
{
measure1 = sfm / denom;
}
else
{
measure1 = NumericTraits< MeasureType >::Zero;
}
// Calculate the measure value between fixed image 2 and the moving image
FixedIteratorType ti2( fixedImage2, this->GetFixedImageRegion2() );
MeasureType measure2;
this->m_NumberOfPixelsCounted = 0;
this->SetTransformParameters( parameters );
sff = NumericTraits< AccumulateType >::Zero;
smm = NumericTraits< AccumulateType >::Zero;
sfm = NumericTraits< AccumulateType >::Zero;
sf = NumericTraits< AccumulateType >::Zero;
sm = NumericTraits< AccumulateType >::Zero;
while(!ti2.IsAtEnd())
{
index = ti2.GetIndex();
// typename Superclass::InputPointType inputPoint;
fixedImage2->TransformIndexToPhysicalPoint( index, inputPoint );
if( this->m_FixedImageMask2 && !this->m_FixedImageMask2->IsInside( inputPoint ) )
{
++ti2;
continue;
}
// typename Superclass::OutputPointType transformedPoint = this->m_Transform->TransformPoint( inputPoint );
if( this->m_MovingImageMask && !this->m_MovingImageMask->IsInside( inputPoint ) )
{
++ti2;
continue;
}
if( this->m_Interpolator2->IsInsideBuffer( inputPoint ) )
{
const RealType movingValue = this->m_Interpolator2->Evaluate( inputPoint );
const RealType fixedValue = ti2.Get();
sff += fixedValue * fixedValue;
smm += movingValue * movingValue;
sfm += fixedValue * movingValue;
if ( this->m_SubtractMean )
{
sf += fixedValue;
sm += movingValue;
}
this->m_NumberOfPixelsCounted++;
}
++ti2;
}
if ( this->m_SubtractMean && this->m_NumberOfPixelsCounted > 0 )
{
sff -= ( sf * sf / this->m_NumberOfPixelsCounted );
smm -= ( sm * sm / this->m_NumberOfPixelsCounted );
sfm -= ( sf * sm / this->m_NumberOfPixelsCounted );
}
denom = -1.0 * sqrt( sff * smm );
if( this->m_NumberOfPixelsCounted > 0 && denom != 0.0)
{
measure2 = sfm / denom;
}
else
{
measure2 = NumericTraits< MeasureType >::Zero;
}
return (measure1 + measure2)/2.0;
}
/*
* Get the Derivative Measure
*/
template < class TFixedImage, class TMovingImage>
void
NormalizedCorrelationTwoImageToOneImageMetric<TFixedImage,TMovingImage>
::GetDerivative( const TransformParametersType & parameters,
DerivativeType & derivative ) const
{
// under construction
}
/*
* Get both the match Measure and theDerivative Measure
*/
template <class TFixedImage, class TMovingImage>
void
NormalizedCorrelationTwoImageToOneImageMetric<TFixedImage,TMovingImage>
::GetValueAndDerivative(const TransformParametersType & parameters,
MeasureType & value, DerivativeType & derivative) const
{
// under construction
}
template < class TFixedImage, class TMovingImage>
void
NormalizedCorrelationTwoImageToOneImageMetric<TFixedImage,TMovingImage>
::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "SubtractMean: " << m_SubtractMean << std::endl;
}
} // end namespace itk
#endif