SimpleITK  1.3.0.dev466
ImageRegistrationMethodBSpline2/ImageRegistrationMethodBSpline2.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) :
28  print("{0:3} = {1:10.5f}".format(method.GetOptimizerIteration(),
29  method.GetMetricValue()))
30  print("\t#: ", len(method.GetOptimizerPosition()))
31 
32 
33 def command_multi_iteration(method) :
34  print("--------- Resolution Changing ---------")
35 
36 
37 if len ( sys.argv ) < 4:
38  print( "Usage: {0} <fixedImageFilter> <movingImageFile> <outputTransformFile>".format(sys.argv[0]))
39  sys.exit ( 1 )
40 
41 
42 fixed = sitk.ReadImage(sys.argv[1], sitk.sitkFloat32)
43 
44 moving = sitk.ReadImage(sys.argv[2], sitk.sitkFloat32)
45 
46 transformDomainMeshSize=[10]*moving.GetDimension()
48  transformDomainMeshSize )
49 
50 print("Initial Parameters:");
51 print(tx.GetParameters())
52 
54 R.SetMetricAsMattesMutualInformation(50)
55 R.SetOptimizerAsGradientDescentLineSearch(5.0, 100,
56  convergenceMinimumValue=1e-4,
57  convergenceWindowSize=5)
58 R.SetOptimizerScalesFromPhysicalShift( )
59 R.SetInitialTransform(tx)
60 R.SetInterpolator(sitk.sitkLinear)
61 
62 R.SetShrinkFactorsPerLevel([6,2,1])
63 R.SetSmoothingSigmasPerLevel([6,2,1])
64 
65 R.AddCommand( sitk.sitkIterationEvent, lambda: command_iteration(R) )
66 R.AddCommand( sitk.sitkMultiResolutionIterationEvent, lambda: command_multi_iteration(R) )
67 
68 outTx = R.Execute(fixed, moving)
69 
70 print("-------")
71 print(outTx)
72 print("Optimizer stop condition: {0}".format(R.GetOptimizerStopConditionDescription()))
73 print(" Iteration: {0}".format(R.GetOptimizerIteration()))
74 print(" Metric value: {0}".format(R.GetMetricValue()))
75 
76 sitk.WriteTransform(outTx, sys.argv[3])
77 
78 if ( not "SITK_NOSHOW" in os.environ ):
79 
80  resampler = sitk.ResampleImageFilter()
81  resampler.SetReferenceImage(fixed);
82  resampler.SetInterpolator(sitk.sitkLinear)
83  resampler.SetDefaultPixelValue(100)
84  resampler.SetTransform(outTx)
85 
86  out = resampler.Execute(moving)
87  simg1 = sitk.Cast(sitk.RescaleIntensity(fixed), sitk.sitkUInt8)
88  simg2 = sitk.Cast(sitk.RescaleIntensity(out), sitk.sitkUInt8)
89  cimg = sitk.Compose(simg1, simg2, simg1//2.+simg2//2.)
90  sitk.Show( cimg, "ImageRegistration1 Composition" )