Cast/Create DisplacementField Python

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()

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:

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]"

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

Hello Simon!

Welcome to the ITK community! :sun_with_face:

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

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

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

1 Like