# Create GridTransform from 3d motion field

**URL:** <https://discourse.slicer.org/t/create-gridtransform-from-3d-motion-field/24496>\
**Category:** Support\
**Tags:** scripted\
**Created:** [July 26, 2022, 11:26am UTC](https://discourse.slicer.org/t/create-gridtransform-from-3d-motion-field/24496 "2022-07-26T11:26:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![llafitte007](https://avatars.discourse-cdn.com/v4/letter/l/7ba0ec/32.png) [@llafitte007](https://discourse.slicer.org/u/llafitte007)\
**Post date:** [July 26, 2022, 11:26am UTC](https://discourse.slicer.org/t/create-gridtransform-from-3d-motion-field/24496/1 "2022-07-26T11:26:35Z")

</div>

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:

```auto
# 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

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [July 26, 2022, 1:11pm UTC](https://discourse.slicer.org/t/create-gridtransform-from-3d-motion-field/24496/2 "2022-07-26T13:11:14Z")

</div>

Here are a couple examples of creating grid transforms in python. Yes, a utility function could be helpful so if you want to try abstracting this code a PR would be great.

> <https://github.com/Slicer/Slicer/blob/3328b81211cb2e9ae16a0b49097744171c8c71c0/Modules/Scripted/DICOMPlugins/DICOMScalarVolumePlugin.py#L621-L648>

> <https://github.com/Slicer/LandmarkRegistration/blob/master/RegistrationLib/ThinPlatePlugin.py#L83-L118>
