Export the displacement vector to matlab

I want to export the output vector field to the MATLAB. I used the B spline registration in Plastimatch module, and saved the Output vector field DVF in nrrd.
Then I read the DVF.nrrd using nrrdread.m. I compared the vector field shown in the Transforms module with the result of the corresponding voxel obtained by the MATLAB. And the two vector fields were completely different. Not the difference between positive and negative signs. Can you give some advice on how to export the vector field to the MATLAB correctly?

I am doing the same thing with you. I want to register two 3D CT volumes using B spline registration in Plastimatch module and use the displacement vector field in MATLAB.
The ‘Output vector field’ was saved as .nrrd format.
Then I read the dvf.nrrd using the nrrdread, and got the resullt as follows.

I want to check whether the vector field read in MATLAB is the same with the vector field shown in Transforms module. And they were completely different. Was your result right? What unit is the exported nrrd file? mm or pixel?

There are many things you need to pay close attention to:

  • you need to take into account the grid origin, spacing, axis directions to look up the correct matrix index
  • the matrix index may be offset by 1 in MATLAB (MATLAB starts indexing from 1)
  • the order of indices may be inverted (i, j, k vs. k, j, i)
  • physical coordinates and displacements may be in LPS or RAS coordinate system

Compared to Python and Python-wrapped toolkits (ITK, VTK, etc.) and applications (such as 3D Slicer) MATLAB is very limited when it comes to visualizing, processing, transforming 3D images. What is the rationale for trying to use MATLAB? Could you do your processing in Slicer’s Python environment instead? What is the overall goal of your project?

I want to use the vector field in matrad - an open source dose calculation for radiotherapy treatment planning. What is the unit of the output vector field, mm or pixel?

Everything is in millimeter.

If matrad has trouble reading in standard displacement fields then you can apply the displacement to any images, contours, dose maps, etc. in Slicer and hopefully matrad at least can read those. Note that you can also use SlicerRT for deformations, dose accumulation, DVH computation, DVH comparison, RT plan visualization, etc. SlicerRT also has some basic dose computation engines, but if those are not sufficient then you can use matrad to compute the dose and to the rest of the analysis via GUI or Python scripting using SlicerRT.

Thank you for your quick reply!I think I still need to use matrad. I also have some questions.
1 What is the difference between ‘Output B-spline transform’ and ‘Output vector field’?
2 Whether the calculated vector is backward mapping or forward mapping? Is the vector field a mapping from moving volume to fixed volume?
3 The calculated vector field is in mm units. Is the saved nrrd file also in mm units? During the save process, is there a coordinate conversion?

4 When I read the nrrd file using nrrdread.m, I also obtain an ijkToLPSTransform matrix. is it used to transform pixel units (ijk coordinate system ) to mm uints (physical LPS coordinate system)? Am I to understand that the data read by nrrdread.m is in pixel units?

B-spline transform contains B-spline coefficients, while the vector field contain displacement vectors.

Image registration always computes vector from the fixed to the moving image. You can easily compute the inverse transform in Transforms module in Slicer (click “Invert” button then and then use Convert section to export to a new displacement field).

Physical coordinate system units is always millimeters, regardless of what file format is used.

Everything is in physical units. You just need IJK to LPS transform to get the index of the matrix element where the displacement vector has to be read from.

I active the ‘dvf’ vector field (the corresponding nrrd file is read in matlab) in the Transforms module. The values shown in the ‘Displacement vector RAS’ is the vector field value not the B-spline coefficients, right? And what is the difference between ‘Transform to patient’ and ‘Transform from patient’? Thank you very much!

Yes, “Displacement vector RAS” contains the actual displacement vector values. Transform to/from a coordinate system indicates the direction (“from” transform is the inverse of the “to” transform).