Convert a displacement filed nifty image to SITK transform type

Hello everyone,

I have a displacement field Nifti format (field.nii.gz). How can I convert it to sitk.transform type (field.tfm) so that I can use it as transformation in SITK?

I really appreciate any help you can provide,
Vahdaneh

Hello @VHK,

This is easily done:

import SimpleITK as sitk

transform = sitk.DisplacementFieldTransform(sitk.ReadImage('field.nii.gz', sitk.sitkVectorFloat64))

For full details I recommend going through the toolkit’s Jupyter notebook repository and specifically the transformations notebook.

1 Like

What if I want to save from DisplacementFieldTransform type to Image type, and then save it in .nii.gz format?

Something similar to this ITK code should accomplish that:

auto imageDF = dfTransform->GetDisplacementField();
itk::WriteImage(imageDF, "filename.nii.gz");
1 Like

Hello @yuanpenggkate,

Assuming you are using SimpleITK in Python the answer is pretty much the same as @dzenanz gave for ITK in C++:

#transform is a DisplacementFieldTransform
sitk.WriteImage(transform.GetDisplacementField(), "displacement_field.nii.gz")