Save a displacement field as a nifti image with SimpleITK in Python

Hello everyone,

I am a beginner of SimpleITK in python. I’ve saw that SimpleITK could save a displacement field as an image but I didn’t find how to do it. So are there anyone can help me please? Thank you very much.

The DisplacementFieldTransform class has a GetDisplacementField method which returns an Image. You can then pass that Image to the WriteImage function to write it in whatever image format you prefer.

The code would look like this:

import SimpleITK as sitk
dft = sitk.DisplacementFieldTransform()
img = dft.GetDisplacementField()
sitk.WriteImage(img, "my_displacement_field.nii")

Here’s the documentation for the DIsplacementFieldTransform:
https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1DisplacementFieldTransform.html

2 Likes

Thank you very much. If I want to save a composite transform composed with an affine transform and a displacement field as a nifti image, is it possible?

1 Like

Hello @CYY,

The functionality you want is “save the transformation” not the displacement field image. For this we have ReadTransform and WriteTransform. See this jupyter notebook for details.

1 Like