Loading transformation from Matlab .mat file

Hi,

I tried to load a FSL generated .mat transformation matrix as a transform but the system crashed. The simple goal is to use the .mat matrix to transform vtk objects into a different imaging space; using 3d Slicer, I envision dragging the vtk objects onto the transform node and hardening the transformation and saving the transformed vtk objects.

Thanks for your help.

Instead of saving transform in proprietary Matlab mat file format, could you save the transform into ITK transform file format? Slicer can directly read/write ITK transform files.

You can use Slicer’s MatlabBridge extension to run Matlab functions directly from Slicer. See more information here: https://www.slicer.org/wiki/Documentation/Nightly/Extensions/MatlabBridge

You can also use this Matlab function that writes a linear transformation matrix to an ITK transformation file: https://github.com/PerkLab/SlicerMatlabBridge/blob/master/MatlabCommander/commandserver/cli_lineartransformwrite.m

Thanks Andras. I believe ITK stores the matrix in LPS format but from what I see in the cli_lineartransformationwrite.m function, there is the option to save transformation matrix in RAS. My images are in RAS so I’m guessing that I’d have to set the transformType to ‘slicer’?

ITK transform is always stored as LPS. The flag controls if you work in RAS or LPS, so that it converts to/from LPS transforms files accordingly.

1 Like

Hi,

I tried to load the FSL generated .mat transformation matrix into Matlab and got the following error message:

>> a = load('affine_ct_mr.mat')
Error using load
Unable to read MAT-file C:\Users\Vinit\Documents\MATLAB\affine_ct_mr.mat. Not a binary MAT-file. Try load -ASCII to read as text.

So, I had converted the .mat array into a .txt file using FSL and was able to read this into Matlab as a 4x4 array:

>> b = load('affine_ct_mr.txt')

b =

    0.9892    0.0481    0.1165  -13.8786
   -0.0145    0.9642   -0.2878   13.6338
   -0.1213    0.2732    0.9450   12.4001
         0         0         0    1.0000

When I attempt to run the cli_lineartransformwrite.m function, I get the following error message:

>> cli_lineartransformwrite('affine_ct_mr_slicer', b, 'slicer')
Undefined function or variable 'cli_transformwrite'.

Error in cli_lineartransformwrite (line 31)
cli_transformwrite(outputfilename, allTransforms);

Matlab requires all necessary .m files to be in the current working directory or in the path. Either change the working directory to .../MatlabCommander/commandserver or add this folder to the path in Matlab.

As it seems that you are very new to Matlab, I would advise to spend minimum necessary time with it, and instead focus your efforts on learning Python, which is a much more versatile language, with many more libraries, much larger community, and it is completely open-source and free.

1 Like

Actually, I’m modifying my workflow to include doing intermodality registrations directly in 3d Slicer based on our convos. This way transformations can be directly calculated without requiring an intermediary Matlab step. I fully agree with what you are saying about Python and open-source. I’ve been doing my neuroimaging work in Nipy precisely for the reasons of versatility and support; the very same reasons why I’m using 3d Slicer. Appreciate all your help.

1 Like