SimpleITK provides easy to use Python interface. Can I extend the class from there?
I need to solve a registration problem, which requires me to write my customized registration class, especially the similarity metric. How can I extend SimpleITK in Python for my use?
The SimpleElastix package is an extension of SimpleITK that includes the elastix registration library in addition to the standard SimpleITK methods. You can write your metric in elastix and use SimpleElastix to wrap your metric in Python (or any of the other languages supported by SimpleITK). You would then use SimpleElastix to perform registration. To use your would-be metric in SimpleElastix:
import SimpleITK as sitk
SimpleElastix = sitk.SimpleElastix()
SimpleElastix.SetParameter("Metric", "NameOfYourMetric")
SimpleElastix.SetFixedImage(sitk.ReadImage("fixedImage.nii"))
SimpleElastix.SetMovingImage(sitk.ReadImage("movingImage.nii"))
resultImage = SimpleElastix.Execute()
Elastix itself is an extension of ITK's v3 registration libraries, so if you are comfortable with ITK, the elastix codebase will not be alien to you. You can see how metrics are implemented by looking at the examples in the src/Components/Metrics directory. Take a look at the AdvancedMeanSqaures metric for instance. Basically you only need to modify the GetValue()
and GetValueAndDerivative()
functions in the files prefixed with itk
to implement your metric. You then need to modify the files prefixed with elx
if you want to pass parameters to your metric via the parameter maps and the CMakeLists file to register the metric with the build system. Parameter maps are key-value pairs that allows you to configure registration components including your metric. You can read more about parameter maps in the SimpleElastix documentation or elastix manual.
To wrap your metric with SimpleElastix, you clone the repository and follow the guide how to build SimpleElastix manually except you set ELASTIX_DIR
to your modified version of elastix. The elastix build system will take care of wrapping your metric in elastix, and the SimpleElastix build system will take care of wrapping elastix in python.
Disclaimer: I am the author of SimpleElastix.