Create GridTransform from 3d motion field

llafitte007
Tue, 26 Jul 2022 11:26:35 +0000
Support

Hi,

I am developing a motion estimation module based on an existing library ; the result of the estimate is 3d vector field of type numpy array (shape = (width, height, depth, 3)) plus other fields (origin, spacing, …).

If I save the motion result from my program (nifti format) and open it as Transformation from slicer it works great: the visualization and the registration of motion are great!

My problem comes in the automatic conversion, in the module, of the estimated motion into a GridTransform. Several utils approach the solution (slicer.util.addVolumeFromArray, slicer.util.loadTransform) but nothing that I can’t use for my case. Would there be an equivalent to slicer.util.addVolumeFromArray for GridTransforms (like slicer.util.addGridTransformFromArray) ?

I tried to manually convert motion field and create GridTransform without success:

# found here https://discourse.slicer.org/t/how-to-convert-3d-numpy-array-to-vtk-and-save-the-vtk-file/22327/3
def numpyToVTK(data):
    data_type = vtk.VTK_FLOAT
    shape = data.shape
    flat_data_array = data.flatten()
    vtk_data = vtk.util.numpy_support.numpy_to_vtk(num_array=flat_data_array, deep=True, array_type=data_type)
    img = vtk.vtkImageData()
    img.GetPointData().SetScalars(vtk_data)
    img.SetDimensions(shape[0], shape[1], shape[2])
    print(img)
    return img

motionNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLGridTransformNode")
motionVtk = numpyToVTK(motionData)
motionNode.GetTransformFromParent().SetDisplacementGridData(motionVtk)
motionNode.GetTransformFromParent().GetDisplacementGrid().SetOrigin(motionOrigin)
motionNode.GetTransformFromParent().GetDisplacementGrid().SetSpacing(motionSpacing)

Can anyone help me ? Thanks

2 posts - 2 participants