# SimpleITK: Using TransformPoint() to Transform Mask Vertices

**URL:** https://discourse.itk.org/t/simpleitk-using-transformpoint-to-transform-mask-vertices/4244
**Category:** Engineering
**Tags:** registration, python, simpleitk
**Created:** [July 5, 2021, 10:22pm UTC](https://discourse.itk.org/t/simpleitk-using-transformpoint-to-transform-mask-vertices/4244 "2021-07-05T22:22:44Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![bbikdash](https://discourse.itk.org/user_avatar/discourse.itk.org/bbikdash/32/2117_2.png) [@bbikdash](https://discourse.itk.org/u/bbikdash)
#### Post date: [July 5, 2021, 10:22pm UTC](https://discourse.itk.org/t/simpleitk-using-transformpoint-to-transform-mask-vertices/4244/1 "2021-07-05T22:22:44Z")

</div>

Hello,

I have a similar problem as the OP [here](https://discourse.itk.org/t/how-do-i-apply-a-transform-to-fiducial-seeds-from-slicer/1020) but I’m attempting to use the `TransformPoint()` function from SimpleITK with a `DisplacementFieldTransform` instead of an affine transform.

### Problem

I have a moving image with a set of vertices that define a mask. After applying SimpleITK’s SymmetricForcesDemonsRegistration I have a transform to go from one image to another and I want to use the transform to find the new locations of mask vertices in the fixed image.

 ![test1](https://discourse.itk.org/uploads/default/original/2X/1/14e02f18b1e134690a115d7d8555ead50806dbe8.jpeg)  
Moving Image (Image with set mask vertices)

 ![test2](https://discourse.itk.org/uploads/default/original/2X/6/6521794a277fb5d8f2b0c8df6817a2298d146099.jpeg)  
Fixed Image (Image to find new location of mask vertices in)

 ![final_img](https://discourse.itk.org/uploads/default/original/2X/1/1e77be6c370a73a1f36974a7aa5df9bd692be90c.jpeg)  
Moving image after applying SymmetricForcesDemonsRegistration

## Question:

Is there a way to use `TransformPoint()` to find the new vertices based on `DisplacementFieldTransform`?

It looks like the transformed vertices are the inverse of the original transformation but I am unsure how to rectify that.

 ![all_vertices](https://discourse.itk.org/uploads/default/original/2X/4/4b070af0a89942afd59578b6203c061b226eae9f.jpeg)  
Original mask vertices and centroids

 ![all_vertices_warped](https://discourse.itk.org/uploads/default/original/2X/0/0ddef61900a533ad532f8dd7967d5e874cf3d648.jpeg)  
Incorrect mask vertices and centroids after applying `Transform.TransformPoint()`

### Attempts:

- I have tried [OP’s original suggestion](https://discourse.itk.org/t/how-do-i-apply-a-transform-to-fiducial-seeds-from-slicer/1020) of computing the inverse transform with `GetInverse()` which throws an error saying that the inverse could not be created.
- I have also tried `GetInverseDisplacementField()` paired with `sitk.DisplacementFieldTransform` to generate the inverse with no luck either.
- I also generated an image with the vertices highlighted and then warped that image using the SimpleITK resampler to execute the transform. This successfully produced the right vertices however, it introduced other issues when I attempted to extract the vertices from the image. Using `TransformPoint()` would be the simplest and easiest if I can get it to work.

Any suggestions would be greatly appreciated.

Thanks.

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [July 6, 2021, 12:55pm UTC](https://discourse.itk.org/t/simpleitk-using-transformpoint-to-transform-mask-vertices/4244/2 "2021-07-06T12:55:31Z")

</div>

Hello @bbikdash,

The output of the registration is a transformation which maps points from the fixed image to the moving image space.

If all you need is to map points from the current moving image to the fixed image, just switch their roles in the registration.

If you want to resample the current moving image onto the fixed image coordinate system **and** you want to map points from the moving image to the fixed image, you will need to estimate the inverse transformation to perform the latter. There are several options for estimating the inverse (different algorithms):

- [InvertDisplacementFieldImageFilter](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1InvertDisplacementFieldImageFilter.html)
- [InverseDisplacementFieldImageFilter](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1InverseDisplacementFieldImageFilter.html#details)
- [IterativeInverseDisplacementFieldImageFilter](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1IterativeInverseDisplacementFieldImageFilter.html)

They all expect an image as input which you obtain from the transform using its `GetDisplacementField` method.

---

<div class="post-metadata">

### Author: ![bbikdash](https://discourse.itk.org/user_avatar/discourse.itk.org/bbikdash/32/2117_2.png) [@bbikdash](https://discourse.itk.org/u/bbikdash)
#### Post date: [July 6, 2021, 3:23pm UTC](https://discourse.itk.org/t/simpleitk-using-transformpoint-to-transform-mask-vertices/4244/3 "2021-07-06T15:23:13Z")

</div>

Of course. Switching the inputs of the registration filter would give me what I need.

Thank you, @zivy .
