SimpleITK  1.2.4
ImageRegistrationMethodBSpline3/ImageRegistrationMethodBSpline3.py
1 #!/usr/bin/env python
2 #=========================================================================
3 #
4 # Copyright Insight Software Consortium
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0.txt
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 #=========================================================================
19 
20 from __future__ import print_function
21 
22 import SimpleITK as sitk
23 import sys
24 import os
25 
26 
27 def command_iteration(method, bspline_transform) :
28  if method.GetOptimizerIteration() == 0:
29  # The BSpline is resized before the first optimizer
30  # iteration is completed per level. Print the transform object
31  # to show the adapted BSpline transform.
32  print(bspline_transform)
33 
34 
35  print("{0:3} = {1:10.5f}".format(method.GetOptimizerIteration(),
36  method.GetMetricValue()))
37 
38 
39 
40 def command_multi_iteration(method) :
41  # The sitkMultiResolutionIterationEvent occurs before the
42  # resolution of the transform. This event is used here to print
43  # the status of the optimizer from the previous registration level.
44  if R.GetCurrentLevel() > 0:
45  print("Optimizer stop condition: {0}".format(R.GetOptimizerStopConditionDescription()))
46  print(" Iteration: {0}".format(R.GetOptimizerIteration()))
47  print(" Metric value: {0}".format(R.GetMetricValue()))
48 
49  print("--------- Resolution Changing ---------")
50 
51 
52 if len ( sys.argv ) < 4:
53  print( "Usage: {0} <fixedImageFilter> <movingImageFile> <outputTransformFile>".format(sys.argv[0]))
54  sys.exit ( 1 )
55 
56 
57 fixed = sitk.ReadImage(sys.argv[1], sitk.sitkFloat32)
58 
59 moving = sitk.ReadImage(sys.argv[2], sitk.sitkFloat32)
60 
61 transformDomainMeshSize=[2]*fixed.GetDimension()
63  transformDomainMeshSize )
64 
65 print("Initial Number of Parameters: {0}".format(tx.GetNumberOfParameters()))
66 
68 R.SetMetricAsJointHistogramMutualInformation()
69 
70 R.SetOptimizerAsGradientDescentLineSearch(5.0,
71  100,
72  convergenceMinimumValue=1e-4,
73  convergenceWindowSize=5)
74 
75 R.SetInterpolator(sitk.sitkLinear)
76 
77 R.SetInitialTransformAsBSpline(tx,
78  inPlace=True,
79  scaleFactors=[1,2,5])
80 R.SetShrinkFactorsPerLevel([4,2,1])
81 R.SetSmoothingSigmasPerLevel([4,2,1])
82 
83 R.AddCommand( sitk.sitkIterationEvent, lambda: command_iteration(R, tx) )
84 R.AddCommand( sitk.sitkMultiResolutionIterationEvent, lambda: command_multi_iteration(R) )
85 
86 outTx = R.Execute(fixed, moving)
87 
88 print("-------")
89 print(tx)
90 print(outTx)
91 print("Optimizer stop condition: {0}".format(R.GetOptimizerStopConditionDescription()))
92 print(" Iteration: {0}".format(R.GetOptimizerIteration()))
93 print(" Metric value: {0}".format(R.GetMetricValue()))
94 
95 sitk.WriteTransform(outTx, sys.argv[3])
96 
97 if ( not "SITK_NOSHOW" in os.environ ):
98 
99  resampler = sitk.ResampleImageFilter()
100  resampler.SetReferenceImage(fixed);
101  resampler.SetInterpolator(sitk.sitkLinear)
102  resampler.SetDefaultPixelValue(100)
103  resampler.SetTransform(outTx)
104 
105  out = resampler.Execute(moving)
106  simg1 = sitk.Cast(sitk.RescaleIntensity(fixed), sitk.sitkUInt8)
107  simg2 = sitk.Cast(sitk.RescaleIntensity(out), sitk.sitkUInt8)
108  cimg = sitk.Compose(simg1, simg2, simg1//2.+simg2//2.)
109  sitk.Show( cimg, "Image Registration Composition" )