20 from distutils.version
import StrictVersion
as VS
22 print(
"ITK 4.9.0 is required.")
25 if len(sys.argv) != 6:
26 print(
"Usage: " + sys.argv[0] +
" <fixedImageFile> <movingImageFile> "
27 "<outputImagefile> <differenceImageAfter> <differenceImageBefore>")
30 fixedImageFile = sys.argv[1]
31 movingImageFile = sys.argv[2]
32 outputImageFile = sys.argv[3]
33 differenceImageAfterFile = sys.argv[4]
34 differenceImageBeforeFile = sys.argv[5]
36 PixelType = itk.ctype(
'float')
38 fixedImage = itk.imread(fixedImageFile, PixelType)
39 movingImage = itk.imread(movingImageFile, PixelType)
41 Dimension = fixedImage.GetImageDimension()
42 FixedImageType =
itk.Image[PixelType, Dimension]
43 MovingImageType =
itk.Image[PixelType, Dimension]
46 initialTransform = TransformType.New()
50 MinimumStepLength=0.001,
52 NumberOfIterations=200)
55 FixedImageType, MovingImageType].New()
58 MovingImage=movingImage,
61 InitialTransform=initialTransform)
63 movingInitialTransform = TransformType.New()
64 initialParameters = movingInitialTransform.GetParameters()
65 initialParameters[0] = 0
66 initialParameters[1] = 0
67 movingInitialTransform.SetParameters(initialParameters)
68 registration.SetMovingInitialTransform(movingInitialTransform)
70 identityTransform = TransformType.New()
71 identityTransform.SetIdentity()
72 registration.SetFixedInitialTransform(identityTransform)
74 registration.SetNumberOfLevels(1)
75 registration.SetSmoothingSigmasPerLevel([0])
76 registration.SetShrinkFactorsPerLevel([1])
80 transform = registration.GetTransform()
81 finalParameters = transform.GetParameters()
82 translationAlongX = finalParameters.GetElement(0)
83 translationAlongY = finalParameters.GetElement(1)
85 numberOfIterations = optimizer.GetCurrentIteration()
87 bestValue = optimizer.GetValue()
90 print(
" Translation X = " + str(translationAlongX))
91 print(
" Translation Y = " + str(translationAlongY))
92 print(
" Iterations = " + str(numberOfIterations))
93 print(
" Metric value = " + str(bestValue))
96 outputCompositeTransform = CompositeTransformType.New()
97 outputCompositeTransform.AddTransform(movingInitialTransform)
98 outputCompositeTransform.AddTransform(registration.GetModifiableTransform())
101 Transform=outputCompositeTransform,
102 UseReferenceImage=
True,
103 ReferenceImage=fixedImage)
104 resampler.SetDefaultPixelValue(100)
106 OutputPixelType = itk.ctype(
'unsigned char')
107 OutputImageType =
itk.Image[OutputPixelType, Dimension]
110 OutputImageType].New(Input=resampler)
113 writer.SetFileName(outputImageFile)
120 OutputImageType].New(
122 OutputMinimum=itk.NumericTraits[OutputPixelType].min(),
123 OutputMaximum=itk.NumericTraits[OutputPixelType].max())
125 resampler.SetDefaultPixelValue(1)
126 writer.SetInput(intensityRescaler.GetOutput())
127 writer.SetFileName(differenceImageAfterFile)
130 resampler.SetTransform(identityTransform)
131 writer.SetFileName(differenceImageBeforeFile)