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