# Invert elastix transformation

Do you know how to compute the inverse transformation of an elastix transformation?

In my case the transformation is quite complex, spanning multiple Transformation.txt files, including several BSplines.

And what I would need is not to transform an image, but a list of points. Transforming lists of points is supported using `-def` in `transformix`, but this will transform points from the `fixed` to the `moving` image, whereas I’d need the opposite direction.

1 Like

I never did this… From the documentation (https://github.com/SuperElastix/elastix/releases/download/4.9.0/elastix-4.9.0-manual.pdf, p38), apparently this can be done by using the displacement magnitude as the penalty that should be minimized.

From the manual:

The DisplacementMagnitudePenalty is a cost function that penalises ||Tμ(x) − x||2. You can use this
to invert transforms, by setting the transform to be inverted as an initial transform (using -t0), setting
(HowToCombineTransforms “Compose”), and running elastix with this metric, using the original fixed
image set both as fixed (-f) and moving (-m) image. After that you can manually set the initial transform
in the last parameter file to “NoInitialTransform”, and voila, you have the inverse transform!

Yes, I found that, too, but it looked rather obscure to me
Also, I did not really get how to actually do it…

The main idea is to compose your know initial forward transform `A` with an unknow transform `B` (it can be whatever, spline, rigid, affine). But:

• you set an identical `Image` for the moving image and the fixed image
• you set the penalty cost for registration as the composed displacement field magnitude

Then you perform the registration, by launching elastik, but you specify the initial transform `-t0 parameterTransformA.txt` and the parameters of the inverse transform you’d like to have with `-p regParamB.txt`

(edit: in the case of multiple chained transforms, normally you just need to specify the last transform file because the chaining is specified in it.)

Because of these parameters, basically doing the registration is equivalent to solve `B(A(Image))=Image`, so in other terms `B = A^-1`; the unknown transform will be the invert of your original transformation.

Note that you can try to invert a spline transform with a rigid transform (it won’t be perfect, but it will try to do its best).

I managed to do some simple inverse transform. I can try to come up with some command line example.

2 Likes

I have some code that might be worth trying, but am traveling at the moment, I’ll be a little slow…

1 Like

I’ve played a bit with the inverse transform. Here’s a small test case:
So on the first column you can see the blobs, and the image to which it is registered on the second column. The registration is done with the composition of a rigid transform followed by a bspline transform:

`elastix -f blobs-warp.tif -m blobs.tif -p euler.txt -p bspline.txt -out folder_out`

The third column shows the result of this first registration, which is bad, but that doesn’t matter, it’s actually better to test if the invert computation works. To compute the inverse transform (which I choose to be a spline, as specified in the file `invRegParameters.txt`), I execute:

`elastix -f grid.tif -m grid.tif -t0 TransformParameters.1.txt -p invRegParameters.txt -out folder_out_inv`

I believe the choice of the image (`grid.tif`) is not important. It’s just helpful to specify the region on which the computation will take place. `TransformParameters.1.txt` is the last transform file generated be the first registration.

This computation yields the file `TransformParameters.0.txt`. In this file you need to change the initial transform to `NoInitialTransform`, and then the file can be used to invert the transform given by the first registration: as you can see on the fourth column, this works nicely.

Here’s the files used, just in case you want to reproduce this sequence:
elastixinvreg.zip (1.1 MB)

1 Like