SimpleITK  
DemonsRegistration2/DemonsRegistration2.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(filter):
26  print(f"{filter.GetElapsedIterations():3} = {filter.GetMetric():10.5f}")
27 
28 
29 if len(sys.argv) < 4:
30  print(
31  "Usage:",
32  sys.argv[0],
33  "<fixedImageFilter> <movingImageFile>",
34  "[initialTransformFile] <outputTransformFile>",
35  )
36  sys.exit(1)
37 
38 fixed = sitk.ReadImage(sys.argv[1])
39 
40 moving = sitk.ReadImage(sys.argv[2])
41 
43 if fixed.GetPixelID() in (sitk.sitkUInt8, sitk.sitkInt8):
44  matcher.SetNumberOfHistogramLevels(128)
45 else:
46  matcher.SetNumberOfHistogramLevels(1024)
47 matcher.SetNumberOfMatchPoints(7)
48 matcher.ThresholdAtMeanIntensityOn()
49 moving = matcher.Execute(moving, fixed)
50 
51 # The fast symmetric forces Demons Registration Filter
52 # Note there is a whole family of Demons Registration algorithms included in
53 # SimpleITK
55 demons.SetNumberOfIterations(200)
56 # Standard deviation for Gaussian smoothing of displacement field
57 demons.SetStandardDeviations(1.0)
58 
59 demons.AddCommand(sitk.sitkIterationEvent, lambda: command_iteration(demons))
60 
61 if len(sys.argv) > 4:
62  initialTransform = sitk.ReadTransform(sys.argv[3])
63  sys.argv[-1] = sys.argv.pop()
64 
65  toDisplacementFilter = sitk.TransformToDisplacementFieldFilter()
66  toDisplacementFilter.SetReferenceImage(fixed)
67 
68  displacementField = toDisplacementFilter.Execute(initialTransform)
69 
70  displacementField = demons.Execute(fixed, moving, displacementField)
71 
72 else:
73 
74  displacementField = demons.Execute(fixed, moving)
75 
76 print("-------")
77 print(f"Number Of Iterations: {demons.GetElapsedIterations()}")
78 print(f" RMS: {demons.GetRMSChange()}")
79 
80 outTx = sitk.DisplacementFieldTransform(displacementField)
81 
82 sitk.WriteTransform(outTx, sys.argv[3])
83 
84 if "SITK_NOSHOW" not in os.environ:
85  resampler = sitk.ResampleImageFilter()
86  resampler.SetReferenceImage(fixed)
87  resampler.SetInterpolator(sitk.sitkLinear)
88  resampler.SetDefaultPixelValue(100)
89  resampler.SetTransform(outTx)
90 
91  out = resampler.Execute(moving)
92  simg1 = sitk.Cast(sitk.RescaleIntensity(fixed), sitk.sitkUInt8)
93  simg2 = sitk.Cast(sitk.RescaleIntensity(out), sitk.sitkUInt8)
94  cimg = sitk.Compose(simg1, simg2, simg1 // 2.0 + simg2 // 2.0)
95  sitk.Show(cimg, "DeformableRegistration1 Composition")
itk::simple::FastSymmetricForcesDemonsRegistrationFilter
Deformably register two images using a symmetric forces demons algorithm.
Definition: sitkFastSymmetricForcesDemonsRegistrationFilter.h:66
itk::simple::WriteTransform
SITKCommon_EXPORT void WriteTransform(const Transform &transform, const std::string &filename)
itk::simple::DisplacementFieldTransform
A dense deformable transform over a bounded spatial domain for 2D or 3D coordinates space.
Definition: sitkDisplacementFieldTransform.h:34
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::TransformToDisplacementFieldFilter
Generate a displacement field from a coordinate transform.
Definition: sitkTransformToDisplacementFieldFilter.h:50
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::Cast
Image Cast(const Image &image, PixelIDValueEnum pixelID)
itk::simple::ResampleImageFilter
Resample an image via a coordinate transform.
Definition: sitkResampleImageFilter.h:52
itk::simple::HistogramMatchingImageFilter
Normalize the grayscale values for a source image by matching the shape of the source image histogram...
Definition: sitkHistogramMatchingImageFilter.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::ReadTransform
SITKCommon_EXPORT Transform ReadTransform(const std::string &filename)