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
22import sys
23import SimpleITK as sitk
24
25# Black image with a small white square in it.
26fixed_image = sitk.Image(100, 100, sitk.sitkUInt8)
27fixed_image[11:20, 11:20] = 200
28
29# Black image with a small grey square at a different location.
30moving_image = sitk.Image(100, 100, sitk.sitkUInt8)
31moving_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.
36fixed_landmarks = [10, 10, 20, 10, 20, 20]
37moving_landmarks = [50, 50, 60, 50, 60, 60]
38
39# Set up the LandmarkBasedTransformInitializerFilter.
41
42landmark_initializer.SetFixedLandmarks(fixed_landmarks)
43landmark_initializer.SetMovingLandmarks(moving_landmarks)
44
45transform = sitk.Euler2DTransform()
46
47# Compute the transform.
48output_transform = landmark_initializer.Execute(transform)
49
50print(output_transform)
51
52
53# Resample the transformed moving image onto the fixed image.
54output_image = sitk.Resample(
55 moving_image, fixed_image, transform=output_transform, defaultPixelValue=150
56)
57
58# Write the resampled image.
59sitk.WriteImage(output_image, "landmark_example.png")
60
61
62# Write the transform.
63if len(sys.argv) > 1:
64 out_name = sys.argv[1]
65else:
66 out_name = "landmark_transform.tfm"
67
68sitk.WriteTransform(output_transform, out_name)
A rigid 2D transform with rotation in radians around a fixed center with translation.
The Image class for SimpleITK.
Definition sitkImage.h:77
SITKBasicFilters_EXPORT Image Resample(const Image &image1, Transform transform=itk::simple::Transform(), InterpolatorEnum interpolator=itk::simple::sitkLinear, double defaultPixelValue=0.0, PixelIDValueEnum outputPixelType=sitkUnknown, bool useNearestNeighborExtrapolator=false)
itk::simple::ResampleImageFilter Procedural Interface
SITKIO_EXPORT void WriteImage(const Image &image, const PathType &fileName, bool useCompression=false, int compressionLevel=-1)
WriteImage is a procedural interface to the ImageFileWriter. class which is convenient for many image...
SITKCommon_EXPORT void WriteTransform(const Transform &transform, const PathType &filename)