# Extracting displacement fields from rigid registration

**URL:** https://discourse.itk.org/t/extracting-displacement-fields-from-rigid-registration/4834
**Category:** Beginner Questions
**Tags:** registration, python, itkv5
**Created:** [February 25, 2022, 11:03pm UTC](https://discourse.itk.org/t/extracting-displacement-fields-from-rigid-registration/4834 "2022-02-25T23:03:39Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![edgar](https://discourse.itk.org/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@edgar](https://discourse.itk.org/u/edgar)
#### Post date: [February 25, 2022, 11:03pm UTC](https://discourse.itk.org/t/extracting-displacement-fields-from-rigid-registration/4834/1 "2022-02-25T23:03:39Z")

</div>

Hello!

> Continuing the discussion from [3D image registration with Python and without SimpleITK](https://discourse.itk.org/t/3d-image-registration-with-python-and-without-simpleitk/4773):

Once I have a [registration](https://discourse.itk.org/t/3d-image-registration-with-python-and-without-simpleitk/4773), what is the best way to extract the deformation field (a.k.a. displacement field) with ITK Python? The idea is to avoid reprocessing and have shorter code.

1. is [converting my image to points](https://discourse.itk.org/t/siutable-deformable-registration/3102) still necessary (see [this](https://discourse.itk.org/t/linear-transforms-to-displacement-field/2992)).
  - is this also the case once the scaling, origin and direction are set for the image (as shown in Sec. 4.14 Defining Origin and Spacing of the Software Guide)?
  - does one need to [iterate over the whole image](https://itk.org/Doxygen52/html/Examples_2RegistrationITKv4_2DeformableRegistration15_8cxx-example.html) (to use points)?

2. is a [composite transform](https://simpleitk.readthedocs.io/en/master/link_ImageRegistrationMethodDisplacement1_docs.html) ([permalink](https://github.com/SimpleITK/SimpleITK/blob/04d30063f48b6a4618197818635b43842e6bc781/docs/source/link_ImageRegistrationMethodDisplacement1_docs.rst)) a better alternative? (As I am looking for a Python ITK solution, I would translate the SimpleITK syntax).
3. is there a way to get the displacement (deformation) field from the registration or transform directly? (as in: a method of the Transform class) [[Edit](https://discourse.itk.org/t/extracting-vectors-from-displacement-field/417/2)]. `itk.TransformToDisplacementFieldFilter` tells me that `AttributeError: module 'itk' has no attribute 'TransformToDisplacementFieldFilter'`
4. is there a full example which shows how to extract the displacements from a rigid registration?

Thank you, very much!

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [February 27, 2022, 6:22pm UTC](https://discourse.itk.org/t/extracting-displacement-fields-from-rigid-registration/4834/2 "2022-02-27T18:22:57Z")

</div>

`TransformToDisplacementFieldFilter` is [wrapped for Python](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Filtering/DisplacementField/wrapping/itkTransformToDisplacementFieldFilter.wrap). If you compiled ITK from source, make sure that `ITK_WRAP_vector_float` is enabled and `ITK_WRAP_VECTOR_COMPONENTS` contains 3. A Python example [which touches a deformation field](https://itk.org/ITKExamples/src/Filtering/ImageGrid/WarpAnImageUsingADeformationField/Documentation.html) might be useful from syntax perspective.

---

<div class="post-metadata">

### Author: ![edgar](https://discourse.itk.org/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@edgar](https://discourse.itk.org/u/edgar)
#### Post date: [February 27, 2022, 6:36pm UTC](https://discourse.itk.org/t/extracting-displacement-fields-from-rigid-registration/4834/3 "2022-02-27T18:36:34Z")

</div>

Hi there, Dženan. Thank you. My CMakeCache.txt shows:

```auto
//Number of vector components available separated by semicolons
// (;)
ITK_WRAP_VECTOR_COMPONENTS:STRING=2;3;4
...
//Wrap vector float type
ITK_WRAP_vector_float:BOOL=ON

```

and my compilation instructions include

```auto
-DITK_WRAP_IMAGE_DIMS:STRING="2;3;4"

```

Do you think that there would be a difference if I recompile like this?:

```auto
-DITK_WRAP_VECTOR_COMPONENTS:STRING="2;3;4" -DITK_WRAP_vector_float:BOOL=ON

```

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [February 27, 2022, 6:50pm UTC](https://discourse.itk.org/t/extracting-displacement-fields-from-rigid-registration/4834/4 "2022-02-27T18:50:51Z")

</div>

I don’t think there would be a difference. My cache has the same content, I think those are default options. There is no problem for me:

```nohighlight
>>> import itk
>>> itk.TransformToDisplacementFieldFilter
<itkTemplate itk::TransformToDisplacementFieldFilter>
>>>

```

---

<div class="post-metadata">

### Author: ![edgar](https://discourse.itk.org/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@edgar](https://discourse.itk.org/u/edgar)
#### Post date: [February 27, 2022, 10:11pm UTC](https://discourse.itk.org/t/extracting-displacement-fields-from-rigid-registration/4834/5 "2022-02-27T22:11:38Z")

</div>

Thank you again, Dženan. As I am looking into my makepkg.conf (makepkg is the building tool of Arch-based systems), I see that there is an option to disable and clear static libraries. I wonder if that has to do with it. I will disable the option, compile and report back. Thanks.

---

<div class="post-metadata">

### Author: ![edgar](https://discourse.itk.org/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@edgar](https://discourse.itk.org/u/edgar)
#### Post date: [March 2, 2022, 5:01am UTC](https://discourse.itk.org/t/extracting-displacement-fields-from-rigid-registration/4834/6 "2022-03-02T05:01:44Z")

</div>

I will create a question to solve my `import` situation and come back to this once I am done

---

<div class="post-metadata">

### Author: ![edgar](https://discourse.itk.org/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@edgar](https://discourse.itk.org/u/edgar)
#### Post date: [April 12, 2022, 7:48pm UTC](https://discourse.itk.org/t/extracting-displacement-fields-from-rigid-registration/4834/7 "2022-04-12T19:48:35Z")

</div>

Hi! I’m back 🙂 .

I’ve got a little step forward:

```
displ_filt = itk.TransformToDisplacementFieldFilter[itk.Image[itk.Vector[itk.F,3],3], itk.D].New()

```

In the [example](https://itk.org/ITKExamples/src/Filtering/ImageGrid/WarpAnImageUsingADeformationField/Documentation.html) which is linked above, the deformation field is provided as an input (`deformationField = fieldReader.GetOutput()`). I already tried to look into the [documentation](https://itk.org/Doxygen/html/classitk_1_1TransformToDisplacementFieldFilter.html) mentioned by @matt.mccormick [over here](https://discourse.itk.org/t/extracting-vectors-from-displacement-field/417/2), and with `help(displ_filt)`. Right now, I am unsure of how to get the deformation field from the [registration](https://discourse.itk.org/t/3d-image-registration-with-python-and-without-simpleitk/4773/2) or [`movingInitialTransform`](https://discourse.itk.org/uploads/short-url/v2jPUbpGc3HXfmMHad1B46sER0o.py). Any tips? Thank you in advance.

It’s nice to be back.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [April 12, 2022, 7:56pm UTC](https://discourse.itk.org/t/extracting-displacement-fields-from-rigid-registration/4834/8 "2022-04-12T19:56:03Z")

</div>

I think you only need these 2 lines more:

```auto
displ_filt.Update()
deformationField = displ_filt.GetOutput()

```

---

<div class="post-metadata">

### Author: ![edgar](https://discourse.itk.org/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@edgar](https://discourse.itk.org/u/edgar)
#### Post date: [April 13, 2022, 10:47am UTC](https://discourse.itk.org/t/extracting-displacement-fields-from-rigid-registration/4834/9 "2022-04-13T10:47:36Z")

</div>

Thank you, Dženan. I initially got this error:

```
RuntimeError: /pkg/insight-toolkit/src/insight-toolkit/Modules/Core/Common/src/itkProcessObject.cxx:1339:
ITK ERROR: TransformToDisplacementFieldFilter(0x55578b3cac50): Input Transform is required but not set.

```

Then, I used this instead:

```
displ_filt.SetTransform(registr_transform)
displ_filt.Update()
displ_field_out = displ_filt.GetOutput()

```

To save to a file, I naïvely used [this](https://discourse.itk.org/t/problem-in-getting-displacement-field/3127):

```
displ_writer = itk.ImageFileWriter.New(
    Input=displ_field_out, FileName=displ_field_vtk)
displ_writer.Update()

```

that produces:

```
RuntimeError: /pkg/insight-toolkit/src/insight-toolkit/Modules/IO/ImageBase/include/itkImageFileWriter.hxx:245:
ITK ERROR: ImageFileWriter(0x555788553d50): Largest possible region does not fully contain requested paste IO regionPaste IO region: ImageIORegion (0x7ffd35280600)
  Dimension: 3
  Index: 0 0 0
  Size: 0 0 0
Largest possible region: ImageRegion (0x7ffd35280760)
  Dimension: 3
  Index: [0, 0, 0]
  Size: [0, 0, 0]

```

I tried other approaches, but I’m sure that expanding on my mistakes is of no use 😛 . How do I save the results? (is what I did to get them the right way?). Thank you again 🙂 .

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [April 13, 2022, 1:46pm UTC](https://discourse.itk.org/t/extracting-displacement-fields-from-rigid-registration/4834/10 "2022-04-13T13:46:17Z")

</div>

> [@edgar](#):
>
> `displ_writer = itk.ImageFileWriter.New(`

This approach might need explicit type specification, such as `displ_writer = itk.ImageFileWriter[itk.Image[itk.Vector[itk.D,3],3]].New(`.

Simpler, recommended way is `itk.imwrite(displ_field_out, displ_field_vtk)`

---

<div class="post-metadata">

### Author: ![edgar](https://discourse.itk.org/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@edgar](https://discourse.itk.org/u/edgar)
#### Post date: [April 13, 2022, 3:41pm UTC](https://discourse.itk.org/t/extracting-displacement-fields-from-rigid-registration/4834/11 "2022-04-13T15:41:35Z")

</div>

Thanks again. (I changed `itk.D` to `itk.F`) I am still getting the same error (with different memory allocation, possibly).

```
>>> itk.imwrite(displ_field_out, displ_field_vtk)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.10/site-packages/itk/support/extras.py", line 910, in imwrite
    writer.Update()
RuntimeError: /home/edgar/Documentos/Linux/Parabola/pkg/insight-toolkit/src/insight-toolkit/Modules/IO/ImageBase/include/itkImageFileWriter.hxx:245:
ITK ERROR: ImageFileWriter(0x55b1c49b2570): Largest possible region does not fully contain requested paste IO regionPaste IO region: ImageIORegion (0x7fff1c528160)
  Dimension: 3
  Index: 0 0 0 
  Size: 0 0 0 
Largest possible region: ImageRegion (0x7fff1c5282c0)
  Dimension: 3
  Index: [0, 0, 0]
  Size: [0, 0, 0]

>>> displ_writer = itk.ImageFileWriter[itk.Image[itk.Vector[itk.F, 3], 3]].New(Input=displ_field_out, FileName=displ_field_vtk)
>>> displ_writer.Update()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: /home/edgar/Documentos/Linux/Parabola/pkg/insight-toolkit/src/insight-toolkit/Modules/IO/ImageBase/include/itkImageFileWriter.hxx:245:
ITK ERROR: ImageFileWriter(0x55b1c4b2ef90): Largest possible region does not fully contain requested paste IO regionPaste IO region: ImageIORegion (0x7fff1c528330)
  Dimension: 3
  Index: 0 0 0 
  Size: 0 0 0 
Largest possible region: ImageRegion (0x7fff1c528490)
  Dimension: 3
  Index: [0, 0, 0]
  Size: [0, 0, 0]
```

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [April 13, 2022, 4:00pm UTC](https://discourse.itk.org/t/extracting-displacement-fields-from-rigid-registration/4834/12 "2022-04-13T16:00:17Z")

</div>

Oh, you need to specify the size etc of the desired displacement field. Use `TransformToDisplacementFieldFilter.SetReferenceImage()` plus `UseReferenceImageOn` or `SetOutputOrigin`, `SetOutputSpacing` etc. See docs for [SetReferenceImage](https://itk.org/Doxygen/html/classitk_1_1TransformToDisplacementFieldFilter.html#aa857b5d1b8ed8f44bf7b1d01cca87ced).

---

<div class="post-metadata">

### Author: ![edgar](https://discourse.itk.org/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@edgar](https://discourse.itk.org/u/edgar)
#### Post date: [April 13, 2022, 5:17pm UTC](https://discourse.itk.org/t/extracting-displacement-fields-from-rigid-registration/4834/13 "2022-04-13T17:17:22Z")

</div>

Great, thanks. Paraphrasing from the docs:

> This method can be used to specify an image from which to copy the [output information (SetOutputSpacing, Origin, and Direction)]. UseReferenceImageOn must be set

So I did:

```
displ_filt = itk.TransformToDisplacementFieldFilter[
    itk.Image[itk.Vector[itk.F, 3], 3], itk.D].New()
displ_filt.SetTransform(registr_transform)
displ_filt.SetReferenceImage(fixed_image)
displ_filt.UseReferenceImageOn()
displ_filt.Update()
displ_field_out = displ_filt.GetOutput()

```

and

```
itk.imwrite(displ_field_out, displ_field_vtk)

```

It runs well now 🙂 .

The (VTK) output shows up in ParaView as a block with the size of my `fixed_image`, completely full of little glyphs in the same direction. This makes me think that I will have to do this with a _moving window_ (an area smaller than the full image) whose position I will move iteratively to get the displacements at each point, correct? This is possibly what @matt.mccormick was trying to say:

> [@Extracting vectors from displacement field](https://discourse.itk.org/t/extracting-vectors-from-displacement-field/417/2):
>
> The displacement vector value at specific locations can be probed with the `GetPixel()` method

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [April 13, 2022, 5:28pm UTC](https://discourse.itk.org/t/extracting-displacement-fields-from-rigid-registration/4834/14 "2022-04-13T17:28:40Z")

</div>

If you need it only in a subset of the image, it would take less time to produce it and less memory to store it.

---

<div class="post-metadata">

### Author: ![edgar](https://discourse.itk.org/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@edgar](https://discourse.itk.org/u/edgar)
#### Post date: [April 13, 2022, 7:32pm UTC](https://discourse.itk.org/t/extracting-displacement-fields-from-rigid-registration/4834/15 "2022-04-13T19:32:53Z")

</div>

Dear Dženan,

I would like to have the displacements (deformation) at each point.

With my moving smiles (the top-most is the reference; the one at the bottom is moved to the right; the difference between them is shown to the left)

 ![itk_hello_registration.diff+in](https://discourse.itk.org/uploads/default/original/2X/e/ebc4c51d24948ad2bfa2db8a68705f4de736efde.png), I was expecting to get zero deformation in the gray areas. With the results from `TransformToDisplacementFieldFilter` (by means of `itk.imwrite(displ_field_out, displ_field_vtk)`), I get a uniform field of vectors ![itk_translation_registration_displacements.magnitude](https://discourse.itk.org/uploads/default/original/2X/3/3bd0ee5900861ad8ea443bcc4d33c8cf8a4ac1d0.png). I am assuming that these vectors are the values of deformation.

Since the field is uniform, what I am assuming is that the overall displacement of the smile is applied in the whole region. That is, having used `fixed_image` (which is the full original image) results in the overall value being applied everywhere.

Therefore, I would think that I need to calculate the deformation at each point… but isn’t that the goal of `TransformToDisplacementFieldFilter`? (to get the deformation at each point?). Thus, I must be doing something wrong.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [April 13, 2022, 7:46pm UTC](https://discourse.itk.org/t/extracting-displacement-fields-from-rigid-registration/4834/16 "2022-04-13T19:46:11Z")

</div>

Displacement field provides translation vector at each point. But if you construct it by computing rigid transform for each of those points, you get a rather uniform DF. So if you have a rigid transform, it is best to keep it in some specialized format (a few numbers specifying translation, rotation and center of rotation).

What you might be looking for is deformable registration. But even with deformable registration, most algorithms would produce movement in the gray areas between the eyes. Why? Because if they didn’t, it would imply huge deformations around the eyes and mouth and no deformation for the rest of the face. Put another way: if your head is translated, each point of your forehead is translated too, not just the eyes.

---

<div class="post-metadata">

### Author: ![edgar](https://discourse.itk.org/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@edgar](https://discourse.itk.org/u/edgar)
#### Post date: [April 14, 2022, 9:20am UTC](https://discourse.itk.org/t/extracting-displacement-fields-from-rigid-registration/4834/17 "2022-04-14T09:20:57Z")

</div>

As always, thank you very much. This will certainly help me.
