DemonsRegistration1

Overview

This example illustrates how to use the classic Demons registration algorithm. The user supplied parameters for the algorithm are the number of iterations and the standard deviations for the Gaussian smoothing of the total displacement field. Additional methods which control regularization, total field smoothing for elastic model or update field smoothing for viscous model are available.

The underlying assumption of the demons framework is that the intensities of homologous points are equal. The example uses histogram matching to make the two images similar prior to registration. This is relevant for registration of MR images where the assumption is not valid. For other imaging modalities where the assumption is valid, such as CT, this step is not necessary. Additionally, the command design pattern is used to monitor registration progress. The resulting deformation field is written to file.

See also: DemonsRegistration2.

Code

// A SimpleITK example demonstrating the classic Demons image registration.

using System;
using itk.simple;

namespace itk.simple.examples
{
    public class DemonsRegistration1
    {
        public class IterationUpdate : Command
        {
            private DemonsRegistrationFilter filter;
            public IterationUpdate(DemonsRegistrationFilter filter)
            {
                this.filter = filter;
            }
            public override void Execute()
            {
                Console.WriteLine(string.Format("{0,3} = {1,10:F5}", filter.GetElapsedIterations(), filter.GetMetric()));
            }
        }

        public static void Main(string[] args)
        {
            if (args.Length < 3)
            {
                Console.WriteLine("Usage: DemonsRegistration1 <fixedImageFile> <movingImageFile> <outputTransformFile>");
                return;
            }

            var fixedImage = SimpleITK.ReadImage(args[0], PixelIDValueEnum.sitkFloat32);
            var movingImage = SimpleITK.ReadImage(args[1], PixelIDValueEnum.sitkFloat32);

            // Perform histogram matching of the moving image to the fixed image
            var matcher = new HistogramMatchingImageFilter();
            matcher.SetNumberOfHistogramLevels(1024);
            matcher.SetNumberOfMatchPoints(7);
            matcher.ThresholdAtMeanIntensityOn();
            movingImage = matcher.Execute(movingImage, fixedImage);

            // The basic Demons Registration Filter
            // Note there is a whole family of Demons Registration algorithms included in SimpleITK
            var demons = new DemonsRegistrationFilter();
            demons.SetNumberOfIterations(50);
            // Standard deviation for Gaussian smoothing of displacement field
            demons.SetStandardDeviations(1.0);

            // Add iteration callback
            var cmd = new IterationUpdate(demons);
            demons.AddCommand(EventEnum.sitkIterationEvent, cmd);

            var displacementField = demons.Execute(fixedImage, movingImage);

            Console.WriteLine("-------");
            Console.WriteLine(string.Format("Number Of Iterations: {0}", demons.GetElapsedIterations()));
            Console.WriteLine(string.Format(" RMS: {0}", demons.GetRMSChange()));

            // Create and write the displacement field transform
            var outTx = new DisplacementFieldTransform(displacementField);
            SimpleITK.WriteTransform(outTx, args[2]);
        }
    }
}