-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathregistration.py
392 lines (324 loc) · 12.6 KB
/
registration.py
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import SimpleITK as sitk
# using ITK procedures
def rigid_transformation(ref_scan,
mov_scan,
number_bins=50,
levels=3,
steps=50,
sampling=0.5,
learning_rate=1.0,
min_step=0.0001,
max_step=0.2,
relaxation_factor=0.5,
verbose=1):
"""
Compute a rigid trasnformation between a ref and a moving scan.
inputs:
- ref_scan: numpy 3D image containing the reference image
- mov_scan: numpy 3D image containing the moving image
- number_bins: number of histogram bins (50)
- levels: number of levels used (3)
- steps: Steps per level (50)
- sampling: (0.5)
- learning_rate: (1.0)
- min_step: (0.001)
- max_step: (0.2)
- relaxation_factor: (0.5)
- verbose: print stuff
outputs:
- transf: itk trasnformation
"""
# convert np arrays into itk image objects
ref = sitk.GetImageFromArray(ref_scan.astype('float32'))
mov = sitk.GetImageFromArray(mov_scan.astype('float32'))
# compute transformations
transf = sitk.CenteredTransformInitializer(
ref,
mov,
sitk.VersorRigid3DTransform(),
sitk.CenteredTransformInitializerFilter.MOMENTS)
# Registration parameters
registration = sitk.ImageRegistrationMethod()
# Similarity metric settings
registration.SetMetricAsMattesMutualInformation(numberOfHistogramBins=number_bins)
registration.SetMetricSamplingStrategy(registration.RANDOM)
registration.SetMetricSamplingPercentage(sampling)
registration.SetInterpolator(sitk.sitkLinear)
# Optimizer settings
registration.SetOptimizerAsRegularStepGradientDescent(
learningRate=learning_rate,
minStep=min_step,
numberOfIterations=steps,
maximumStepSizeInPhysicalUnits=max_step,
relaxationFactor=relaxation_factor
)
registration.SetOptimizerScalesFromPhysicalShift()
# Setup for the multi-resolution framework.
smoothing_sigmas = range(levels - 1, -1, -1)
shrink_factor = [2**i for i in smoothing_sigmas]
registration.SetShrinkFactorsPerLevel(shrinkFactors=shrink_factor)
registration.SetSmoothingSigmasPerLevel(smoothingSigmas=smoothing_sigmas)
registration.SmoothingSigmasAreSpecifiedInPhysicalUnitsOn()
# Connect all of the observers so that we can perform plotting during registration.
if verbose > 0:
print('Rigid initial registration')
registration.AddCommand(sitk.sitkMultiResolutionIterationEvent,
lambda: print('{:} level {:}'.format(
registration.GetName(),
registration.GetCurrentLevel())))
if verbose > 1:
registration.AddCommand(
sitk.sitkIterationEvent,
lambda: print_current(registration, transf))
# Initial versor optimisation
registration.SetInitialTransform(transf)
registration.Execute(ref, mov)
return transf
def affine_transformation(
ref_scan,
mov_scan,
initial_tf,
number_bins=50,
levels=3,
steps=50,
sampling=0.5,
learning_rate=1.0,
min_step=0.0001,
max_step=0.2,
relaxation_factor=0.5,
verbose=1):
"""
Compute a affine transformation between a ref and a moving scan.
inputs:
- ref_scan: numpy 3D image containing the reference image
- mov_scan: numpy 3D image containing the moving image
- initial_tf: initial rigid transformation
- number_bins: number of histogram bins (50)
- levels: number of levels used (3)
- steps: Steps per level (50)
- sampling: (0.5)
- learning_rate: (1.0)
- min_step: (0.001)
- max_step: (0.2)
- relaxation_factor: (0.5)
- verbose: print stuff
outputs:
- transf: itk trasnformation
"""
# convert np arrays into itk image objects
ref = sitk.GetImageFromArray(ref_scan.astype('float32'))
mov = sitk.GetImageFromArray(mov_scan.astype('float32'))
optimized_tf = sitk.AffineTransform(3)
# Registration parameters
registration = sitk.ImageRegistrationMethod()
# Similarity metric settings
registration.SetMetricAsMattesMutualInformation(numberOfHistogramBins=number_bins)
registration.SetMetricSamplingStrategy(registration.RANDOM)
registration.SetMetricSamplingPercentage(sampling)
registration.SetInterpolator(sitk.sitkLinear)
# Optimizer settings
registration.SetOptimizerAsRegularStepGradientDescent(
learningRate=learning_rate,
minStep=min_step,
numberOfIterations=steps,
maximumStepSizeInPhysicalUnits=max_step,
relaxationFactor=relaxation_factor
)
registration.SetOptimizerScalesFromPhysicalShift()
# Setup for the multi-resolution framework.
smoothing_sigmas = range(levels - 1, -1, -1)
shrink_factor = [2**i for i in smoothing_sigmas]
registration.SetShrinkFactorsPerLevel(shrinkFactors=shrink_factor)
registration.SetSmoothingSigmasPerLevel(smoothingSigmas=smoothing_sigmas)
registration.SmoothingSigmasAreSpecifiedInPhysicalUnitsOn()
# affine Optimizer settings.
registration.RemoveAllCommands()
if verbose > 0:
print('\tAffine registration')
registration.AddCommand(
sitk.sitkMultiResolutionIterationEvent,
lambda: print('\t > %s level %d' % (
registration.GetName(),
registration.GetCurrentLevel()
))
)
if verbose > 1:
registration.AddCommand(
sitk.sitkIterationEnvent,
lambda: print_current(registration, optimized_tf)
)
registration.SetMovingInitialTransform(initial_tf)
registration.SetInitialTransform(optimized_tf)
registration.Execute(ref, mov)
affine_tf = sitk.Transform(optimized_tf)
affine_tf.AddTransform(initial_tf)
return affine_tf
def linear_registration(ref_scan,
mov_scan,
reg_type='affine',
interpolation=sitk.sitkBSpline,
number_bins=50,
levels=3,
steps=50,
sampling=0.5,
learning_rate=1.0,
min_step=0.0001,
max_step=0.2,
rel_factor=0.5,
default_value=0.0,
verbose=1):
"""
Perform a rigid registration between a ref and moving image.
Using ITK procedures.
inputs:
- ref_scan: numpy 3D image containing the reference image
- mov_scan: numpy 3D image containing the moving image
- ret_type: 'affine', 'rigid'
- interpolation: ITK tranformation type (BSSpline, Linear, ...)
- number_bins: number of histogram bins (50)
- levels: number of levels used (3)
- steps: Steps per level (50)
- sampling: (0.5)
- learning_rate: (1.0)
- min_step: (0.001)
- max_step: (0.2)
- relaxation_factor: (0.5)
- verbose: print stuff
outputs:
- transf: itk trasnformation
"""
# compute the rigid transformation
current_transf = rigid_transformation(ref_scan,
mov_scan,
number_bins=number_bins,
levels=levels,
steps=steps,
sampling=sampling,
learning_rate=learning_rate,
min_step=min_step,
max_step=max_step,
relaxation_factor=rel_factor,
verbose=verbose)
if reg_type == 'affine':
# compute the affine transformation
current_transf = affine_transformation(ref_scan,
mov_scan,
initial_tf=current_transf,
number_bins=number_bins,
levels=levels,
steps=steps,
sampling=sampling,
learning_rate=learning_rate,
min_step=min_step,
max_step=max_step,
relaxation_factor=rel_factor,
verbose=verbose)
# apply the transformation
return reg_resample(ref_scan,
mov_scan,
current_transf,
default_value=default_value,
interpolation=interpolation)
def reg_resample(ref_scan,
mov_scan,
transform,
default_value=0.0,
interpolation=sitk.sitkBSpline):
"""
Given a computed transformation, resample two images.
Inputs:
- ref_scan: ref image
- mov_scan: moving image to resample
- tranform: rigid, affine or deformable itk transformation
- interpolation: ITK tranformation type (BSSpline, Linear, ...)
Outputs:
- resampled moving scan
"""
# convert np arrays into itk image objects
ref = sitk.GetImageFromArray(ref_scan.astype('float32'))
mov = sitk.GetImageFromArray(mov_scan.astype('float32'))
resampled = sitk.Resample(mov, ref, transform, interpolation, default_value)
return sitk.GetArrayFromImage(resampled)
def print_current(reg_method, tf_):
"""
Print information about the current registration iteration
"""
print('\t MI (%d): %f\n\t %s: [%s]' % (
reg_method.GetOptimizerIteration(),
reg_method.GetMetricValue(),
tf.GetName(),
', '.join(['%s' % p for p in tf_.GetParameters()])))
def deformation_field(
ref_scan,
mov_scan,
write_res=True,
steps=50,
sigma=1.0,
verbose=1):
"""
Compute the deformation field between a ref and a moving scan.
Inputs:
- ref_scan: reference scan
- mov_scan: moving scan
- def_name: deformation field name to save
- write_res: write results to disk (res)
steps: number of steps (50)
sigma: (1)
verbose)
outputs:
- deformation field tranformation
"""
# convert np arrays into itk image objects
ref = sitk.GetImageFromArray(ref_scan.astype('float32'))
mov = sitk.GetImageFromArray(mov_scan.astype('float32'))
if verbose > 1:
print('\t Deformation: ', def_name)
demons = sitk.DemonsRegistrationFilter()
demons.SetNumberOfIterations(steps)
demons.SetStandardDeviations(sigma)
if verbose > 1:
demons.AddCommand(
sitk.sitkIterationEvent,
lambda: print('\t Demons %d: %f' % (demons.GetElapsedIterations(),
demons.GetMetric())))
# compute deformation fields
def_field = demons.Execute(ref, mov)
# if write_res:
# sitk.WriteImage(deformation_field, def_name)
return def_field
#return sitk.GetArrayFromImage(deformation_field)
def deformable_registration(ref_scan,
mov_scan,
write_res=False,
steps=50,
sigma=1.0,
verbose=1):
"""
Compute the deformation field between a ref and a moving scan.
Inputs:
- ref_scan: reference scan
- mov_scan: moving scan
- def_name: deformation field name to save
- write_res: write results to disk (res)
steps: number of steps (50)
sigma: (1)
verbose)
outputs:
- registered_moving image
- 3 deformation fields
"""
# compute the deformation field
def_field = deformation_field(
ref_scan=ref_scan,
mov_scan=mov_scan,
write_res=write_res,
steps=steps,
sigma=sigma,
verbose=verbose)
# transform the def_field
out_def = sitk.DisplacementFieldTransform(def_field)
# resample image
def_image = reg_resample(ref_scan, mov_scan, out_def)
# convert the maps back
def_maps = sitk.GetArrayFromImage(def_field)
return def_image, def_maps