Mutual Information Metric#

The MutualInformationImageToImageMetric class computes the mutual information between two images, i.e. the degree to which information content in one image is dependent on the other image. This example shows how MutualInformationImageToImageMetric can be used to map affine transformation parameters and register two images using a gradient ascent algorithm.

[1]:
import os
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from urllib.request import urlretrieve

import itk
from itkwidgets import compare, checkerboard

Retrieve fixed and moving images for registration#

We aim to register two slice images, one of which has an arbitrary offset and rotation. We seek to use an affine transform to appropriately rotate and translate the moving image to register with the fixed image.

[2]:
fixed_image_path = "fixed.png"
moving_image_path = "moving.png"

[3]:
if not os.path.exists(fixed_image_path):
    url = "https://data.kitware.com/api/v1/file/602c10a22fa25629b97d2896/download"
    urlretrieve(url, fixed_image_path)
if not os.path.exists(moving_image_path):
    url = "https://data.kitware.com/api/v1/file/602c10a32fa25629b97d28a0/download"
    urlretrieve(url, moving_image_path)

[4]:
fixed_image = itk.imread(fixed_image_path, itk.F)
moving_image = itk.imread(moving_image_path, itk.F)

[5]:
checkerboard(fixed_image, moving_image)

Prepare images for registration#

[6]:
ImageType = type(fixed_image)

[7]:
fixed_normalized_image = itk.normalize_image_filter(fixed_image)
fixed_smoothed_image = itk.discrete_gaussian_image_filter(fixed_normalized_image, variance=2.0)

moving_normalized_image = itk.normalize_image_filter(moving_image)
moving_smoothed_image = itk.discrete_gaussian_image_filter(moving_normalized_image, variance=2.0)

[8]:
compare(fixed_smoothed_image, moving_smoothed_image)

Plot the MutualInformationImageToImageMetric surface#

For this relatively simple example we seek to adjust only the x- and y-offset of the moving image with a TranslationTransform. We can acquire MutualInformationImageToImageMetric values comparing the two images at many different possible offset pairs with ExhaustiveOptimizer and visualize this data set as a surface with matplotlib.

The affine transform contains six parameters representing each element in an affine matrix A which will dictate how the moving image is sampled. We know that the moving image has been translated so we will visualize the two translation parameters, but we could set X_INDEX and Y_INDEX to visualize any pair of parameters. See https://en.wikipedia.org/wiki/Affine_transformation#Image_transformation for more information on affine transformations.

[9]:
X_INDEX = 4  # Translation in the X direction
Y_INDEX = 5  # Translation in the Y direction

[10]:
# Move at most 20 pixels away from the initial position
window_size = [0] * 6
window_size[X_INDEX] = 20  # Set lower if visualizing elements 0-3
window_size[Y_INDEX] = 20  # Set lower if visualizing elements 0-3

# Collect 50 steps of data along each axis
n_steps = [0] * 6
n_steps[X_INDEX] = 50
n_steps[Y_INDEX] = 50

[11]:
dim = fixed_image.GetImageDimension()

TransformType = itk.AffineTransform[itk.D, dim]
transform = TransformType.New()

[12]:
InterpolatorType = itk.LinearInterpolateImageFunction[ImageType, itk.D]
interpolator = InterpolatorType.New()

[13]:
MetricType = itk.MutualInformationImageToImageMetric[ImageType, ImageType]
metric = MetricType.New()

metric.SetNumberOfSpatialSamples(100)
metric.SetFixedImageStandardDeviation(5.0)
metric.SetMovingImageStandardDeviation(5.0)

metric.ReinitializeSeed(121212)

[14]:
ExhaustiveOptimizerType = itk.ExhaustiveOptimizer
optimizer = ExhaustiveOptimizerType.New()

# Map out [n_steps] in each direction
optimizer.SetNumberOfSteps(n_steps)

# Move [window_size / n_steps] units with every step
scales = optimizer.GetScales()
scales.SetSize(6)

for i in range(0, 6):
    scales.SetElement(i, (window_size[i] / n_steps[i]) if n_steps[i] != 0 else 1)

optimizer.SetScales(scales)

[15]:
# Collect data describing the parametric surface with an observer
surface = dict()


def print_iteration():
    surface[tuple(optimizer.GetCurrentPosition())] = optimizer.GetCurrentValue()


optimizer.AddObserver(itk.IterationEvent(), print_iteration)

[15]:
0
[16]:
RegistrationType = itk.ImageRegistrationMethod[ImageType, ImageType]
registrar = RegistrationType.New()

registrar.SetFixedImage(fixed_smoothed_image)
registrar.SetMovingImage(moving_smoothed_image)
registrar.SetOptimizer(optimizer)
registrar.SetTransform(transform)
registrar.SetInterpolator(interpolator)
registrar.SetMetric(metric)

registrar.SetFixedImageRegion(fixed_image.GetBufferedRegion())
registrar.SetInitialTransformParameters(transform.GetParameters())

[17]:
registrar.Update()

[18]:
# Check the extreme positions within the observed window
max_position = list(optimizer.GetMaximumMetricValuePosition())
min_position = list(optimizer.GetMinimumMetricValuePosition())

max_val = optimizer.GetMaximumMetricValue()
min_val = optimizer.GetMinimumMetricValue()

print(max_position)
print(min_position)

[1.0, 0.0, 0.0, 1.0, -0.4, 13.200000000000001]
[1.0, 0.0, 0.0, 1.0, -16.0, -17.6]
[19]:
# Set up values for the plot
x_vals = [
    list(set([x[i] for x in surface.keys()])) for i in range(0, transform.GetNumberOfParameters())
]

for i in range(0, transform.GetNumberOfParameters()):
    x_vals[i].sort()

X, Y = np.meshgrid(x_vals[X_INDEX], x_vals[Y_INDEX])
Z = np.array([[surface[(1, 0, 0, 1, x0, x1)] for x1 in x_vals[X_INDEX]] for x0 in x_vals[Y_INDEX]])

[20]:
# Plot the surface as a 2D heat map
fig = plt.figure()

# Invert the y-axis to represent the image coordinate system
plt.gca().invert_yaxis()
ax = plt.gca()

surf = ax.scatter(X, Y, c=Z, cmap=cm.coolwarm)

# Mark extremes on the plot
ax.plot(max_position[X_INDEX], max_position[Y_INDEX], "k^")
ax.plot(min_position[X_INDEX], min_position[Y_INDEX], "kv")

[20]:
[<matplotlib.lines.Line2D at 0x2a56ed10d90>]
../../../../_images/src_Core_Transform_MutualInformationAffine_MutualInformationAffine_23_1.png
[21]:
# Plot the surface as a 3D scatter plot
fig = plt.figure()
ax = fig.gca(projection="3d")

surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm)

../../../../_images/src_Core_Transform_MutualInformationAffine_MutualInformationAffine_24_0.png

Follow gradient ascent#

Once we understand the shape of the parametric surface it is easier to visualize the gradient ascent algorithm. We see that there is some roughness to the surface, but it has a clear slope upwards. We want to maximize the mutual information between the two images in order to optimize registration. The results of gradient ascent optimization can be superimposed onto the matplotlib plot.

[22]:
transform = TransformType.New()
interpolator = InterpolatorType.New()

[23]:
metric = MetricType.New()

metric.SetNumberOfSpatialSamples(100)
metric.SetFixedImageStandardDeviation(5.0)
metric.SetMovingImageStandardDeviation(5.0)

metric.ReinitializeSeed(121212)

[24]:
n_iterations = 200

optimizer = itk.GradientDescentOptimizer.New()

optimizer.SetLearningRate(1.0)
optimizer.SetNumberOfIterations(n_iterations)
optimizer.MaximizeOn()

# Set scales so that the optimizer can take
# large steps along translation parameters,
# moderate steps along rotational parameters, and
# small steps along scale parameters
optimizer.SetScales([100, 0.5, 0.5, 100, 0.0001, 0.0001])

[25]:
descent_data = dict()
descent_data[0] = (1, 0, 0, 1, 0, 0)


def log_iteration():
    descent_data[optimizer.GetCurrentIteration() + 1] = tuple(optimizer.GetCurrentPosition())


optimizer.AddObserver(itk.IterationEvent(), log_iteration)

[25]:
0
[26]:
registrar = RegistrationType.New()
registrar.SetFixedImage(fixed_smoothed_image)
registrar.SetMovingImage(moving_smoothed_image)
registrar.SetTransform(transform)
registrar.SetInterpolator(interpolator)
registrar.SetMetric(metric)
registrar.SetOptimizer(optimizer)

registrar.SetFixedImageRegion(fixed_image.GetBufferedRegion())
registrar.SetInitialTransformParameters(transform.GetParameters())

[27]:
registrar.Update()

[28]:
print(f"Its: {optimizer.GetCurrentIteration()}")
print(f"Final Value: {optimizer.GetValue()}")
print(f"Final Position: {list(registrar.GetLastTransformParameters())}")

Its: 200
Final Value: 0.0007345867979511311
Final Position: [1.0029839517261592, -0.010245644582257242, -0.01641632700222059, 1.0032415646265085, 13.10655567089651, 12.761316915069711]
[29]:
descent_data

[29]:
{0: (1, 0, 0, 1, 0, 0),
 1: (0.999999965527849,
  -0.0016629375454765493,
  -0.0019468854493280953,
  1.0000030221155434,
  0.0070724728333067316,
  -0.03606097600487626),
 2: (1.0000158649938202,
  0.0017875012247273974,
  0.0014649178874578785,
  1.000018244613104,
  0.16180008682720395,
  0.11360707328765127),
 3: (1.0000425997042064,
  0.0032077425755804466,
  -0.0021841309405414124,
  1.0000041473195547,
  0.22605434788625947,
  0.0009694550761079751),
 4: (1.0000753342618283,
  0.008455524952988737,
  -0.00010225287048921733,
  1.0000301796832407,
  0.4832979753254426,
  0.13790535364603634),
 5: (1.0000557531623095,
  0.0016169386175090023,
  0.0001399946290998774,
  1.0000317304876585,
  0.3263146246837202,
  0.2067871716700091),
 6: (1.0000514289215043,
  0.0013991846942296771,
  0.0034409916360437867,
  1.0000603451418513,
  0.2547503259637926,
  0.32331389761150553),
 7: (1.0000390671368042,
  -0.00464084624417071,
  0.0020064603033397776,
  1.0000567323503309,
  0.0867925370379804,
  0.26824463962805783),
 8: (1.0000323035713792,
  -0.006548715918176186,
  0.008161116356050421,
  1.000107659902533,
  0.062976923187307,
  0.5532678365738305),
 9: (1.0000427625470925,
  0.0009210905481415264,
  0.012503459911795356,
  1.0001106920885776,
  0.24221370589392452,
  0.6564395759394801),
 10: (1.0000644916142147,
  0.01134327391351616,
  0.01223330202000162,
  1.000103016019471,
  0.5229809457512343,
  0.5915683378194321),
 11: (1.0000518070970936,
  0.009506212296739217,
  0.009755186043286826,
  1.0000965159502793,
  0.4786979296746874,
  0.5341487982419897),
 12: (1.0000632855970673,
  0.012466891904789178,
  0.012024568878491737,
  1.0000992466585101,
  0.5905852114954294,
  0.6268634183585254),
 13: (1.0000373290348534,
  0.01101344325538434,
  0.014841027517697895,
  1.000112473200331,
  0.4987265352644986,
  0.7418373068560897),
 14: (1.0000546866010733,
  0.015493937057581212,
  0.01666651384068458,
  1.000132765035817,
  0.6463833982580928,
  0.8080911676910822),
 15: (1.0000631867012741,
  0.01342235419293244,
  0.01928722841564486,
  1.0001677388801908,
  0.6313075016185806,
  0.9504706426545657),
 16: (1.0000658347903864,
  0.013760347690960288,
  0.020309308864496344,
  1.0001802914015183,
  0.6381874605855995,
  1.0074894422852676),
 17: (1.0001104781926822,
  0.02089549688220317,
  0.01754713377932798,
  1.0001855726881073,
  0.9287857148328786,
  0.9338002039563951),
 18: (1.0001122807183165,
  0.017763036125256645,
  0.020640107973677255,
  1.0002135832291803,
  0.8593389248830472,
  1.105353339897966),
 19: (1.000122821487329,
  0.018582231918642292,
  0.020335068820269128,
  1.0002246377256918,
  0.8666517882674349,
  1.1110164493119412),
 20: (1.0001273603009995,
  0.021960842879295686,
  0.021033407917090154,
  1.000232420518082,
  1.002845683269043,
  1.1684552062025277),
 21: (1.0001397514011425,
  0.024740142447823087,
  0.022433020183041844,
  1.000244763827763,
  1.129079120734455,
  1.2430975064681058),
 22: (1.0001223435989122,
  0.02167390074035541,
  0.02413304640059116,
  1.000261266375767,
  1.0284992108314137,
  1.3138294318612544),
 23: (1.0001231381137234,
  0.01882320833012633,
  0.025686464118271607,
  1.0002857117503006,
  0.9582982738986365,
  1.4011972381355713),
 24: (1.0001077910687561,
  0.016391824328981664,
  0.02561497988662803,
  1.0003018794849772,
  0.8709406445656998,
  1.4167659128852281),
 25: (1.0001088003263428,
  0.014991274941814237,
  0.02713568191592862,
  1.0003164948852543,
  0.833210537074188,
  1.505187689416831),
 26: (1.000112280135333,
  0.013455063290054583,
  0.027778099818430543,
  1.0003439451387963,
  0.765573189118558,
  1.5669164620080727),
 27: (1.0001450154081564,
  0.0177292068447402,
  0.03261127598071813,
  1.0003734817767191,
  0.9018893930559542,
  1.7629587392841095),
 28: (1.0001478929494814,
  0.014634770850063202,
  0.031209234963315156,
  1.000390997915687,
  0.8764704378125825,
  1.7600207879040424),
 29: (1.0001700260192306,
  0.023304792919655027,
  0.03334327221311678,
  1.000415861108279,
  1.1050624653061767,
  1.8286067547503744),
 30: (1.0002238314105383,
  0.03161445006384011,
  0.030894925266538815,
  1.00042125420657,
  1.48370570704743,
  1.753463644918086),
 31: (1.000267445438375,
  0.03519002134376226,
  0.03206699672260963,
  1.0004408361991857,
  1.637502178426234,
  1.8135444785593309),
 32: (1.0002925314787032,
  0.038194063131100985,
  0.03175449876971995,
  1.0004437954970606,
  1.8007953543880255,
  1.820369894479594),
 33: (1.0003377638901942,
  0.04502109006274137,
  0.034993001930344464,
  1.0004917167239793,
  2.0943103237531275,
  2.0200348747122416),
 34: (1.0003803879833888,
  0.04979456747358726,
  0.037166416447014544,
  1.0005113084616988,
  2.262996533539432,
  2.138944755461071),
 35: (1.0003964256083064,
  0.05117236577832922,
  0.036139025055172636,
  1.000517255314559,
  2.3653462187910086,
  2.149530830319958),
 36: (1.0004360747618182,
  0.05345144578045517,
  0.041353120222081374,
  1.000574529441919,
  2.506749317525062,
  2.4517033740702763),
 37: (1.0004800273917174,
  0.055476026763283504,
  0.03497869525349116,
  1.0005694882764171,
  2.699417361085486,
  2.2820352287186982),
 38: (1.0005206995135523,
  0.05935785756284043,
  0.03680599549073893,
  1.00059995447889,
  2.8314656220050343,
  2.379800641551389),
 39: (1.0005597309781562,
  0.0629519671554236,
  0.03286977985068564,
  1.0006061112248472,
  3.0482611975454437,
  2.300140204936005),
 40: (1.0005771303435242,
  0.06359760525832305,
  0.03084883069424145,
  1.0006276527065008,
  3.094388142005032,
  2.302490335848059),
 41: (1.0005531002842671,
  0.05406694461103779,
  0.03197192149481904,
  1.0006410504293661,
  2.8277726043789086,
  2.4080371348532528),
 42: (1.0005540699748965,
  0.0499425356669065,
  0.03146040367046596,
  1.000651531523379,
  2.7609765291414368,
  2.4179316449263375),
 43: (1.000610737465348,
  0.055348765756564236,
  0.033964422597629856,
  1.0006648447876705,
  3.0028005303581273,
  2.5357804191677906),
 44: (1.0006330623321382,
  0.0604441118756231,
  0.03541207386386212,
  1.0006892015344302,
  3.1729606591188624,
  2.6409718876715735),
 45: (1.0006442795064596,
  0.06054527728396184,
  0.034876033570904005,
  1.0006762494684076,
  3.2010553258318533,
  2.613460581514949),
 46: (1.000662149541532,
  0.0607445636408164,
  0.03813667098198882,
  1.0007125957729466,
  3.2382560865404764,
  2.770594834115738),
 47: (1.0006729481736498,
  0.060918231289935475,
  0.03233310861663754,
  1.0006972055901309,
  3.317069230559112,
  2.615928702093721),
 48: (1.0006671735013677,
  0.058603049816866676,
  0.031921242837147284,
  1.0007020569642333,
  3.2015939820683537,
  2.5920285219449837),
 49: (1.0006967794678154,
  0.062351381457950376,
  0.03219102024577858,
  1.0007204433036898,
  3.389920656675441,
  2.6347548227953634),
 50: (1.0007074104619513,
  0.06005071670104204,
  0.03276782470243937,
  1.0007309277353125,
  3.35628336706342,
  2.7091459084586655),
 51: (1.0007396923199825,
  0.061291160665742646,
  0.030129086972525133,
  1.0007287540495768,
  3.491521638128275,
  2.6836909479579494),
 52: (1.0007572355137908,
  0.05960947091978929,
  0.03198540406396382,
  1.000758633658917,
  3.5365386031695225,
  2.8431750698970517),
 53: (1.0007798845923912,
  0.06509119967730434,
  0.035952863624348,
  1.000789187579904,
  3.7494949981560985,
  3.0473717852738775),
 54: (1.000781473519786,
  0.060341197508327865,
  0.035722251156929986,
  1.0007937977676336,
  3.6918683782560695,
  3.1184458997851303),
 55: (1.0007872554502402,
  0.06040160758394653,
  0.03518786014563553,
  1.000800477687533,
  3.7135825408548624,
  3.1342223383341103),
 56: (1.0008145677249618,
  0.06452951906037036,
  0.03734410678688241,
  1.0008181568848529,
  3.935213218074109,
  3.3080838931206684),
 57: (1.0008140957881748,
  0.06490597049903526,
  0.037054637041254314,
  1.0008340727056613,
  3.9740722322538815,
  3.342867380839161),
 58: (1.0008180551607286,
  0.061866698794218127,
  0.03526112066702049,
  1.0008347154048034,
  3.8996560917193013,
  3.2997991235710646),
 59: (1.0008184915440024,
  0.05810194800622753,
  0.032078749472733536,
  1.0008174040415063,
  3.8702246971271292,
  3.275480122016447),
 60: (1.0008289755868198,
  0.05760769157075074,
  0.035245271559863914,
  1.000846651219139,
  3.902919247486544,
  3.47761867695573),
 61: (1.0008633410589363,
  0.05572871476066318,
  0.031553091525065226,
  1.0008579228538625,
  3.990726748079648,
  3.448824699625424),
 62: (1.0008784378417017,
  0.05266122941402723,
  0.03264443802534106,
  1.0009120469223736,
  3.978242611217881,
  3.600547321798405),
 63: (1.0008928890970574,
  0.0526558541734464,
  0.033503931295208476,
  1.0009317713690227,
  4.034332325489235,
  3.6886036941446476),
 64: (1.0009139729436916,
  0.055303340473382784,
  0.037179269502224506,
  1.0009491506310282,
  4.143989163518143,
  3.8492430670691165),
 65: (1.0009452007831592,
  0.05673317055934501,
  0.03769485457324463,
  1.0009815413572847,
  4.3005043187866026,
  3.9354929724543823),
 66: (1.000964661596496,
  0.056193617300998526,
  0.038228221343659385,
  1.0009985420720955,
  4.391974061573027,
  4.047226171582918),
 67: (1.0009489516390504,
  0.05131866362736038,
  0.034655435593925296,
  1.0009733373448018,
  4.292328227469166,
  3.9568703108886076),
 68: (1.0009413430480196,
  0.04894249095833897,
  0.03671924896600889,
  1.0010030560844039,
  4.25078789548804,
  4.118073144864641),
 69: (1.0009619851850104,
  0.04729950161388283,
  0.03709263153386867,
  1.0010230937192157,
  4.281357081635324,
  4.2287662477296895),
 70: (1.0009883423387873,
  0.048950062105423985,
  0.038046513712827364,
  1.0010597645316839,
  4.430992282208885,
  4.34743407298929),
 71: (1.0010137638710939,
  0.05336056386113549,
  0.03689130248075374,
  1.001072749055473,
  4.671966314989308,
  4.356481292663306),
 72: (1.0010035331560332,
  0.051521442717293024,
  0.03407454202526072,
  1.001069535774639,
  4.675948704401148,
  4.318943572622043),
 73: (1.0010133467070166,
  0.05025409278757773,
  0.03552744416230726,
  1.0010991581691187,
  4.706984724922817,
  4.469211006294377),
 74: (1.00103872421304,
  0.04799225072152847,
  0.034678317190555384,
  1.00111106699047,
  4.747261791561516,
  4.522030002434047),
 75: (1.0010537945165752,
  0.048916122961739084,
  0.03456239062299948,
  1.0011399296717824,
  4.893172777587763,
  4.678249007363964),
 76: (1.0010708116503542,
  0.04966257392911915,
  0.03151892711151409,
  1.0011406407015644,
  4.999013958146794,
  4.606774486089828),
 77: (1.0011074021693485,
  0.05330094656215735,
  0.02955549065724992,
  1.001148211132933,
  5.168830012564221,
  4.591695110628777),
 78: (1.0011180403178352,
  0.04911265606635972,
  0.028874066097180205,
  1.0011697779064674,
  5.096506430676856,
  4.655314137512003),
 79: (1.0011304572059416,
  0.048411502402000844,
  0.026947412173529944,
  1.0011677592034094,
  5.152398101773921,
  4.635063604691433),
 80: (1.0011534166529035,
  0.045533538723998894,
  0.0247009249692374,
  1.0011741027229923,
  5.167592764506425,
  4.649732789182321),
 81: (1.0011874751177516,
  0.04608734927115699,
  0.023052334706844004,
  1.0011910423241892,
  5.316733019992377,
  4.7044779053144365),
 82: (1.0012216530417395,
  0.051030729738921046,
  0.024090469721467323,
  1.0012143207041417,
  5.5741717336269865,
  4.84196772524597),
 83: (1.0012155910218057,
  0.047979595405993385,
  0.02060019659317837,
  1.001201637958086,
  5.571806837673036,
  4.7889465009998675),
 84: (1.0012275595939713,
  0.049104395383112634,
  0.01985796096916095,
  1.0012032163768907,
  5.640324279276126,
  4.803288119178803),
 85: (1.0012314523542247,
  0.050020319896353506,
  0.016917352746062826,
  1.001191677914485,
  5.66305474131709,
  4.696474927128242),
 86: (1.0012231554137863,
  0.0459316553389,
  0.0172195732646551,
  1.001202043874071,
  5.567941410443212,
  4.762817321161207),
 87: (1.0012255448411016,
  0.04368101466938556,
  0.01981615508009791,
  1.0012343996433413,
  5.527174476496044,
  4.925926300210495),
 88: (1.0012438685165126,
  0.04536241517724545,
  0.020819924588875043,
  1.001243630091461,
  5.64088706315316,
  5.008415816496351),
 89: (1.0012529681222695,
  0.04500445744378577,
  0.01825969037534871,
  1.0012471442579247,
  5.673998014080022,
  4.943213170012537),
 90: (1.0012466479207036,
  0.04301406713437193,
  0.02062079465068866,
  1.0012772145535302,
  5.64032722398762,
  5.0967382031312),
 91: (1.0012702065173769,
  0.03919037399383151,
  0.018848213311686898,
  1.0013013012804877,
  5.65402825580481,
  5.146696157604204),
 92: (1.0013122519844961,
  0.045587033304291084,
  0.01942914361498954,
  1.0013390244055498,
  5.9799874568084155,
  5.280096943144681),
 93: (1.0013238518956937,
  0.04726847057600322,
  0.01746131636518727,
  1.0013366753312876,
  6.059948204069908,
  5.243347406334709),
 94: (1.0013432211440945,
  0.05017160839262471,
  0.017760160273959325,
  1.0013385900967755,
  6.227867299852726,
  5.336827192070069),
 95: (1.00138194743515,
  0.05338162142606548,
  0.019636002898359105,
  1.0013632592399198,
  6.45888100317004,
  5.516433884078349),
 96: (1.001381582879274,
  0.049469137405552234,
  0.019300379997726096,
  1.0013737444800777,
  6.390238529153018,
  5.574935374902547),
 97: (1.0013613147845113,
  0.042036458090255586,
  0.017203201204209487,
  1.0013796450507715,
  6.26251297746609,
  5.597725239436271),
 98: (1.0013623974055619,
  0.03976736948692073,
  0.01941599991203298,
  1.0014089134131836,
  6.249321082440559,
  5.762854116216476),
 99: (1.00139726526146,
  0.04412004516156002,
  0.02573942288520286,
  1.0014562562514249,
  6.487622112286879,
  6.101108327904233),
 100: (1.0014010328638006,
  0.0379669742778653,
  0.024572631664173908,
  1.0014628283539142,
  6.383539464934111,
  6.150896352637763),
 101: (1.0014149262114136,
  0.041378435781837794,
  0.026972688564634934,
  1.0014742044756928,
  6.4906024381097795,
  6.2622292763551926),
 102: (1.0014482705832934,
  0.04645118396678878,
  0.02718414409075883,
  1.0014865399033572,
  6.738645539197731,
  6.332576271079053),
 103: (1.001441702134815,
  0.03742094561902583,
  0.023041360279085946,
  1.0014959164165806,
  6.614978071563363,
  6.3458020769269),
 104: (1.001470899238623,
  0.03996662305978739,
  0.022611364691556567,
  1.0015023160362937,
  6.838050131268682,
  6.410886597716072),
 105: (1.0014867384509618,
  0.04013115597118111,
  0.02211848469257116,
  1.0015260855565642,
  6.923321358271605,
  6.507334867695091),
 106: (1.0014829435376351,
  0.034595999005676836,
  0.019201735339162744,
  1.0015291577499232,
  6.884027415499893,
  6.5419377689552745),
 107: (1.001489794752776,
  0.03018596527246648,
  0.017791319153445343,
  1.0015427911719266,
  6.851726145955262,
  6.61478836386323),
 108: (1.0014715489793686,
  0.022633193203852528,
  0.019015449479443336,
  1.001570165378783,
  6.620603357920789,
  6.740712292523077),
 109: (1.001500162701251,
  0.026487924533173713,
  0.019990747350695937,
  1.0015989212740382,
  6.856752942813844,
  6.867842765389764),
 110: (1.0015509438817778,
  0.027242278092531422,
  0.020362279700267927,
  1.0016415012953963,
  7.027932705213258,
  7.005081363564973),
 111: (1.0015668815656489,
  0.029448817637910765,
  0.0232755679407571,
  1.0016699408100305,
  7.166489885024689,
  7.183240996636877),
 112: (1.001574210620731,
  0.02823845267609452,
  0.023834445539348315,
  1.0016775695764397,
  7.21961240167998,
  7.277345275352267),
 113: (1.0015797695672788,
  0.027537454115571566,
  0.024680652071500355,
  1.001706752910666,
  7.295887091777702,
  7.431088809402569),
 114: (1.0015876297889528,
  0.021349873406022995,
  0.028117663094964662,
  1.0017499399414185,
  7.238044790518791,
  7.706090230472631),
 115: (1.001637058151661,
  0.026778711088343902,
  0.018953030939650603,
  1.0017309729368442,
  7.644316425332046,
  7.464771623412593),
 116: (1.0016818510558196,
  0.032364129202914406,
  0.016941239211920995,
  1.0017314468890584,
  7.940030314212486,
  7.438644962181216),
 117: (1.0017071445778736,
  0.02628104344114241,
  0.013359352125705673,
  1.001750707808061,
  7.891921812119137,
  7.433340341929363),
 118: (1.00175125155169,
  0.028049322362313462,
  0.011754147243330158,
  1.0017767127016353,
  8.109698590771947,
  7.5255627143892365),
 119: (1.0017691132097544,
  0.02532885173191134,
  0.015227420589668899,
  1.0018401096480538,
  8.13860084006787,
  7.831474719459007),
 120: (1.0017546789884808,
  0.018737382269375946,
  0.008593788821656012,
  1.0018283305349545,
  8.038537145215951,
  7.670300782551979),
 121: (1.001769085060014,
  0.017198352238636284,
  0.0074900756827615276,
  1.0018508239437725,
  8.07146720613475,
  7.728350718696938),
 122: (1.00180308725956,
  0.017078727318170013,
  0.0026358144462596423,
  1.001852102837873,
  8.171412048709891,
  7.631328646149379),
 123: (1.0018450596501052,
  0.025174423895821592,
  0.003803416383942778,
  1.0018802855336497,
  8.508271553555526,
  7.7301143138514465),
 124: (1.001868716780902,
  0.027869945112617138,
  0.006898295442192711,
  1.0019098292467803,
  8.674463968869048,
  7.961878222634894),
 125: (1.0018975943961956,
  0.02555863942469917,
  0.007203020911408353,
  1.0019416902575486,
  8.719008720267837,
  8.086630186820488),
 126: (1.0019321075344925,
  0.03025701602610059,
  0.00977908516863384,
  1.0019896055669617,
  8.980513235319998,
  8.302325562149456),
 127: (1.0019434802840155,
  0.02641931134636337,
  0.006890119948983443,
  1.0020049643264584,
  9.013813156726519,
  8.331631036133144),
 128: (1.0019649443283094,
  0.02481934696813929,
  0.008700023842445207,
  1.0020214993877548,
  9.052787545162385,
  8.468835714061086),
 129: (1.001977466045828,
  0.025602368331860595,
  0.007656998926756687,
  1.0020228684827135,
  9.264991814102144,
  8.570812050566111),
 130: (1.0019880400527548,
  0.02366754833492131,
  0.004921442060685271,
  1.0020199403676828,
  9.2276099142676,
  8.492107473158196),
 131: (1.002012808656276,
  0.023067581783013637,
  0.0013147090602290477,
  1.0020134557439981,
  9.281250255733404,
  8.395547033640439),
 132: (1.0020142931848017,
  0.016937804173622506,
  0.0027107467356588116,
  1.0020422575863144,
  9.193813887839951,
  8.607520248931714),
 133: (1.0020566752649949,
  0.02470093527526523,
  0.0053171400098446986,
  1.0020894229051642,
  9.50803108334762,
  8.808864489497951),
 134: (1.0020671798303653,
  0.022780214040542982,
  0.004839560672355789,
  1.002108157932218,
  9.580582232353557,
  8.913717226204195),
 135: (1.0020545961823053,
  0.010831889106925218,
  0.0007168892147971983,
  1.0021025981677067,
  9.310006256558985,
  8.883731253770843),
 136: (1.0020819029166785,
  0.012982215474751485,
  0.0032605154650179467,
  1.0021524524449537,
  9.490352307116236,
  9.09744727805679),
 137: (1.002109180964236,
  0.015690414259106136,
  0.002035795364862914,
  1.0021686192761907,
  9.653761410090535,
  9.116800533432912),
 138: (1.0021347563864977,
  0.012775032716776897,
  0.002241634122103183,
  1.0022122853506348,
  9.706648627847356,
  9.301233114588095),
 139: (1.0021467645240343,
  0.012392837436219047,
  0.004974770308221652,
  1.002254247595866,
  9.808506194749773,
  9.517054086232006),
 140: (1.0021531263959906,
  0.011454319092641213,
  0.005968666978669034,
  1.0022691603170968,
  9.841185810692997,
  9.615961196728737),
 141: (1.0021636129379816,
  0.012860474410313537,
  0.00716679477386708,
  1.0022844451757713,
  9.9334776367762,
  9.736766019305797),
 142: (1.0021717067834552,
  0.0077128345348900906,
  0.00505315994133678,
  1.0022823628079773,
  9.858195652628702,
  9.724085749556073),
 143: (1.0021957169862827,
  0.00815006081402336,
  0.006964109380848566,
  1.0023104271672316,
  9.937004586752606,
  9.886346442920633),
 144: (1.0022227436436295,
  0.009124131247707395,
  0.004897834540664515,
  1.0023198603308,
  10.082870304596783,
  9.882098531290065),
 145: (1.002244660234744,
  0.010330327697804757,
  0.004619950757925363,
  1.0023376700099085,
  10.211732957604358,
  9.938269227639653),
 146: (1.0022509357955849,
  0.0045462724061707165,
  0.003207062851745687,
  1.0023499522238073,
  10.109823367661134,
  10.007119784592303),
 147: (1.0022897146001202,
  0.007009077859381334,
  0.001905690288132529,
  1.0023603969312305,
  10.249674556962448,
  10.004899338281737),
 148: (1.0023307339375538,
  0.011854440472387397,
  0.001395537500113279,
  1.0023836674565736,
  10.562623227085258,
  10.091395779709025),
 149: (1.0023491080827485,
  0.010023367509582841,
  0.0016006347717150862,
  1.002405795274947,
  10.569876775313933,
  10.170316462493894),
 150: (1.0023415302697172,
  0.005033589388035417,
  0.0030580317013123187,
  1.002433510493446,
  10.46334590307692,
  10.288912199920812),
 151: (1.002355187464412,
  0.007438562624094941,
  0.0014666261671097383,
  1.002449103175267,
  10.57800832871742,
  10.29096938129903),
 152: (1.0023779639797088,
  0.006611598905641159,
  -0.0009694616343345389,
  1.0024603191385222,
  10.647795929263976,
  10.268152947214265),
 153: (1.0023790503810388,
  -0.0009820093817523431,
  -0.0033278558507537173,
  1.002487805648212,
  10.501452276263736,
  10.317654090534338),
 154: (1.0023897385072735,
  0.0005008292798210276,
  -0.0044510083020785985,
  1.0024859716108288,
  10.634952019200377,
  10.335173135623029),
 155: (1.0024362890140468,
  0.004387502551718853,
  -0.0035224457675955027,
  1.0025140734756244,
  10.895892979562953,
  10.447644622501505),
 156: (1.0024466941909564,
  0.004699089211273155,
  -0.0003068668122849232,
  1.002561952379878,
  10.967113915372746,
  10.683013611641053),
 157: (1.0024554494000404,
  0.003423055595332401,
  -0.0014416670133380646,
  1.0025735090184824,
  11.011909110739651,
  10.732587967512332),
 158: (1.0024954148673397,
  0.011234636500195003,
  -0.0018110726266817893,
  1.002590733166248,
  11.402080297169924,
  10.84176600097245),
 159: (1.0025051015835722,
  0.00874200203332762,
  -0.004019833937020776,
  1.0025977394525125,
  11.406249926717942,
  10.823691690204441),
 160: (1.0025570245947704,
  0.014855113112736826,
  -0.006992764462789507,
  1.0026105810429808,
  11.72866922576604,
  10.794607122723155),
 161: (1.0025359563440432,
  0.009325001855811987,
  -0.006238214456332685,
  1.002630774106633,
  11.553168385270247,
  10.872059976477777),
 162: (1.0025637628994528,
  0.009866145652299908,
  -0.005634243638170224,
  1.0026501411813078,
  11.681333233623228,
  10.984178282247514),
 163: (1.0025683451519263,
  0.008205152159930028,
  -0.006566921600087174,
  1.0026515364772985,
  11.645487917927875,
  10.976362115703154),
 164: (1.0025708974079064,
  0.0036928939194679506,
  -0.005910005869683161,
  1.0026852235294212,
  11.549738995436346,
  11.130181727987276),
 165: (1.0025661456923456,
  -0.0019473377976114876,
  -0.0038312347685751404,
  1.002720535989897,
  11.45810100216064,
  11.331800447991817),
 166: (1.0026045020969392,
  0.0014724612997977986,
  -0.0029816498869544386,
  1.0027596099262128,
  11.671567624226059,
  11.44724569982194),
 167: (1.0026179188511732,
  -0.0009988831204435283,
  -0.007349765288631139,
  1.0027608479314447,
  11.767708714535903,
  11.397670115012215),
 168: (1.0026328177169597,
  -0.0012009088006586452,
  -0.005333752440411114,
  1.0028011026565333,
  11.837356199495847,
  11.54892238210282),
 169: (1.0026558099372855,
  0.0016127174005692564,
  -0.004173752289507825,
  1.0028209155926329,
  12.034539276742827,
  11.665876722964216),
 170: (1.0026812322471446,
  0.004288232446802556,
  -0.007083763040147426,
  1.0028234422345583,
  12.189257114560851,
  11.625397078585234),
 171: (1.0026727108133495,
  4.1514506145755824e-05,
  -0.006304579706448422,
  1.0028379936273555,
  12.100920153893794,
  11.711202289518422),
 172: (1.0026918374815659,
  -0.0002504564916615083,
  -0.010624145002741621,
  1.0028441568903979,
  12.206653464299775,
  11.680096121366361),
 173: (1.0027087934437167,
  -0.0006164357258983809,
  -0.011674723356247196,
  1.002850980752777,
  12.274434388035639,
  11.687624939643868),
 174: (1.0027027995977735,
  -0.0040625673991487084,
  -0.013456790525997592,
  1.0028523991610334,
  12.18493805988577,
  11.668671820259437),
 175: (1.002717948252488,
  -0.0019180976676498925,
  -0.012127969858136542,
  1.0028759159699396,
  12.271589527895541,
  11.75500739710454),
 176: (1.002768517000085,
  0.005535949234591564,
  -0.013487806812915414,
  1.0028906393693426,
  12.608494713594943,
  11.771240908075697),
 177: (1.0027568328451135,
  -0.003916800322814321,
  -0.011560820482554524,
  1.0029285915774904,
  12.357598239126128,
  11.953544412097122),
 178: (1.002813683361649,
  0.001961950109245625,
  -0.013304825374824164,
  1.0029393180800068,
  12.700242065509961,
  11.956347569140418),
 179: (1.00279789771569,
  -0.003193374892262785,
  -0.012880220347632582,
  1.0029579821002805,
  12.559326146592904,
  12.025573281023275),
 180: (1.002802586201974,
  -0.00440998412181167,
  -0.012365375707334546,
  1.0030022965984196,
  12.576081151667733,
  12.153860219616268),
 181: (1.0028171010961264,
  -0.004168638325610454,
  -0.01355736993851798,
  1.003008819069466,
  12.67004411668296,
  12.17220077955086),
 182: (1.0028336261164101,
  -0.004659945960625215,
  -0.013078205409303219,
  1.0030229419991026,
  12.731497110624838,
  12.261402751806099),
 183: (1.0028604713353673,
  -0.0015658607855911643,
  -0.012098971139120134,
  1.0030481953989219,
  12.886170487293692,
  12.348160722182769),
 184: (1.0028391199434779,
  -0.0060682743974767445,
  -0.012710025423094413,
  1.003051980482818,
  12.713402110572995,
  12.354469304845153),
 185: (1.0028716066519192,
  -0.0022402529215429755,
  -0.010531800997446685,
  1.003065705525438,
  12.908507468934957,
  12.449980443294777),
 186: (1.002835145455527,
  -0.012918649903239376,
  -0.011778209644880954,
  1.0030741856208256,
  12.589461289364138,
  12.458567659637575),
 187: (1.0028520671829444,
  -0.007926316106780379,
  -0.013247294121776654,
  1.0030846276708403,
  12.742940663835762,
  12.39912214878219),
 188: (1.0028780520908482,
  -0.003461006808138728,
  -0.014222028826517415,
  1.0030923876467457,
  12.886803620486718,
  12.373312430926969),
 189: (1.002902656929669,
  0.000205777477017971,
  -0.014202676036452231,
  1.0031073723328114,
  13.062210917092276,
  12.446303575932543),
 190: (1.0028867480131376,
  -0.005844044675561992,
  -0.013870468438909586,
  1.0031357310484912,
  12.882414795728224,
  12.56653743056387),
 191: (1.0028958526262244,
  -0.007568305207781002,
  -0.010605578847616657,
  1.0031755396313145,
  12.852062833325618,
  12.783826271634979),
 192: (1.0028963479903135,
  -0.009815130463589205,
  -0.009754698885980396,
  1.0031843810024403,
  12.802034975401906,
  12.857856947593104),
 193: (1.0029023334878442,
  -0.009283392377352285,
  -0.010583307768849984,
  1.0031926652781853,
  12.81404673064819,
  12.846984324494825),
 194: (1.0029502435355997,
  -0.001769953603955812,
  -0.01280460451576811,
  1.0031941136052003,
  13.112260351920053,
  12.77927424946506),
 195: (1.0029537393766395,
  -0.0034345249773231144,
  -0.013593367756741392,
  1.0031937693307251,
  13.082299346551222,
  12.72771859751158),
 196: (1.0029523340382582,
  -0.006568016544511244,
  -0.011275040609366172,
  1.0032218753367068,
  13.016930425149347,
  12.874030691163242),
 197: (1.0029877604510269,
  -0.0027191514271736107,
  -0.010622643176869562,
  1.0032301481911745,
  13.232374787855136,
  12.919664502155081),
 198: (1.0029982208042074,
  -0.0048912418557315535,
  -0.007496627803292464,
  1.003262644468391,
  13.18926787990394,
  13.085748816506415),
 199: (1.0029896781839711,
  -0.00807390329113471,
  -0.01401479995489551,
  1.0032479288751739,
  13.162486728355939,
  12.874730622712242),
 200: (1.0029839517261592,
  -0.010245644582257242,
  -0.01641632700222059,
  1.0032415646265085,
  13.10655567089651,
  12.761316915069711)}
[30]:
x_vals = [descent_data[i][X_INDEX] for i in range(0, n_iterations)]
y_vals = [descent_data[i][Y_INDEX] for i in range(0, n_iterations)]

We see in the plot that the metric generally improves as transformation parameters are updated with each iteration, but the final position may not align with the maximum position on the plot. This is one case in which it is difficult to visualize gradient ascent over a hyperdimensional space, where the optimizer is stepping through six parameter dimensions but the 2D plot we collected with ExhaustiveOptimizer represents a ‘slice’ in space with x[0:4] fixed at (1,0,0,1). Here it may be more useful to directly compare the two images after registration to evaluate fitness.

[31]:
fig = plt.figure()
# Note: We invert the y-axis to represent the image coordinate system
plt.gca().invert_yaxis()
ax = plt.gca()

surf = ax.scatter(X, Y, c=Z, cmap=cm.coolwarm)

for i in range(0, n_iterations - 1):
    plt.plot(x_vals[i : i + 2], y_vals[i : i + 2], "wx-")
plt.plot(descent_data[0][X_INDEX], descent_data[0][Y_INDEX], "bo")
plt.plot(descent_data[n_iterations - 1][X_INDEX], descent_data[n_iterations - 1][Y_INDEX], "ro")

plt.plot(max_position[X_INDEX], max_position[Y_INDEX], "k^")
plt.plot(min_position[X_INDEX], min_position[Y_INDEX], "kv")

[31]:
[<matplotlib.lines.Line2D at 0x2a56f1a7c70>]
../../../../_images/src_Core_Transform_MutualInformationAffine_MutualInformationAffine_36_1.png

Resample the moving image#

In order to apply the results of gradient ascent we must resample the moving image into the domain of the fixed image. The TranslationTransform whose parameters have been selected through gradient ascent is used to dictate how the moving image is sampled from the fixed image domain. We can compare the two images with itkwidgets to verify that registration is successful.

[32]:
ResampleFilterType = itk.ResampleImageFilter[ImageType, ImageType]
resample = ResampleFilterType.New(
    Transform=transform,
    Input=moving_image,
    Size=fixed_image.GetLargestPossibleRegion().GetSize(),
    OutputOrigin=fixed_image.GetOrigin(),
    OutputSpacing=fixed_image.GetSpacing(),
    OutputDirection=fixed_image.GetDirection(),
    DefaultPixelValue=100,
)

[33]:
resample.Update()

[34]:
checkerboard(fixed_image, resample.GetOutput())

The image comparison shows that the images were successfully translated to overlap, but were not fully rotated to exactly align. If we were to explore further we could use a different optimizer with the metric, such as the LBFGSBOptimizer class, which may be more successful in optimizing over a rough parametric surface. We can also explore different metrics such as the MattesMutualInformationImageToImageMetricv4 class to take advantage of the ITK v4+ registration framework, in contrast with the MutualInformationImageToImageMetric used in this example as part of the v3 framework.

Clean up#

[35]:
os.remove(fixed_image_path)
os.remove(moving_image_path)