Image Registration Method 2

Overview

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

Example Run

Running the Python code with the following inputs:

main('BrainProtonDensitySliceBorder20.png', 'BrainProtonDensitySliceShifted13x17y.png', 'displaceMeth2.hdf5')

produces the text and images below.

Output Text

Text Output (click triangle to collapse)
  0  = -0.49019  : (0.0028307206468947536, 0.0023491904331766162)
  1  = -0.49030  : (0.015968780135700314, 0.01326142597839447)
  2  = -0.49068  : (0.08103357252207889, 0.06751733183069583)
  3  = -0.49262  : (0.4031918173866258, 0.3356312129926553)
  4  = -0.50145  : (1.9891643379821866, 1.6551894820472324)
  5  = -0.53055  : (10.002028651827025, 8.700643712668963)
  6  = -0.71953  : (14.301161173235505, 16.02941144181758)
  7  = -1.14334  : (12.934080126605934, 16.847083218629955)
  8  = -1.28582  : (13.021228858202821, 16.980930809778222)
  9  = -1.29232  : (13.010721376329036, 17.001152119792206)
 10  = -1.29329  : (13.011578940166988, 17.000992823315222)
 11  = -1.29327  : (13.01231557664238, 17.000844562218692)
 12  = -1.29324  : (13.012426459622057, 17.000820248130232)
 13  = -1.29323  : (13.012465052549832, 17.000811591622085)
-------
itk::simple::TranslationTransform
 TranslationTransform (0x559d400c0180)
   RTTI typeinfo:   itk::TranslationTransform<double, 2u>
   Reference Count: 2
   Modified Time: 25514
   Debug: Off
   Object Name: 
   Observers: 
     none
   Offset: [13.0116, 17.001]

Optimizer stop condition: GradientDescentLineSearchOptimizerv4Template: Convergence checker passed at iteration 14.
 Iteration: 14
 Metric value: -1.2932878917340445

Input Images

_images/ImageRegistrationMethod2_fixed.png

Fixed Image

_images/ImageRegistrationMethod2_moving.png

Moving Image

Output Image

_images/ImageRegistrationMethod2_composition.png

Composition Image

Code

using System;
using itk.simple;

namespace itk.simple.examples
{


    class IterationUpdate : Command
    {

        private ImageRegistrationMethod m_Method;

        public IterationUpdate(ImageRegistrationMethod m)
        {
            m_Method = m;
        }

        public override void Execute()
        {
            VectorDouble pos = m_Method.GetOptimizerPosition();
            Console.WriteLine("{0:3} = {1:10.5} : [{2}, {3}]",
                              m_Method.GetOptimizerIteration(),
                              m_Method.GetMetricValue(),
                              pos[0], pos[1]);
        }
    }

    class ImageRegistrationMethod2
    {

        static void Main(string[] args)
        {

            if (args.Length < 3)
            {
                Console.WriteLine("Usage: %s <fixedImageFilter> <movingImageFile> <outputTransformFile>\n", "ImageRegistrationMethod2");
                return;
            }

            ImageFileReader reader = new ImageFileReader();
            reader.SetOutputPixelType(PixelIDValueEnum.sitkFloat32);

            reader.SetFileName(args[0]);
            Image fixedImage = reader.Execute();
            fixedImage = SimpleITK.Normalize(fixedImage);
            SimpleITK.DiscreteGaussian(fixedImage, 2.0);

            reader.SetFileName(args[1]);
            Image movingImage = reader.Execute();
            movingImage=SimpleITK.Normalize(movingImage);
            movingImage = SimpleITK.DiscreteGaussian(movingImage, 2.0);

            ImageRegistrationMethod R = new ImageRegistrationMethod();
            R.SetMetricAsJointHistogramMutualInformation();

            double learningRate = 1;
            uint  numberOfIterations = 200;
            double convergenceMinimumValue = 1e-4;
            uint convergenceWindowSize = 5;
            
            R.SetOptimizerAsGradientDescentLineSearch(learningRate,
                                                      numberOfIterations,
                                                      convergenceMinimumValue,
                                                      convergenceWindowSize);

            R.SetInitialTransform(new TranslationTransform(fixedImage.GetDimension()));
            R.SetInterpolator(InterpolatorEnum.sitkLinear);

            IterationUpdate cmd = new IterationUpdate(R);
            R.AddCommand(EventEnum.sitkIterationEvent, cmd);

            Transform outTx = R.Execute(fixedImage, movingImage);

            outTx.WriteTransform(args[2]);

        }

    }

}