SimpleITK  
LandmarkRegistration/LandmarkRegistration.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 landmark registration. """
21 
22 import sys
23 import SimpleITK as sitk
24 
25 # Black image with a small white square in it.
26 fixed_image = sitk.Image(100, 100, sitk.sitkUInt8)
27 fixed_image[11:20, 11:20] = 200
28 
29 # Black image with a small grey square at a different location.
30 moving_image = sitk.Image(100, 100, sitk.sitkUInt8)
31 moving_image[51:60, 51:60] = 69
32 
33 
34 # Landmarks are 3 corners of the squares.
35 # 3 (X, Y) pairs are flattened into 1-d lists.
36 fixed_landmarks = [10, 10, 20, 10, 20, 20]
37 moving_landmarks = [50, 50, 60, 50, 60, 60]
38 
39 # Set up the LandmarkBasedTransformInitializerFilter.
40 landmark_initializer = sitk.LandmarkBasedTransformInitializerFilter()
41 
42 landmark_initializer.SetFixedLandmarks(fixed_landmarks)
43 landmark_initializer.SetMovingLandmarks(moving_landmarks)
44 
45 transform = sitk.Euler2DTransform()
46 
47 # Compute the transform.
48 output_transform = landmark_initializer.Execute(transform)
49 
50 print(output_transform)
51 
52 
53 # Resample the transformed moving image onto the fixed image.
54 output_image = sitk.Resample(
55  moving_image, fixed_image, transform=output_transform, defaultPixelValue=150
56 )
57 
58 # Write the resampled image.
59 sitk.WriteImage(output_image, "landmark_example.png")
60 
61 
62 # Write the transform.
63 if len(sys.argv) > 1:
64  out_name = sys.argv[1]
65 else:
66  out_name = "landmark_transform.tfm"
67 
68 sitk.WriteTransform(output_transform, out_name)
itk::simple::Image
The Image class for SimpleITK.
Definition: sitkImage.h:76
itk::simple::Euler2DTransform
A rigid 2D transform with rotation in radians around a fixed center with translation.
Definition: sitkEuler2DTransform.h:33
itk::simple::WriteTransform
SITKCommon_EXPORT void WriteTransform(const Transform &transform, const PathType &filename)
itk::simple::Resample
Image Resample(const Image &image1, const std::vector< uint32_t > &size, Transform transform=itk::simple::Transform(), InterpolatorEnum interpolator=itk::simple::sitkLinear, const std::vector< double > &outputOrigin=std::vector< double >(3, 0.0), const std::vector< double > &outputSpacing=std::vector< double >(3, 1.0), const std::vector< double > &outputDirection=std::vector< double >(), double defaultPixelValue=0.0, PixelIDValueEnum outputPixelType=sitkUnknown, bool useNearestNeighborExtrapolator=false)
itk::simple::ResampleImageFilter Procedural Interface
itk::simple::WriteImage
SITKIO_EXPORT void WriteImage(const Image &image, const std::vector< PathType > &fileNames, bool useCompression=false, int compressionLevel=-1)
WriteImage is a procedural interface to the ImageSeriesWriter. class which is convenient for many ima...
itk::simple::LandmarkBasedTransformInitializerFilter
Definition: sitkLandmarkBasedTransformInitializerFilter.h:73