SimpleITK  
DemonsRegistration1/DemonsRegistration1.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""" A SimpleITK example demonstrating the classic Demons image registration. """
21
22import sys
23import os
24import SimpleITK as sitk
25
26
27def command_iteration(sitk_filter):
28 """ Callback invoked when the filter processes an iteration. """
29 print(f"{sitk_filter.GetElapsedIterations():3} = {sitk_filter.GetMetric():10.5f}")
30
31
32if len(sys.argv) < 4:
33 print(
34 f"Usage: {sys.argv[0]}"
35 + " <fixedImageFilter> <movingImageFile> <outputTransformFile>"
36 )
37 sys.exit(1)
38
39fixed = sitk.ReadImage(sys.argv[1], sitk.sitkFloat32)
40
41moving = sitk.ReadImage(sys.argv[2], sitk.sitkFloat32)
42
44matcher.SetNumberOfHistogramLevels(1024)
45matcher.SetNumberOfMatchPoints(7)
46matcher.ThresholdAtMeanIntensityOn()
47moving = matcher.Execute(moving, fixed)
48
49# The basic Demons Registration Filter
50# Note there is a whole family of Demons Registration algorithms included in
51# SimpleITK
53demons.SetNumberOfIterations(50)
54# Standard deviation for Gaussian smoothing of displacement field
55demons.SetStandardDeviations(1.0)
56
57demons.AddCommand(sitk.sitkIterationEvent, lambda: command_iteration(demons))
58
59displacementField = demons.Execute(fixed, moving)
60
61print("-------")
62print(f"Number Of Iterations: {demons.GetElapsedIterations()}")
63print(f" RMS: {demons.GetRMSChange()}")
64
65outTx = sitk.DisplacementFieldTransform(displacementField)
66
67sitk.WriteTransform(outTx, sys.argv[3])
68
69if "SITK_NOSHOW" not in os.environ:
70 resampler = sitk.ResampleImageFilter()
71 resampler.SetReferenceImage(fixed)
72 resampler.SetInterpolator(sitk.sitkLinear)
73 resampler.SetDefaultPixelValue(100)
74 resampler.SetTransform(outTx)
75
76 out = resampler.Execute(moving)
77 simg1 = sitk.Cast(sitk.RescaleIntensity(fixed), sitk.sitkUInt8)
78 simg2 = sitk.Cast(sitk.RescaleIntensity(out), sitk.sitkUInt8)
79 # Use the // floor division operator so that the pixel type is
80 # the same for all three images which is the expectation for
81 # the compose filter.
82 cimg = sitk.Compose(simg1, simg2, simg1 // 2.0 + simg2 // 2.0)
83 sitk.Show(cimg, "DeformableRegistration1 Composition")
Deformably register two images using the demons algorithm.
A dense deformable transform over a bounded spatial domain for 2D or 3D coordinates space.
Normalize the grayscale values for a source image by matching the shape of the source image histogram...
Resample an image via a coordinate transform.
SITKIO_EXPORT Image ReadImage(const PathType &filename, PixelIDValueEnum outputPixelType=sitkUnknown, const std::string &imageIO="")
ReadImage is a procedural interface to the ImageFileReader class which is convenient for most image r...
Image Compose(const std::vector< Image > &images)
ComposeImageFilter combine several scalar images into a multicomponent image.
void SITKIO_EXPORT Show(const Image &image, const std::string &title="", const bool debugOn=ProcessObject::GetGlobalDefaultDebug())
SITKCommon_EXPORT void WriteTransform(const Transform &transform, const PathType &filename)
Image RescaleIntensity(Image &&image1, double outputMinimum=0, double outputMaximum=255)
Applies a linear transformation to the intensity levels of the input Image .
Image Cast(const Image &image, PixelIDValueEnum pixelID)