Image Registration Method 4

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

produces the text and images below.

Output Text

Text Output (click triangle to collapse)
  0 =   -0.33292 : (0.6974697291629768, 0.7166142455333441)
  1 =   -0.34712 : (1.3826576924988139, 1.4449805372435578)
  2 =   -0.35632 : (2.0209454146564374, 2.2147784222385947)
  3 =   -0.36772 : (2.817775466755714, 2.818981921129046)
  4 =   -0.37974 : (3.5412552846345484, 3.5093274528173515)
  5 =   -0.38915 : (4.17262694459711, 4.284807837479736)
  6 =   -0.39826 : (4.842158034704644, 5.027591870277573)
  7 =   -0.41073 : (5.618124855519715, 5.658365595946415)
  8 =   -0.42544 : (6.331395364335284, 6.359254446802225)
  9 =   -0.43789 : (6.7942950248163205, 7.245665132842585)
 10 =   -0.45431 : (7.544337926019115, 7.907054312036874)
 11 =   -0.47388 : (8.343405644945726, 8.508295345707584)
 12 =   -0.49189 : (9.145493728722611, 9.105501094058289)
 13 =   -0.50712 : (9.671166611523828, 9.956187887353833)
 14 =   -0.53050 : (10.096065490788998, 10.861428707467024)
 15 =   -0.56185 : (10.777109118944601, 11.593671547342328)
 16 =   -0.60354 : (11.363211584918258, 12.40390848761326)
 17 =   -0.65763 : (11.979663861447968, 13.191300760093336)
 18 =   -0.70627 : (12.22968203140312, 14.159541905001088)
 19 =   -0.76821 : (12.698968023419425, 15.04258814257717)
 20 =   -0.86157 : (12.84540596340077, 16.031808001725793)
 21 =   -1.05343 : (13.020465889206477, 17.01636578183669)
 22 =   -1.41660 : (12.55461144775009, 16.834762810012556)
 23 =   -1.21739 : (12.795523061525898, 16.9015579771127)
 24 =   -1.33854 : (13.034695058197899, 16.974336792766216)
 25 =   -1.41243 : (12.91947867112382, 17.022815490661227)
 26 =   -1.40205 : (12.979156568674075, 17.004246738845843)
 27 =   -1.41816 : (13.037911572290357, 16.982937127163535)
 28 =   -1.41256 : (13.007795502562258, 16.99127888022463)
 29 =   -1.41815 : (12.978281849358487, 17.00155052929221)
 30 =   -1.41813 : (12.993525321663073, 16.99811874009424)
 31 =   -1.41888 : (12.985739704862503, 16.99876629494496)
 32 =   -1.41869 : (12.989615523718104, 16.99827965620722)
 33 =   -1.41883 : (12.99350408514837, 16.997908335502775)
 34 =   -1.41888 : (12.99159061165701, 16.99829989196548)
-------
itk::simple::TranslationTransform
 TranslationTransform (0x55ca4f128560)
   RTTI typeinfo:   itk::TranslationTransform<double, 2u>
   Reference Count: 2
   Modified Time: 13969
   Debug: Off
   Object Name: 
   Observers: 
     none
   Offset: [12.9916, 16.9983]

Optimizer stop condition: RegularStepGradientDescentOptimizerv4: Step too small after 35 iterations. Current step (0.000976562) is less than minimum step (0.001).
 Iteration: 36
 Metric value: -1.4188673781593846

Input Images

_images/ImageRegistrationMethod4_fixed.png

Fixed Image

_images/ImageRegistrationMethod4_moving.png

Moving Image

Output Image

_images/ImageRegistrationMethod4_composition.png

Composition Image

Code

#!/usr/bin/env python

import SimpleITK as sitk
import sys
import os


def command_iteration(method):
    print(
        f"{method.GetOptimizerIteration():3} "
        + f"= {method.GetMetricValue():10.5f} "
        + f": {method.GetOptimizerPosition()}"
    )


def main(args):
    if len(args) < 3:
        print(
            "Usage:",
            "ImageRegistrationMethod4",
            "<fixedImageFilter> <movingImageFile>",
            "<outputTransformFile> <numberOfBins> <samplingPercentage>",
        )
        sys.exit(1)

    fixed = sitk.ReadImage(args[1], sitk.sitkFloat32)
    moving = sitk.ReadImage(args[2], sitk.sitkFloat32)

    numberOfBins = 24
    samplingPercentage = 0.10

    if len(args) > 4:
        numberOfBins = int(args[4])
    if len(args) > 5:
        samplingPercentage = float(args[5])

    R = sitk.ImageRegistrationMethod()
    R.SetMetricAsMattesMutualInformation(numberOfBins)
    R.SetMetricSamplingPercentage(samplingPercentage, sitk.sitkWallClock)
    R.SetMetricSamplingStrategy(R.RANDOM)
    R.SetOptimizerAsRegularStepGradientDescent(1.0, 0.001, 200)
    R.SetInitialTransform(sitk.TranslationTransform(fixed.GetDimension()))
    R.SetInterpolator(sitk.sitkLinear)

    R.AddCommand(sitk.sitkIterationEvent, lambda: command_iteration(R))

    outTx = R.Execute(fixed, moving)

    print("-------")
    print(outTx)
    print(f"Optimizer stop condition: {R.GetOptimizerStopConditionDescription()}")
    print(f" Iteration: {R.GetOptimizerIteration()}")
    print(f" Metric value: {R.GetMetricValue()}")

    sitk.WriteTransform(outTx, args[3])

    resampler = sitk.ResampleImageFilter()
    resampler.SetReferenceImage(fixed)
    resampler.SetInterpolator(sitk.sitkLinear)
    resampler.SetDefaultPixelValue(100)
    resampler.SetTransform(outTx)

    out = resampler.Execute(moving)
    simg1 = sitk.Cast(sitk.RescaleIntensity(fixed), sitk.sitkUInt8)
    simg2 = sitk.Cast(sitk.RescaleIntensity(out), sitk.sitkUInt8)
    cimg = sitk.Compose(simg1, simg2, simg1 // 2.0 + simg2 // 2.0)

    return {"fixed": fixed,
            "moving": moving,
            "composition": cimg}