Search code examples
imagesimpleitknifti

How to save a Displacement Field Transform as an image in SimpleITK?


I want to save a DisplacementFieldTransform as an image in SimpleITK.

The documentation says:

A displacement field, such as one stored in a DisplacementFieldTransform object, can also be saved as an image (.nrrd, .nhdr, .mha, .mhd, .nii, .nii.gz).

But when I do

demons = sitk.FastSymmetricForcesDemonsRegistrationFilter()
demons.SetNumberOfIterations(15)
# Standard deviation for Gaussian smoothing of displacement field
demons.SetStandardDeviations(1.0)

demons.AddCommand( sitk.sitkIterationEvent, lambda: command_iteration(demons) )

displacementField = demons.Execute(followup, baseline)

deformation_field = sitk.DisplacementFieldTransform(displacementField)

deformationFieldPath = os.path.join(destinationDataPath, str(patientId) + '_deformationField.nrrd')

sitk.WriteTransform(deformation_field, deformationFieldPath)

I get the following error:

RuntimeError: Exception thrown in SimpleITK WriteTransform: /tmp/SimpleITK-build/ITK/Modules/IO/TransformBase/include/itkTransformFileWriter.hxx:167:
itk::ERROR: TransformFileWriterTemplate(0x555a72b3e700): Could not create Transform IO object for writing file /home/amasson/fastai/EMISEP-differences/6_deformationField.nrrd
  Tried to create one of the following:
    HDF5TransformIOTemplate
    HDF5TransformIOTemplate
    MatlabTransformIOTemplate
    MatlabTransformIOTemplate
    TxtTransformIOTemplate
    TxtTransformIOTemplate
  You probably failed to set a file suffix, or
    set the suffix to an unsupported type.

How can I save my DisplacementFieldTransform as an image?


Solution

  • The DisplacementFieldTransform has a GetDisplacementField (and GetInverseDisplacementField) method that returns an Image. With that Image object you can call WriteImage and export it to whichever image format you prefer.

    Here's the documentation for the DisplacementFieldTransform class: https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1DisplacementFieldTransform.html