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.34741 : (0.7489820589143295, 0.6625902771882876)
1 = -0.35794 : (1.1941170159786205, 1.5580537716979028)
2 = -0.36440 : (1.9051160412039123, 2.261246765212973)
3 = -0.37602 : (2.871872087249055, 2.516947268605443)
4 = -0.38470 : (3.6200955730206092, 3.180394037638094)
5 = -0.39311 : (4.4612447257705234, 3.7211972392078527)
6 = -0.40514 : (5.141679502808506, 4.454005886944266)
7 = -0.41723 : (5.907661254726357, 5.096868201696701)
8 = -0.43019 : (6.5560275316669205, 5.8581967581861)
9 = -0.44583 : (7.330357208802286, 6.49097914661631)
10 = -0.46806 : (7.997195598009237, 7.2361815101927265)
11 = -0.49074 : (8.534950379630756, 8.079282805911808)
12 = -0.50826 : (9.118912175325061, 8.8910639470881)
13 = -0.53101 : (9.63045776525243, 9.750320084352104)
14 = -0.56404 : (10.098990332067213, 10.633766310125893)
15 = -0.59001 : (10.630601974691096, 11.480754539930025)
16 = -0.63413 : (11.116659778627087, 12.354681202318194)
17 = -0.67299 : (11.473938476275661, 13.288679024696028)
18 = -0.72745 : (12.148672165019144, 14.02674030214839)
19 = -0.79327 : (12.617660772785365, 14.90994451737016)
20 = -0.87309 : (12.98085733261639, 15.841657058300521)
21 = -1.01699 : (13.042825456798223, 16.83973518729914)
22 = -1.35611 : (12.739470845218626, 17.79261292323035)
23 = -1.08571 : (12.918604881493943, 17.32580354345343)
24 = -1.27645 : (13.083719467055985, 16.853853150260804)
25 = -1.35380 : (12.94726124517934, 17.063326665728187)
26 = -1.38892 : (13.035189536430005, 16.974480643882874)
27 = -1.39956 : (12.982485296781629, 17.00807399327061)
28 = -1.40347 : (13.011657891793998, 16.996870324217827)
29 = -1.40390 : (12.9964082376448, 17.00027453827247)
30 = -1.40429 : (13.004158163744622, 16.999287724360276)
31 = -1.40426 : (13.00026408587451, 16.999595858428517)
32 = -1.40432 : (13.002215400131234, 16.99951177551032)
-------
itk::simple::TranslationTransform
TranslationTransform (0x559380484e10)
RTTI typeinfo: itk::TranslationTransform<double, 2u>
Reference Count: 2
Modified Time: 13963
Debug: Off
Object Name:
Observers:
none
Offset: [13.0022, 16.9995]
Optimizer stop condition: RegularStepGradientDescentOptimizerv4: Step too small after 33 iterations. Current step (0.000976562) is less than minimum step (0.001).
Iteration: 34
Metric value: -1.4042980571486046
Input Images¶
Fixed Image |
Moving Image |
Output Image¶
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}
# Run with:
#
# Rscript --vanilla ImageRegistrationMethod4.R fixedImageFilter movingImageFile outputTransformFile numberOfBins samplingPercentage
#
library(SimpleITK)
commandIteration <- function(method)
{
msg <- paste(method$GetOptimizerIteration(), "=",
method$GetMetricValue(), ":",
method$GetOptimizerPosition(), "\n" )
cat(msg)
}
args <- commandArgs( TRUE )
if (length(args) < 3) {
stop("3, 4, or 5 arguments expected - fixedImageFilter, movingImageFile, outputTransformFile [numberOfBins] [samplingPercentage]")
}
fixed <- ReadImage(args[[1]], 'sitkFloat32')
moving <- ReadImage(args[[2]], 'sitkFloat32')
numberOfBins <- 24
samplingPercentage <- 0.10
if (length(args) > 4) {
numberOfBins <- strtoi(args[[4]])
}
if (length(args) > 5) {
samplingPercentage <- as.numeric(args[[5]])
}
R <- ImageRegistrationMethod()
R$SetMetricAsMattesMutualInformation(numberOfBins)
R$SetMetricSamplingPercentage(samplingPercentage)
R$SetMetricSamplingStrategy('RANDOM')
R$SetOptimizerAsRegularStepGradientDescent(1.0,.001,200)
R$SetInitialTransform(TranslationTransform(fixed$GetDimension()))
R$SetInterpolator('sitkLinear')
R$AddCommand( 'sitkIterationEvent', function() commandIteration(R) )
outTx <- R$Execute(fixed, moving)
cat("-------\n")
outTx
cat("Optimizer stop condition:", R$GetOptimizerStopConditionDescription(), '\n')
cat("Iteration:", R$GetOptimizerIteration(), '\n')
cat("Metric value:", R$GetMetricValue(), '\n')
WriteTransform(outTx, args[[3]])

