Registration and Transformation

Overview

This example demonstrates image registration and transformation using SimpleElastix. SimpleElastix provides access to the powerful elastix registration toolbox through SimpleITK’s ElastixImageFilter and TransformixImageFilter classes.

This example includes two programs:

  1. elx - Performs image registration using ElastixImageFilter

  2. tfx - Applies transformations using TransformixImageFilter

The registration workflow typically consists of:

  1. Reading fixed and moving images

  2. Loading or creating a parameter map that defines the registration strategy

  3. Executing the registration to obtain a registered image and transform parameters

  4. Optionally applying the transform to other images using Transformix

Parameter maps can be loaded from text files, created using default presets, or customized programmatically. The elastix parameter files use a simple text format that allows fine-grained control over the registration process.

Code

Registration (elx)

using System;
using itk.simple;

namespace itk.simple.examples
{
    class Elastix
    {
        static void Main(string[] args)
        {
            if (args.Length < 5)
            {
                Console.WriteLine("Usage: elx <fixedImage> <movingImage> <parameterFile> <outputImage> <outputParameterFile>");
                return;
            }

            // Instantiate SimpleElastix
            ElastixImageFilter elastixImageFilter = new ElastixImageFilter();

            // Read input
            elastixImageFilter.SetFixedImage(SimpleITK.ReadImage(args[0]));
            elastixImageFilter.SetMovingImage(SimpleITK.ReadImage(args[1]));
            elastixImageFilter.SetParameterMap(SimpleITK.ReadParameterFile(args[2]));
            elastixImageFilter.LogToConsoleOn();

            // Run registration
            elastixImageFilter.Execute();

            // Write result image
            SimpleITK.WriteImage(elastixImageFilter.GetResultImage(), args[3]);

            // Write parameter file. This example only supports one parameter map and one transform parameter map.
            SimpleITK.WriteParameterFile(elastixImageFilter.GetTransformParameterMaps()[0], args[4]);
        }
    }
}

Transformation (tfx)

using System;
using itk.simple;

namespace itk.simple.examples
{
    class Transformix
    {
        static void Main(string[] args)
        {
            if (args.Length < 3)
            {
                Console.WriteLine("Usage: tfx <inputImage> <parameterFile> <outputImage>");
                return;
            }

            // Instantiate transformix
            TransformixImageFilter transformixImageFilter = new TransformixImageFilter();
            transformixImageFilter.LogToConsoleOn();

            // Read input
            transformixImageFilter.SetMovingImage(SimpleITK.ReadImage(args[0]));
            transformixImageFilter.SetTransformParameterMap(SimpleITK.ReadParameterFile(args[1]));

            // Run warp
            transformixImageFilter.Execute();

            // Write result image
            SimpleITK.WriteImage(transformixImageFilter.GetResultImage(), args[2]);
        }
    }
}