Time average of sequence of transforms

Hi all,

I am recently working with a time series of volumes. First, I obtained a sequence (Sequence module) of volumes and then registered them using the SequenceRegistration module obtaining the corresponding Transforms and displacement fields. I would like to obtain the time-averaged transformation. Is there a way in Slicer or also in Python to perform the time average operation over the sequence of transformations?

Also, can I apply mathematical operations (such as invention or composition) to these transforms?
Thanks a lot!

You can write a small Python code snippet that iterates through the sequence for each time point it exports the transform into a displacement field vector volume, and you copy the voxels of the volume into a numpy array. In the end you can concatenate the arrays and use numpy to compute statistics along any axis (e.g., compute mean along time axis).

1 Like

Thanks for the suggestion! Once the time-averaged displacement field is loaded back in Slcier, how can I apply it on a Volume? Can I convert a Displacement Field into a Transform?
P.S. is there any example of a snippet code like the one you mentioned?

You can load a displacement field image file (e.g., in a nrrd file) as a transform. Or you can manually create a grid transform and set the displacement image into it.

1 Like

I converted all the Transform nodes into Displacement Fields with:
displacement_field_volume =slicer.modules.transforms.logic().CreateDisplacementVolumeFromTransform(transformNode, referenceVolumeNode, False)

Then I tried to extract the corresponding array in this way:
displacement_field_volume_node = slicer.util.getNode(displacement_field_volume.GetID())
array_displacement_field = slicer.util.arrayFromVolume(displacement_field_volume_node)

But slicer gives the error:
File “/opt/Slicer-4.11.20210226-linux-amd64/bin/Python/slicer/util.py”, line 1443, in arrayFromVolume
narray = vtk.util.numpy_support.vtk_to_numpy(vimage.GetPointData().GetScalars()).reshape(nshape)
ValueError: cannot reshape array of size 1720320 into shape (28,160,128)

It seems that the arrayFromVolume tries to reshape all the 3 channels of the displacement fields together, do you know how to solve this?