SimpleITK  
ImageRegistrationMethod3/ImageRegistrationMethod3.py
1 #!/usr/bin/env python
2 # =========================================================================
3 #
4 # Copyright NumFOCUS
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 import SimpleITK as sitk
21 import sys
22 import os
23 
24 
25 def command_iteration(method):
26  if method.GetOptimizerIteration() == 0:
27  print("Estimated Scales: ", method.GetOptimizerScales())
28  print(
29  f"{method.GetOptimizerIteration():3} "
30  + f"= {method.GetMetricValue():7.5f} "
31  + f": {method.GetOptimizerPosition()}"
32  )
33 
34 
35 def main(args):
36  if len(args) < 3:
37  print(
38  "Usage:",
39  "ImageRegistrationMethod3"
40  "<fixedImageFilter> <movingImageFile> <outputTransformFile>",
41  )
42  sys.exit(1)
43 
44  fixed = sitk.ReadImage(args[1], sitk.sitkFloat32)
45 
46  moving = sitk.ReadImage(args[2], sitk.sitkFloat32)
47 
49 
50  R.SetMetricAsCorrelation()
51 
52  R.SetOptimizerAsRegularStepGradientDescent(
53  learningRate=2.0,
54  minStep=1e-4,
55  numberOfIterations=500,
56  gradientMagnitudeTolerance=1e-8,
57  )
58  R.SetOptimizerScalesFromIndexShift()
59 
61  fixed, moving, sitk.Similarity2DTransform()
62  )
63  R.SetInitialTransform(tx)
64 
65  R.SetInterpolator(sitk.sitkLinear)
66 
67  R.AddCommand(sitk.sitkIterationEvent, lambda: command_iteration(R))
68 
69  outTx = R.Execute(fixed, moving)
70 
71  print("-------")
72  print(outTx)
73  print(f"Optimizer stop condition: {R.GetOptimizerStopConditionDescription()}")
74  print(f" Iteration: {R.GetOptimizerIteration()}")
75  print(f" Metric value: {R.GetMetricValue()}")
76 
77  sitk.WriteTransform(outTx, args[3])
78 
79  resampler = sitk.ResampleImageFilter()
80  resampler.SetReferenceImage(fixed)
81  resampler.SetInterpolator(sitk.sitkLinear)
82  resampler.SetDefaultPixelValue(100)
83  resampler.SetTransform(outTx)
84 
85  out = resampler.Execute(moving)
86 
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.0 + simg2 // 2.0)
90 
91  return {"fixed": fixed,
92  "moving": moving,
93  "composition": cimg}
94 
95 
96 if __name__ == "__main__":
97  return_dict = main(sys.argv)
98  if "SITK_NOSHOW" not in os.environ:
99  sitk.Show(return_dict["composition"], "ImageRegistration3 Composition")
itk::simple::WriteTransform
SITKCommon_EXPORT void WriteTransform(const Transform &transform, const std::string &filename)
itk::simple::RescaleIntensity
Image RescaleIntensity(const Image &image1, double outputMinimum=0, double outputMaximum=255)
Applies a linear transformation to the intensity levels of the input Image .
itk::simple::Show
void SITKIO_EXPORT Show(const Image &image, const std::string &title="", const bool debugOn=ProcessObject::GetGlobalDefaultDebug())
itk::simple::ReadImage
SITKIO_EXPORT Image ReadImage(const std::vector< std::string > &fileNames, PixelIDValueEnum outputPixelType=sitkUnknown, const std::string &imageIO="")
ReadImage is a procedural interface to the ImageSeriesReader class which is convenient for most image...
itk::simple::Similarity2DTransform
A similarity 2D transform with rotation in radians and isotropic scaling around a fixed center with t...
Definition: sitkSimilarity2DTransform.h:33
itk::simple::Cast
Image Cast(const Image &image, PixelIDValueEnum pixelID)
itk::simple::ResampleImageFilter
Resample an image via a coordinate transform.
Definition: sitkResampleImageFilter.h:52
itk::simple::Compose
Image Compose(const Image &image1, const Image &image2, const Image &image3, const Image &image4, const Image &image5)
ComposeImageFilter combine several scalar images into a multicomponent image.
itk::simple::ImageRegistrationMethod
An interface method to the modular ITKv4 registration framework.
Definition: sitkImageRegistrationMethod.h:89
itk::simple::CenteredTransformInitializer
Transform CenteredTransformInitializer(const Image &fixedImage, const Image &movingImage, const Transform &transform, CenteredTransformInitializerFilter::OperationModeType operationMode=itk::simple::CenteredTransformInitializerFilter::MOMENTS)
CenteredTransformInitializer is a helper class intended to initialize the center of rotation and the ...