Multi stage composite transform

Hello to all,

I am trying to write a very simple gui that allows to choose to cancatenate different types of trasforms (rigid, affine and elastic) with different parameters/metrics/optimizers and save the tfm file. I tried first the VH_registration example, but when I try to write the .tfm file I get this error:

RuntimeError: Exception thrown in SimpleITK WriteTransform: D:\a\1\sitk-build\ITK\Modules\IO\TransformInsightLegacy\include\itkTxtTransformIO.hxx:349:
itk::ERROR: TxtTransformIOTemplate(0000000045619F60): Composite Transform can only be 1st transform in a file

I looked a bit also in composite transforms, but I didn’t get these two issues: is it possible to optimize more than one transform? Can I use the CenteredTransformInitializer?

Hello @lucilla7,

The composite transform in SimpleITK has very recently been updated. We now have an explicit CompositeTransform that is different from Transform.

To install the latest nightly binaries:

pip install --upgrade --pre SimpleITK --find-links

The following code will work with this updated transform type:

import SimpleITK as sitk
# Two translations in a list used in constructor, 
#we can also add transforms with the AddTransform method
tx = sitk.CompositeTransform([sitk.TranslationTransform(2,[1,2])]*2)
sitk.WriteTransform(tx, 'result.tfm')

This error message occurs when trying to write nested composite Transforms.

Using the new interface:

>>> tx = sitk.CompositeTransform([sitk.TranslationTransform(2), sitk.CompositeTransform(2)])
>>> sitk.WriteTransform(tx, "test.txt")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/blowekamp/miniconda3/envs/sitk-ibex/lib/python3.6/site-packages/SimpleITK/", line 4777, in WriteTransform
    return _SimpleITK.WriteTransform(transform, filename)
RuntimeError: Exception thrown in SimpleITK WriteTransform: /Users/runner/runners/2.170.1/work/1/sitk-build/ITK/Modules/IO/TransformInsightLegacy/src/itkTxtTransformIO.cxx:335:
itk::ERROR: itk::ERROR: TxtTransformIOTemplate(0x7fc31fdd1f30): Composite Transform can only be 1st transform in a file
>>> tx.FlattenTransform()
>>> sitk.WriteTransform(tx, "test.txt")

Thank you!
In the documentation of the new CompositeTransform I see this, though:

The only parameters of the transform at the back of the queue are exposed and optimizable for registration.

So, I cannot optimize all the three transforms. If I’d need to do that, I should apply individually each transform and save its tfm file, right?

You can optimize one transform at a time and composite them. An example might be first to optimize an affine transform then a displacement field.

You can see an example of this here:


Thanks!! Just one more question, if I need to set a mask on the metric calculation, should I set it for all the trasnforms?

Hello @lucilla7,
Generally speaking, yes, you would use the same mask for all registrations. The mask is the subregion of the image that contains relevant information for registration. Usually this is the same region for all steps (affine followed by Bspline/Demons…). If for some reason you only need to deform a portion of the image after doing a global affine registration then you would potentially have one mask or no mask for the initial registration and a sedond mask limited to the region undergoing deformation.

1 Like