-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathitkAvantsPDEDeformableRegistrationFunction.h
359 lines (314 loc) · 11.2 KB
/
itkAvantsPDEDeformableRegistrationFunction.h
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*=========================================================================
Program: Advanced Normalization Tools
Module: $RCSfile: itkAvantsPDEDeformableRegistrationFunction.h,v $
Language: C++
Date: $Date: 2008/11/15 23:46:06 $
Version: $Revision: 1.18 $
Copyright (c) ConsortiumOfANTS. All rights reserved.
See accompanying COPYING.txt or
http://sourceforge.net/projects/advants/files/ANTS/ANTSCopyright.txt 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 _itkAvantsPDEDeformableRegistrationFunction_h_
#define _itkAvantsPDEDeformableRegistrationFunction_h_
#include "itkPDEDeformableRegistrationFunction.h"
#include "itkImageRegionIteratorWithIndex.h"
#include "itkPointSet.h"
namespace itk
{
/** \class AvantsPDEDeformableRegistrationFunction
*
* This is an abstract base class for all PDE functions which drives a
* deformable registration algorithm. It is used by
* PDEDeformationRegistrationFilter subclasses to compute the
* output deformation field which will map a moving image onto
* a fixed image.
*
* This class is templated over the fixed image type, moving image type
* and the deformation field type.
*
* \sa AvantsPDEDeformableRegistrationFilter
* \ingroup PDEDeformableRegistrationFunctions
*/
template <class TFixedImage, class TMovingImage, class TDisplacementField>
class ITK_EXPORT AvantsPDEDeformableRegistrationFunction :
public PDEDeformableRegistrationFunction<TFixedImage, TMovingImage, TDisplacementField>
{
public:
/** Standard class typedefs. */
typedef AvantsPDEDeformableRegistrationFunction Self;
typedef PDEDeformableRegistrationFunction<TFixedImage, TMovingImage, TDisplacementField> Superclass;
typedef SmartPointer<Self> Pointer;
typedef SmartPointer<const Self> ConstPointer;
/** Run-time type information (and related methods) */
itkTypeMacro( AvantsPDEDeformableRegistrationFunction,
PDEDeformableRegistrationFunction );
/** MovingImage image type. */
typedef TMovingImage MovingImageType;
typedef typename MovingImageType::ConstPointer MovingImagePointer;
typedef typename TMovingImage::IndexType IndexType;
enum { ImageDimension = MovingImageType::ImageDimension };
/** FixedImage image type. */
typedef TFixedImage FixedImageType;
typedef typename FixedImageType::ConstPointer FixedImagePointer;
typedef typename Superclass::NeighborhoodType NeighborhoodType;
typedef typename Superclass::FloatOffsetType FloatOffsetType;
/** Deformation field type. */
typedef TDisplacementField DisplacementFieldType;
typedef typename DisplacementFieldType::Pointer
DisplacementFieldTypePointer;
typedef typename TDisplacementField::PixelType VectorType;
/** PointSet Types */
typedef itk::PointSet<long, ImageDimension> PointSetType;
typedef typename PointSetType::Pointer PointSetPointer;
typedef typename PointSetType::PointType PointType;
typedef typename PointSetType::PixelType PointDataType;
typedef Image<float, ImageDimension> MetricImageType;
typedef typename Image<float, ImageDimension>::Pointer MetricImagePointer;
/** Set the moving image. */
void SetMovingImage( const MovingImageType * ptr )
{
Superclass::m_MovingImage = ptr;
}
/** Get the moving image. */
MovingImageType * GetMovingImage(void)
{
return const_cast<MovingImageType *>(Superclass::m_MovingImage.GetPointer() );
}
/** Set the fixed image. */
void SetFixedImage( const FixedImageType * ptr )
{
Superclass::m_FixedImage = ptr;
}
/** Get the fixed image. */
FixedImageType * GetFixedImage(void)
{
return const_cast<FixedImageType *>(Superclass::m_FixedImage.GetPointer() );
}
/** Set the fixed image. */
void SetDisplacementField( DisplacementFieldTypePointer ptr )
{
Superclass::m_DisplacementField = ptr;
}
/** Get the fixed image. */
DisplacementFieldTypePointer GetDisplacementField(void)
{
return Superclass::m_DisplacementField;
}
void SetEnergy( double /* e */)
{
this->m_Energy = 0.0;
}
double GetEnergy()
{
return this->m_Energy;
}
void SetGradientStep( double e)
{
this->m_GradientStep = e;
}
void SetMaxAllowedStep( double e)
{
this->m_MaxAllowedStep = e;
}
double GetGradientStep()
{
return this->m_GradientStep;
}
void SetNormalizeGradient( bool e)
{
this->m_NormalizeGradient = e;
}
bool GetNormalizeGradient()
{
return this->m_NormalizeGradient;
}
/*
* allows one to compute the metric everywhere
*/
void ComputeMetricImage()
{
bool makenewimage = false;
typedef ImageRegionIteratorWithIndex<MetricImageType> ittype;
FixedImageType* img = const_cast<FixedImageType *>(this->m_FixedImage.GetPointer() );
typename FixedImageType::SizeType imagesize = img->GetLargestPossibleRegion().GetSize();
if( !m_MetricImage )
{
makenewimage = true;
}
else if( imagesize[0] != m_MetricImage->GetLargestPossibleRegion().GetSize()[0] )
{
makenewimage = true;
}
if( makenewimage )
{
m_MetricImage = MetricImageType::New();
m_MetricImage->SetLargestPossibleRegion(img->GetLargestPossibleRegion() );
m_MetricImage->SetBufferedRegion(img->GetLargestPossibleRegion() );
m_MetricImage->SetSpacing(img->GetSpacing() );
m_MetricImage->SetOrigin(img->GetOrigin() );
m_MetricImage->Allocate();
ittype it(m_MetricImage, m_MetricImage->GetLargestPossibleRegion().GetSize() );
for( it.GoToBegin(); !it.IsAtEnd(); ++it )
{
it.Set(0);
}
}
return;
}
virtual double ComputeMetricAtPair(IndexType /* fixedindex */,
typename TDisplacementField::PixelType /* vec */)
{
return 0.0;
}
virtual VectorType ComputeUpdateInv(const NeighborhoodType & neighborhood,
void * /* globalData */,
const FloatOffsetType & /* offset */ = FloatOffsetType(0.0) )
{
bool m_Use1SidedDiff = false;
VectorType update;
update.Fill(0.0);
typename FixedImageType::SpacingType spacing = this->GetFixedImage()->GetSpacing();
IndexType oindex = neighborhood.GetIndex();
typename TDisplacementField::PixelType vec = this->m_DisplacementField->GetPixel(oindex);
float loce = 0.0;
double nccp1, nccm1;
if( m_Use1SidedDiff )
{
nccp1 = this->ComputeMetricAtPair(oindex, vec);
}
for( int imd = 0; imd < ImageDimension; imd++ )
{
typename TDisplacementField::PixelType fdvec1 = this->m_DisplacementField->GetPixel(oindex);
typename TDisplacementField::PixelType fdvec2 = this->m_DisplacementField->GetPixel(oindex);
float step = 0.1 * spacing[imd];
fdvec1[imd] = vec[imd] + step;
if( !m_Use1SidedDiff )
{
nccp1 = this->ComputeMetricAtPair(oindex, fdvec1);
}
fdvec2[imd] = vec[imd] - step;
nccm1 = this->ComputeMetricAtPair(oindex, fdvec2);
update[imd] = nccp1 - nccm1;
loce += (nccp1 + nccm1);
}
loce /= (2.0 * (float)ImageDimension); // this->ComputeMetricAtPair(oindex,vec);
this->m_Energy += loce;
if( m_MetricImage )
{
m_MetricImage->SetPixel(oindex, loce);
}
return update * this->m_GradientStep;
}
virtual VectorType ComputeUpdate(const NeighborhoodType & neighborhood,
void * /* globalData */,
const FloatOffsetType & /* offset */ = FloatOffsetType(0.0) )
{
bool m_Use1SidedDiff = false;
VectorType update;
update.Fill(0.0);
typename FixedImageType::SpacingType spacing = this->GetFixedImage()->GetSpacing();
IndexType oindex = neighborhood.GetIndex();
typename TDisplacementField::PixelType vec = this->m_DisplacementField->GetPixel(oindex);
float loce = 0.0;
double nccp1, nccm1;
if( m_Use1SidedDiff )
{
nccp1 = this->ComputeMetricAtPair(oindex, vec);
}
for( int imd = 0; imd < ImageDimension; imd++ )
{
typename TDisplacementField::PixelType fdvec1 = this->m_DisplacementField->GetPixel(oindex);
typename TDisplacementField::PixelType fdvec2 = this->m_DisplacementField->GetPixel(oindex);
float step = 0.1 * spacing[imd];
fdvec1[imd] = vec[imd] + step;
if( !m_Use1SidedDiff )
{
nccp1 = this->ComputeMetricAtPair(oindex, fdvec1);
}
fdvec2[imd] = vec[imd] - step;
nccm1 = this->ComputeMetricAtPair(oindex, fdvec2);
update[imd] = nccp1 - nccm1;
loce += (nccp1 + nccm1);
}
loce /= (2.0 * (float)ImageDimension); // this->ComputeMetricAtPair(oindex,vec);
this->m_Energy += loce;
if( m_MetricImage )
{
m_MetricImage->SetPixel(oindex, loce);
}
// float mag=0;
// for (int imd=0; imd<ImageDimension; imd++) mag+=update[imd]*update[imd];
// if (mag > 1) update.Fill(0.0);
// //std::cout << " update " << update << " ind " << oindex << std::endl;
return update * this->m_GradientStep;
}
void SetIterations( unsigned int i )
{
this->m_Iterations = i;
}
/** this parameter is used to set a minimum value for the metric -- a
minimum value that is treated as " close enough" -- */
void SetRobustnessParameter( float i )
{
this->m_RobustnessParameter = i;
}
void SetFixedPointSet( PointSetPointer p )
{
this->m_FixedPointSet = p;
}
void SetMovingPointSet( PointSetPointer p )
{
this->m_MovingPointSet = p;
}
bool ThisIsAPointSetMetric()
{
return this->m_IsPointSetMetric;
}
protected:
AvantsPDEDeformableRegistrationFunction()
{
this->m_MovingImage = NULL;
m_MetricImage = NULL;
this->m_FixedImage = NULL;
this->m_DisplacementField = NULL;
this->m_Energy = 0.0;
m_BestEnergy = 0.0;
this->m_NormalizeGradient = true;
this->m_GradientStep = 1.0;
m_AverageStepMag = 0;
m_MaxStepMag = 0;
m_AvgCt = 0;
m_Iterations = 0;
this->m_FixedPointSet = NULL;
this->m_MovingPointSet = NULL;
this->m_IsPointSetMetric = false;
this->m_RobustnessParameter = -1.e12;
}
~AvantsPDEDeformableRegistrationFunction()
{
}
void PrintSelf(std::ostream& os, Indent indent) const
{
this->PrintSelf(os, indent);
};
mutable double m_BestEnergy;
mutable double m_LastLastEnergy;
mutable double m_MaxAllowedStep;
mutable double m_AverageStepMag;
mutable unsigned long m_AvgCt;
mutable unsigned long m_Iterations;
mutable double m_MaxStepMag;
PointSetPointer m_FixedPointSet;
PointSetPointer m_MovingPointSet;
bool m_IsPointSetMetric;
MetricImagePointer m_MetricImage;
float m_RobustnessParameter;
private:
AvantsPDEDeformableRegistrationFunction(const Self &); // purposely not implemented
void operator=(const Self &); // purposely not implemented
};
} // end namespace itk
#endif