# ITK Python Image of itk.D Vectors For Composite Transform

**URL:** https://discourse.itk.org/t/itk-python-image-of-itk-d-vectors-for-composite-transform/5122
**Category:** Beginner Questions
**Created:** [June 21, 2022, 9:32pm UTC](https://discourse.itk.org/t/itk-python-image-of-itk-d-vectors-for-composite-transform/5122 "2022-06-21T21:32:17Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![josebouza](https://discourse.itk.org/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@josebouza](https://discourse.itk.org/u/josebouza)
#### Post date: [June 21, 2022, 9:32pm UTC](https://discourse.itk.org/t/itk-python-image-of-itk-d-vectors-for-composite-transform/5122/1 "2022-06-21T21:32:17Z")

</div>

Hello,

I am trying to convert a numpy array to an image of type `itk.Image[itk.Vector[itk.D,3],3]` in python ITK. The issue is that most ITK filters do not support this type. I would rather avoid having to compile my own ITK version, so I am trying to figure out if there is any workaround. Here are some things I’ve tried:

- Use `itk.image_from_array` to load numpy array into `itk.VectorImage` type, and then use `CastImageFilter` to convert this to `itk.Image[itk.Vector[itk.D,3],3]`. Python CastImageFilter does not support this output type.
- Use `itk.image_from_array` to load numpy array into three different `itk.Image[itk.D,3]` images and then combine them with `itk.ComposeImageFilter`. Again, `itk.ComposeImageFilter` does not support `itk.Image[itk.Vector[itk.D,3],3]` as an output type.

The reason I need `itk.Image[itk.Vector[itk.D,3],3]` is to initialize a `itk.DisplacementFieldTransform` object, which only appears to support images of vectors (not `itk.VectorImage`). Any help would be greatly appreciated!

---

<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: [June 27, 2022, 12:07pm UTC](https://discourse.itk.org/t/itk-python-image-of-itk-d-vectors-for-composite-transform/5122/2 "2022-06-27T12:07:52Z")

</div>

Have you tried casting your numpy array into 32-bit floats, then using `itk.Image[itk.Vector[itk.F,3],3]`? There is usually no need for such high precision for displacement fields, hence it is not pre-built.

---

<div class="post-metadata">

### Author: ![josebouza](https://discourse.itk.org/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@josebouza](https://discourse.itk.org/u/josebouza)
#### Post date: [June 27, 2022, 1:50pm UTC](https://discourse.itk.org/t/itk-python-image-of-itk-d-vectors-for-composite-transform/5122/3 "2022-06-27T13:50:38Z")

</div>

Thank you for the reply. The reason for wanting a displacement field of type `itk.Image[itk.Vector[itk.D,3],3]` is that I eventually want to use the displacement field in a CompositeTransform and `itk.CompositeTransform` only exposes the template class for type `itk.D`.

Maybe a future ITK version should expose `itk.CompositeTransform` for `itk.F` to make this use case easier.

---

<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: [June 27, 2022, 1:59pm UTC](https://discourse.itk.org/t/itk-python-image-of-itk-d-vectors-for-composite-transform/5122/4 "2022-06-27T13:59:00Z")

</div>

@Tom_Birdsong @Pranjal_Sahu did you run into this issue, and if so how did you solve it?

---

<div class="post-metadata">

### Author: ![Pranjal\_Sahu](https://discourse.itk.org/user_avatar/discourse.itk.org/pranjal_sahu/32/2698_2.png) [@Pranjal\_Sahu](https://discourse.itk.org/u/Pranjal_Sahu)
#### Post date: [June 27, 2022, 2:03pm UTC](https://discourse.itk.org/t/itk-python-image-of-itk-d-vectors-for-composite-transform/5122/5 "2022-06-27T14:03:48Z")

</div>

Please check this test file.

> <https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Filtering/DisplacementField/wrapping/test/itkDisplacementFieldTransformTest.py>

---

<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: [June 27, 2022, 2:57pm UTC](https://discourse.itk.org/t/itk-python-image-of-itk-d-vectors-for-composite-transform/5122/6 "2022-06-27T14:57:52Z")

</div>

This test does not address composite transform use of DF transform.

---

<div class="post-metadata">

### Author: ![Anton\_Naumov](https://discourse.itk.org/user_avatar/discourse.itk.org/anton_naumov/32/3900_2.png) [@Anton\_Naumov](https://discourse.itk.org/u/Anton_Naumov)
#### Post date: [February 2, 2024, 12:12pm UTC](https://discourse.itk.org/t/itk-python-image-of-itk-d-vectors-for-composite-transform/5122/7 "2024-02-02T12:12:55Z")

</div>

I ran into exactly the same issue – one can define the DisplacementField transform of a float type, but cannot add it to a composite transform since it can only be of a double precision.

@Pranjal_Sahu, I’ve checked your file, but it doesn’t cover the CompositeTransform case.

Any improvements or ideas? The only workaround I’ve found is to cast dvf through numpy, but it is ugly ☹

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [February 2, 2024, 6:59pm UTC](https://discourse.itk.org/t/itk-python-image-of-itk-d-vectors-for-composite-transform/5122/8 "2024-02-02T18:59:34Z")

</div>

Hi,

We can also wrap Composite Transform for `itk.F` – here is a patch:

> <https://github.com/InsightSoftwareConsortium/ITK/pull/4444>
>
> For the use case of a float32 displacement vector field.
