# Image Registration Optimizer Weights¶

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

## Overview¶

The image registration framework allows us to disable subsets of the transformation parameters so that we can use a lower dimensional transformation. In some cases, if we have some better prior knowledge we can weigh the parameters accordingly (this is much harder to utilize to get a desired effect than disabling parameters).

In this example, we work with a 3D rigid transformation using the Euler parameterization, but we only allow rotation around the z axis. As there is no transformation that represents this subspace of rigid transformations, we use the SetOptimizerWeights method to disable rotations around the x and y axes. The order of the weights in the weights parameter matches the order of parameters returned from the transformation GetParameters method. For the Euler3DTransform the order is [angleX, angleY, angleZ, tx, ty, tz], so our weights vector is [0,0,1,1,1,1].

## Code¶

#!/usr/bin/env python

import SimpleITK as sitk
import sys

if len(sys.argv) < 4:
print("Usage:", sys.argv[0], "<fixedImageFile> <movingImageFile>",
"<outputTransformFile>")
sys.exit(1)

fixed_image = sitk.ReadImage(sys.argv[1], sitk.sitkFloat32)
moving_image = sitk.ReadImage(sys.argv[2], sitk.sitkFloat32)

# initialization
transform = sitk.CenteredTransformInitializer(fixed_image,
moving_image,
sitk.Euler3DTransform(),
sitk.CenteredTransformInitializerFilter.GEOMETRY)
# registration
registration_method = sitk.ImageRegistrationMethod()
registration_method.SetMetricAsCorrelation()
registration_method.SetMetricSamplingStrategy(registration_method.NONE)
registration_method.SetInterpolator(sitk.sitkLinear)
registration_method.SetOptimizerAsGradientDescent(learningRate=1.0,
numberOfIterations=300,
convergenceMinimumValue=1e-6,
convergenceWindowSize=10)
registration_method.SetOptimizerScalesFromPhysicalShift()
registration_method.SetInitialTransform(transform, inPlace=True)
registration_method.SetOptimizerWeights([0,0,1,1,1,1])
registration_method.Execute(fixed_image, moving_image)

print("-------")
print(f"Final transform parameters: {transform.GetParameters()}")
print(f"Optimizer stop condition: {registration_method.GetOptimizerStopConditionDescription()}")
print(f"Iteration: {registration_method.GetOptimizerIteration()}")
print(f"Metric value: {registration_method.GetMetricValue()}")

sitk.WriteTransform(transform, sys.argv[3])