# Import displacement field from Matlab

**URL:** <https://discourse.slicer.org/t/import-displacement-field-from-matlab/21224>\
**Category:** Support\
**Tags:** registration, python, file-import\
**Created:** [December 27, 2021, 2:01am UTC](https://discourse.slicer.org/t/import-displacement-field-from-matlab/21224 "2021-12-27T02:01:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Engineer90](https://avatars.discourse-cdn.com/v4/letter/e/87869e/32.png) [@Engineer90](https://discourse.slicer.org/u/Engineer90)\
**Post date:** [December 27, 2021, 2:01am UTC](https://discourse.slicer.org/t/import-displacement-field-from-matlab/21224/1 "2021-12-27T02:01:17Z")

</div>

Hi everyone.

I was wondering if on 3D Slicer it was possible to import a registration matrix that I obtained with Matlab (using an optical flow algorithm), so to apply it to a segment.  
In my case I have three 512x512x306 matrices (pixel \* pixel \* number of slices), each one containing the displacements of each pixel respectively in x, y, z direction.

Thank you very much,  
kind regards

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [December 28, 2021, 4:44pm UTC](https://discourse.slicer.org/t/import-displacement-field-from-matlab/21224/2 "2021-12-28T16:44:10Z")

</div>

Yes, you can import a displacement field into Slicer very easily. All you need is to save the displacement field matrix into a nrrd file using [nrrdwrite.m](https://github.com/PerkLab/SlicerMatlabBridge/blob/master/MatlabCommander/commandserver/nrrdwrite.m) and [load that file into Slicer as a Transform](https://slicer.readthedocs.io/en/latest/user_guide/modules/transforms.html#load-transform).

---

<div class="post-metadata">

**Author:** ![Engineer90](https://avatars.discourse-cdn.com/v4/letter/e/87869e/32.png) [@Engineer90](https://discourse.slicer.org/u/Engineer90)\
**Post date:** [December 28, 2021, 6:46pm UTC](https://discourse.slicer.org/t/import-displacement-field-from-matlab/21224/3 "2021-12-28T18:46:49Z")

</div>

I tried with my 4D matrix 512x512x205x3 (where 3 is x, y, z displacement), and I got this error:

_Undefined function ‘nrrdaddmetafield’ for input arguments of type ‘struct’._

_Error in nrrdwrite (line 117)_

- img = nrrdaddmetafield(img,‘MultiVolume.NumberOfFrames’,size(img.pixelData,4));\*

_Error in PROVAnrrd (line 45)_  
_nrrdwrite(‘testOutput\_x.nrrd’, img);_

So I divided the 4D displacement matrix into three 3D displacement matrices (512x512x205), each one containing x or y or z displacements. In this case nrrdwrite works.

However when I import one of these three matrices into 3D slicer I get this:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/e/8/e83640991797dc83c35107edaf5d09a09da1d74a.png)

Despite the grid origin, grid size … are correct (comparing with others transformation, for example by Elastix).
