Image Registration Method 1

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', 'displaceMeth1.hdf5')

produces the text and images below.

Output Text

Text Output (click triangle to collapse)
  0 = 4499.45303 : (2.928695951245586, 2.7244705953923805)
  1 = 3860.83626 : (6.135143776902402, 5.115849348610004)
  2 = 3508.02147 : (8.822660051952475, 8.078492808653918)
  3 = 3117.30771 : (10.968558473732326, 11.454158663474674)
  4 = 2125.42619 : (13.105290365964755, 14.835634202454191)
  5 =  911.30826 : (12.75173580401588, 18.819978461140323)
  6 =  741.41676 : (13.139053510563274, 16.857840597942413)
  7 =   16.89176 : (12.356787624301035, 17.480785285045815)
  8 =  233.71446 : (12.79212443526829, 17.234854683011704)
  9 =   39.80270 : (13.167510875734614, 16.904574468172815)
 10 =   16.57314 : (12.938831371165355, 17.005597654570586)
 11 =    1.68763 : (13.063495692092735, 16.996443033457986)
 12 =    1.79437 : (13.001061362657559, 16.999307384689935)
 13 =    0.00076 : (12.945418587211314, 17.0277701944711)
 14 =    1.74802 : (12.974454390534774, 17.01621663980765)
 15 =    0.43025 : (13.002439510423766, 17.002309966416835)
 16 =    0.00532 : (12.989877586882951, 16.99301810428082)
-------
itk::simple::TranslationTransform
 TranslationTransform (0x5639ff927c90)
   RTTI typeinfo:   itk::TranslationTransform<double, 2u>
   Reference Count: 2
   Modified Time: 2526
   Debug: Off
   Object Name: 
   Observers: 
     none
   Offset: [12.9899, 16.993]

Optimizer stop condition: RegularStepGradientDescentOptimizerv4: Step too small after 17 iterations. Current step (0.0078125) is less than minimum step (0.01).
 Iteration: 18
 Metric value: 0.07213459794461137

Input Images

_images/ImageRegistrationMethod1_fixed.png

Fixed Image

_images/ImageRegistrationMethod1_moving.png

Moving Image

Output Image

_images/ImageRegistrationMethod1_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 ImageRegistrationMethod1 {

    static void Main(string[] args) {

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

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

      reader.SetFileName(args[0]);
      Image fixedImage = reader.Execute();

      reader.SetFileName(args[1]);
      Image movingImage = reader.Execute();

      ImageRegistrationMethod R = new  ImageRegistrationMethod();
      R.SetMetricAsMeanSquares();
      double maxStep = 4.0;
      double minStep = 0.01;
      uint numberOfIterations = 200;
      double relaxationFactor = 0.5;
      R.SetOptimizerAsRegularStepGradientDescent( maxStep,
                                                  minStep,
                                                  numberOfIterations,
                                                  relaxationFactor );
      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 );

      // System.out.println("-------");
      // System.out.println(outTx.toString());
      // System.out.format("Optimizer stop condition: %s\n", R.getOptimizerStopConditionDescription());
      // System.out.format(" Iteration: %d\n", R.getOptimizerIteration());
      // System.out.format(" Metric value: %f\n", R.getMetricValue());

      outTx.WriteTransform(args[2]);

    }

  }

}