SimpleITK  2.0.0
ImageRegistrationMethod1/ImageRegistrationMethod1.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 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} : {2}".format(method.GetOptimizerIteration(),
29  method.GetMetricValue(),
30  method.GetOptimizerPosition()))
31 
32 
33 if len(sys.argv) < 4:
34  print("Usage:", sys.argv[0], "<fixedImageFilter> <movingImageFile>",
35  "<outputTransformFile>")
36  sys.exit(1)
37 
38 fixed = sitk.ReadImage(sys.argv[1], sitk.sitkFloat32)
39 
40 moving = sitk.ReadImage(sys.argv[2], sitk.sitkFloat32)
41 
43 R.SetMetricAsMeanSquares()
44 R.SetOptimizerAsRegularStepGradientDescent(4.0, .01, 200)
45 R.SetInitialTransform(sitk.TranslationTransform(fixed.GetDimension()))
46 R.SetInterpolator(sitk.sitkLinear)
47 
48 R.AddCommand(sitk.sitkIterationEvent, lambda: command_iteration(R))
49 
50 outTx = R.Execute(fixed, moving)
51 
52 print("-------")
53 print(outTx)
54 print("Optimizer stop condition: {0}"
55  .format(R.GetOptimizerStopConditionDescription()))
56 print(" Iteration: {0}".format(R.GetOptimizerIteration()))
57 print(" Metric value: {0}".format(R.GetMetricValue()))
58 
59 sitk.WriteTransform(outTx, sys.argv[3])
60 
61 if ("SITK_NOSHOW" not in os.environ):
62  resampler = sitk.ResampleImageFilter()
63  resampler.SetReferenceImage(fixed)
64  resampler.SetInterpolator(sitk.sitkLinear)
65  resampler.SetDefaultPixelValue(100)
66  resampler.SetTransform(outTx)
67 
68  out = resampler.Execute(moving)
69  simg1 = sitk.Cast(sitk.RescaleIntensity(fixed), sitk.sitkUInt8)
70  simg2 = sitk.Cast(sitk.RescaleIntensity(out), sitk.sitkUInt8)
71  cimg = sitk.Compose(simg1, simg2, simg1 // 2. + simg2 // 2.)
72  sitk.Show(cimg, "ImageRegistration1 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::TranslationTransform
Translation of a 2D or 3D coordinate space.
Definition: sitkTranslationTransform.h:35
itk::simple::Cast
Image Cast(const Image &image, PixelIDValueEnum pixelID)
itk::simple::ResampleImageFilter
Resample an image via a coordinate transform.
Definition: sitkResampleImageFilter.h:53
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