# Cast/Create DisplacementField Python

**URL:** https://discourse.itk.org/t/cast-create-displacementfield-python/2610
**Category:** Beginner Questions
**Created:** [January 12, 2020, 9:29am UTC](https://discourse.itk.org/t/cast-create-displacementfield-python/2610 "2020-01-12T09:29:28Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![SiPyWiFi](https://discourse.itk.org/user_avatar/discourse.itk.org/sipywifi/32/1321_2.png) [@SiPyWiFi](https://discourse.itk.org/u/SiPyWiFi)
#### Post date: [January 12, 2020, 9:29am UTC](https://discourse.itk.org/t/cast-create-displacementfield-python/2610/1 "2020-01-12T09:29:28Z")

</div>

Dear All!

I have only been using ITK for a short time.  
In my project the goal is to transform an input image with a displacement field.  
For this I want to use the Python version.

My problems is/are (They are linked):  
a) I cannot create the displacement field with the data type i want.  
b) I cannot change the datatype of the displacement field.

I created a Displacement field with the following code:  
field = itk.DisplacementFieldTransform[itk.D, 2].New()

a)  
Double is the only allowed data type (-\> checked it with itk.DisplacementFieldTransform.GetOutputs()).  
I think my problem would be solved if the function would allow other data types.

* * *

To transform the image i followed the example:  
[https://itk.org/ITKExamples/src/Filtering/ImageGrid/WarpAnImageUsingADeformationField/Documentation.html](https://itk.org/ITKExamples/src/Filtering/ImageGrid/WarpAnImageUsingADeformationField/Documentation.html)

b)  
To cast the displacement field to the right data type I wanted to use itk.CastImageFilter.  
Because the C++ Function Itk.VectorCastImageFilter is not implemented in ITK python.  
Which results in the Error:  
"itk.CastImageFilter is not wrapped for input type `itk.Image[itk.Vector[itk.D,2],2], itk.Image[itk.Vector[itk.F,2],2]`"

Summary:  
itk.WarpImageFilter needs itk.Image[itk.Vector[itk.F,2],2] -\> I can only create itk.Image[itk.Vector[itk.D,2],2] -\> I havenot managed to cast it to the right dtype.

What can I do?

Best Regards Simon

---

<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: [January 12, 2020, 5:53pm UTC](https://discourse.itk.org/t/cast-create-displacementfield-python/2610/2 "2020-01-12T17:53:29Z")

</div>

Hello Simon!

Welcome to the ITK community! 🌞

Good questions.

`itk.Vector[itk.D, Dimension]` support is currently missing in the binary ITK Python packages – we will add it to the ITK 5.1 RC 2 release. Then, the displacement field image can be read with `itk.imread` or `itk.ImageFileReader`, or cast with `itk.cast_image_filter`. The transform can then be set up with

```auto
transform = itk.DisplacementFieldTransform[itk.D, 2].New()
# field is the itk.Image[itk.Vector[itk.D, 2]
transform.SetDisplacementField(field)

```

---

<div class="post-metadata">

### Author: ![SiPyWiFi](https://discourse.itk.org/user_avatar/discourse.itk.org/sipywifi/32/1321_2.png) [@SiPyWiFi](https://discourse.itk.org/u/SiPyWiFi)
#### Post date: [March 20, 2020, 1:41pm UTC](https://discourse.itk.org/t/cast-create-displacementfield-python/2610/3 "2020-03-20T13:41:40Z")

</div>

Hi,  
thank you for adding it to the new release!

This is my solution, if anyone else has the same problem:

```
VectorDimension = 3
PixelType = itk.Vector[itk.F, VectorDimension]

ImageDimension = 3
ImageType = itk.Image[PixelType, ImageDimension]

DisplacementFieldNumpy = np.zeros((100, 100, 100, 3)).astype(np.float32)
PyBuffer = itk.PyBuffer[ImageType]
VectorImage = PyBuffer.GetImageFromArray(displacementField)

```

Best Regards  
Simon
