Landmark Registration

If you are not familiar with the SimpleITK registration framework we recommend that you read the registration overview before continuing with the example.

Overview

Code

#!/usr/bin/env python

import sys
import SimpleITK as sitk

# Black image with a small white square in it.
fixed_image = sitk.Image(100, 100, sitk.sitkUInt8)
fixed_image[11:20, 11:20] = 200

# Black image with a small grey square at a different location.
moving_image = sitk.Image(100, 100, sitk.sitkUInt8)
moving_image[51:60, 51:60] = 69


# Landmarks are 3 corners of the squares.
# 3 (X, Y) pairs are flattened into 1-d lists.
fixed_landmarks = [10, 10, 20, 10, 20, 20]
moving_landmarks = [50, 50, 60, 50, 60, 60]

# Set up the LandmarkBasedTransformInitializerFilter.
landmark_initializer = sitk.LandmarkBasedTransformInitializerFilter()

landmark_initializer.SetFixedLandmarks(fixed_landmarks)
landmark_initializer.SetMovingLandmarks(moving_landmarks)

transform = sitk.Euler2DTransform()

# Compute the transform.
output_transform = landmark_initializer.Execute(transform)

print(output_transform)


# Resample the transformed moving image onto the fixed image.
output_image = sitk.Resample(
    moving_image, fixed_image, transform=output_transform, defaultPixelValue=150
)

# Write the resampled image.
sitk.WriteImage(output_image, "landmark_example.png")


# Write the transform.
if len(sys.argv) > 1:
    out_name = sys.argv[1]
else:
    out_name = "landmark_transform.tfm"

sitk.WriteTransform(output_transform, out_name)