forked from InsightSoftwareConsortium/ITK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModelToImageRegistration2.cxx
376 lines (281 loc) · 11.9 KB
/
ModelToImageRegistration2.cxx
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
// Software Guide : BeginLatex
//
// This example illustrates the use of the \doxygen{SpatialObject} as a
// component of the registration framework in order to perform model based
// registration. In this case, a SpatialObject is used for generating a
// \doxygen{PointSet} whose points are located in a narrow band around the
// edges of the SpatialObject. This PointSet is then used in order to perform
// PointSet to Image registration.
//
// Software Guide : EndLatex
// Software Guide : BeginLatex
//
// In this example we use the \doxygen{BoxSpatialObject}, that is one of the
// simplest SpatialObjects in ITK.
//
// \index{itk::BoxSpatialObject!header}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
#include "itkBoxSpatialObject.h"
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The generation of the PointSet is done in two stages. First the
// SpatialObject is rasterized in order to generate an image containing a
// binary mask that represents the inside and outside of the SpatialObject.
// Second, this mask is used for computing a distance map, and the points
// close to the boundary of the mask are taken as elements of the final
// PointSet. The pixel values associated to the point in the PointSet are the
// values of distance from each point to the binary mask. The first stage is
// performed by the \doxygen{SpatialObjectToImageFilter}, while the second
// stage is performed witht eh \doxygen{BinaryMaskToNarrowBandPointSetFilter}
//
// \index{itk::Spatial\-Object\-To\-Image\-Filter!header}
// \index{itk::Binary\-Mask\-To\-Narrow\-Band\-Point\-Set\-Filter!header}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
#include "itkSpatialObjectToImageFilter.h"
#include "itkBinaryMaskToNarrowBandPointSetFilter.h"
// Software Guide : EndCodeSnippet
#include "itkBinaryMaskToNarrowBandPointSetFilter.h"
#include "itkPointSet.h"
#include "itkPointSetToImageRegistrationMethod.h"
#include "itkNormalizedCorrelationPointSetToImageMetric.h"
#include "itkNearestNeighborInterpolateImageFunction.h"
#include "itkRigid2DTransform.h"
#include "itkRegularStepGradientDescentOptimizer.h"
#include "itkResampleImageFilter.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
//
// Observer to the optimizer
//
class CommandIterationUpdate : public itk::Command
{
public:
typedef CommandIterationUpdate Self;
typedef itk::Command Superclass;
typedef itk::SmartPointer<Self> Pointer;
itkNewMacro( Self );
protected:
CommandIterationUpdate() {};
public:
typedef itk::RegularStepGradientDescentOptimizer OptimizerType;
typedef const OptimizerType * OptimizerPointer;
void Execute(itk::Object *caller, const itk::EventObject & event)
{
Execute( (const itk::Object *)caller, event);
}
void Execute(const itk::Object * object, const itk::EventObject & event)
{
OptimizerPointer optimizer = static_cast< OptimizerPointer >( object );
if( typeid( event ) != typeid( itk::IterationEvent ) )
{
return;
}
OptimizerType::DerivativeType gradient = optimizer->GetGradient();
OptimizerType::ScalesType scales = optimizer->GetScales();
double magnitude2 = 0.0;
for(unsigned int i=0; i<gradient.size(); i++)
{
const double fc = gradient[i] / scales[i];
magnitude2 += fc * fc;
}
const double gradientMagnitude = std::sqrt( magnitude2 );
std::cout << optimizer->GetCurrentIteration() << " ";
std::cout << optimizer->GetValue() << " ";
std::cout << gradientMagnitude << " ";
std::cout << optimizer->GetCurrentPosition() << std::endl;
}
};
int main( int argc, char * argv [] )
{
if( argc < 2 )
{
std::cerr << "Missing argument" << std::endl;
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0] << " movingImageFileName [initialX initialY] " << std::endl;
std::cerr << "[rasterizedObjectFileName] [BoxSizeX BoxSizeY]" << std::endl;
return EXIT_FAILURE;
}
const unsigned int Dimension = 2;
typedef unsigned char MaskPixelType;
typedef itk::Image< MaskPixelType, Dimension > MaskImageType;
typedef itk::BoxSpatialObject< Dimension > SpatialObjectType;
typedef itk::SpatialObjectToImageFilter<
SpatialObjectType,
MaskImageType
> SpatialObjectToImageFilterType;
typedef itk::PointSet< float, Dimension > FixedPointSetType;
typedef itk::BinaryMaskToNarrowBandPointSetFilter<
MaskImageType,
FixedPointSetType
> NarrowBandFilterType;
typedef signed short PixelType;
typedef itk::Image< PixelType, Dimension > ImageType;
typedef unsigned char MaskPixelType;
typedef itk::Image< MaskPixelType, Dimension > MaskImageType;
typedef itk::Rigid2DTransform< double > TransformType;
typedef TransformType::ParametersType ParametersType;
typedef itk::RegularStepGradientDescentOptimizer OptimizerType;
typedef itk::LinearInterpolateImageFunction<
ImageType,
double > LinearInterpolatorType;
typedef itk::NormalizedCorrelationPointSetToImageMetric<
FixedPointSetType,
ImageType > MetricType;
typedef OptimizerType::ScalesType OptimizerScalesType;
typedef itk::PointSetToImageRegistrationMethod<
FixedPointSetType,
ImageType > RegistrationType;
typedef CommandIterationUpdate IterationObserverType;
typedef itk::ImageFileReader< ImageType > ImageReaderType;
SpatialObjectType::Pointer spatialObject;
TransformType::Pointer transform;
OptimizerType::Pointer optimizer;
IterationObserverType::Pointer iterationObserver;
LinearInterpolatorType::Pointer linearInterpolator;
MetricType::Pointer metric;
RegistrationType::Pointer registrationMethod;
ImageReaderType::Pointer movingImageReader;
FixedPointSetType::Pointer fixedPointSet;
ImageType::ConstPointer movingImage;
SpatialObjectToImageFilterType::Pointer rasterizationFilter;
NarrowBandFilterType::Pointer narrowBandPointSetFilter;
metric = MetricType::New();
transform = TransformType::New();
optimizer = OptimizerType::New();
linearInterpolator = LinearInterpolatorType::New();
registrationMethod = RegistrationType::New();
iterationObserver = IterationObserverType::New();
spatialObject = SpatialObjectType::New();
rasterizationFilter = SpatialObjectToImageFilterType::New();
narrowBandPointSetFilter = NarrowBandFilterType::New();
movingImageReader = ImageReaderType::New();
movingImageReader->SetFileName( argv[1] );
try
{
movingImageReader->Update();
}
catch( itk::ExceptionObject & excp )
{
std::cerr << "Problem reading Moving image from = " << std::endl;
std::cerr << argv[1] << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
movingImage = movingImageReader->GetOutput();
SpatialObjectType::SizeType boxSize;
boxSize[0] = 60.0; // mm
boxSize[1] = 60.0; // mm
if( argc > 6 )
{
boxSize[0] = atof( argv[5] );
boxSize[1] = atof( argv[6] );
}
//
// The geometry of the BoxSpatialObject is such that one of
// its corners is located at the origin of coordinates.
//
spatialObject->SetSize( boxSize );
ImageType::RegionType region =
movingImage->GetLargestPossibleRegion();
ImageType::SizeType imageSize = region.GetSize();
ImageType::SpacingType spacing = movingImage->GetSpacing();
ImageType::PointType origin;
origin[0] = ( boxSize[0] - imageSize[0] * spacing[0] ) / 2.0;
origin[1] = ( boxSize[1] - imageSize[1] * spacing[1] ) / 2.0;
rasterizationFilter->SetInput( spatialObject );
rasterizationFilter->SetSize( imageSize );
rasterizationFilter->SetSpacing( spacing );
rasterizationFilter->SetOrigin( origin );
narrowBandPointSetFilter->SetBandWidth( 5.0 );
narrowBandPointSetFilter->SetInput(
rasterizationFilter->GetOutput() );
narrowBandPointSetFilter->Update();
if( argc > 4 )
{
typedef itk::ImageFileWriter< MaskImageType > MaskWriterType;
MaskWriterType::Pointer maskWriter = MaskWriterType::New();
maskWriter->SetInput( rasterizationFilter->GetOutput() );
maskWriter->SetFileName( argv[4] );
maskWriter->Update();
}
fixedPointSet = narrowBandPointSetFilter->GetOutput();
fixedPointSet->Print( std::cout );
registrationMethod->SetOptimizer( optimizer );
registrationMethod->SetInterpolator( linearInterpolator );
registrationMethod->SetMetric( metric );
registrationMethod->SetTransform( transform );
registrationMethod->SetMovingImage( movingImage );
registrationMethod->SetFixedPointSet( fixedPointSet );
optimizer->SetMaximumStepLength( 1.00 );
optimizer->SetMinimumStepLength( 0.001 );
optimizer->SetNumberOfIterations( 300 );
optimizer->SetRelaxationFactor( 0.90 );
optimizer->SetGradientMagnitudeTolerance( 0.05 );
optimizer->MinimizeOn();
optimizer->AddObserver( itk::IterationEvent(), iterationObserver );
TransformType::TranslationType initialTranslation;
initialTranslation[0] = 0.0;
initialTranslation[1] = 0.0;
if( argc >= 4 )
{
initialTranslation[0] = atof( argv[2] );
initialTranslation[1] = atof( argv[3] );
}
TransformType::OutputPointType rotationCenter;
rotationCenter[0] = boxSize[0] / 2.0;
rotationCenter[1] = boxSize[1] / 2.0;
transform->SetIdentity();
transform->SetCenter( rotationCenter );
transform->SetTranslation( initialTranslation );
registrationMethod->SetInitialTransformParameters(
transform->GetParameters() );
OptimizerScalesType optimizerScales( transform->GetNumberOfParameters() );
const double translationScale = 1.0 / 1000.0;
optimizerScales[0] = 1.0;
optimizerScales[1] = translationScale;
optimizerScales[2] = translationScale;
optimizer->SetScales( optimizerScales );
try
{
registrationMethod->Update();
std::cout << "Optimizer stop condition: "
<< registrationMethod->GetOptimizer()->GetStopConditionDescription()
<< std::endl;
}
catch( itk::ExceptionObject & excp )
{
std::cerr << "Problem found during the registration" << std::endl;
std::cerr << argv[1] << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
ParametersType transformParameters =
registrationMethod->GetLastTransformParameters();
TransformType::OutputPointType center = transform->GetCenter();
std::cout << "Registration parameter = " << std::endl;
std::cout << "Rotation center = " << center << std::endl;
std::cout << "Parameters = " << transformParameters << std::endl;
return EXIT_SUCCESS;
}