SimpleITK  1.0.1
DemonsRegistration2/DemonsRegistration2.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(filter) :
28  print("{0:3} = {1:10.5f}".format(filter.GetElapsedIterations(),
29  filter.GetMetric()))
30 
31 if len ( sys.argv ) < 4:
32  print( "Usage: {0} <fixedImageFilter> <movingImageFile> [initialTransformFile] <outputTransformFile>".format(sys.argv[0]))
33  sys.exit ( 1 )
34 
35 
36 fixed = sitk.ReadImage(sys.argv[1])
37 
38 moving = sitk.ReadImage(sys.argv[2])
39 
41 if ( fixed.GetPixelID() in ( sitk.sitkUInt8, sitk.sitkInt8 ) ):
42  matcher.SetNumberOfHistogramLevels(128)
43 else:
44  matcher.SetNumberOfHistogramLevels(1024)
45 matcher.SetNumberOfMatchPoints(7)
46 matcher.ThresholdAtMeanIntensityOn()
47 moving = matcher.Execute(moving,fixed)
48 
50 demons.SetNumberOfIterations(200)
51 demons.SetStandardDeviations(1.0)
52 
53 demons.AddCommand( sitk.sitkIterationEvent, lambda: command_iteration(demons) )
54 
55 if len( sys.argv ) > 4:
56  initialTransform = sitk.ReadTransform(sys.argv[3])
57  sys.argv[-1] = sys.argv.pop()
58 
59  toDisplacementFilter = sitk.TransformToDisplacementFieldFilter()
60  toDisplacementFilter.SetReferenceImage(fixed)
61 
62  displacementField = toDisplacementFilter.Execute(initialTransform)
63 
64  displacementField = demons.Execute(fixed, moving, displacementField)
65 
66 else:
67 
68  displacementField = demons.Execute(fixed, moving)
69 
70 
71 print("-------")
72 print("Number Of Iterations: {0}".format(demons.GetElapsedIterations()))
73 print(" RMS: {0}".format(demons.GetRMSChange()))
74 
75 outTx = sitk.DisplacementFieldTransform(displacementField)
76 
77 sitk.WriteTransform(outTx, sys.argv[3])
78 
79 if (not "SITK_NOSHOW" in os.environ):
80 
81  resampler = sitk.ResampleImageFilter()
82  resampler.SetReferenceImage(fixed);
83  resampler.SetInterpolator(sitk.sitkLinear)
84  resampler.SetDefaultPixelValue(100)
85  resampler.SetTransform(outTx)
86 
87  out = resampler.Execute(moving)
88  simg1 = sitk.Cast(sitk.RescaleIntensity(fixed), sitk.sitkUInt8)
89  simg2 = sitk.Cast(sitk.RescaleIntensity(out), sitk.sitkUInt8)
90  cimg = sitk.Compose(simg1, simg2, simg1/2.+simg2/2.)
91  sitk.Show( cimg, "DeformableRegistration1 Composition" )