Extensions/MatlabBridge: Send transform (4x4 matrix) from Matlab to 3D Slicer

I’m beginning to work now with Matlab. I’ve looked over descriptions and questions but some questions are not clear for me yet. So I was wondering if somebody could help me out:
I wanted to ask if it is possible to visualize a filtered volume (based on matrices 181x299x305 double - created from image slices) from a Matlab file in 3D Slicer via the MatlabBridge Module Generator? Or whether I need to take the OpenIGTLink where I would need to create libraries (…) first (based on Mex files? Is it possible to see the volume then from the 3 different angles? Can this volume be adjusted with certain modules too?
I’m very thankful for any kind of suggestions!

Yes. MatlabBridge Module Generator creates a skeleton that you customize to call the Matlab function you need.

Note that when you use Slicer’s MatlabBridge, Slicer drives the workflow: you load input volumes and other data into Slicer, choose processing inputs and outputs, and initiate processing in Slicer, and then when processing is completed, results automatically transferred to and displayed in Slicer.

Slicer uses OpenIGTLink under the hood to communicate with the Matlab process, but you will not need to worry about these details. There is no need to build any mex files (avoiding mex files was one of the important design requirements of MatlabBridge, as it is a nightmare to maintain them) - MatlabBridge includes an OpenIGTLink server implemented in native Matlab script. But again, it is an internal detail that should not matter for you.

Yes, the full arsenal of Slicer’s visualization tools are available for you. You can view any number of slices, in any orientation, volume renderings, surface renderings, fusing multiple volumes, etc.

Yes. You can implement complete processing workflows as a mixture of Slicer core modules and your Matlab modules.

I can give more specific answers if you describe the clinical problem that you would like to solve and how you plan to approach it.

Thank you very much for this detailed reply!
I´m trying to display in the 3d slicer a filtered volume, which consists out of 181x299x305 double matrices(=FilteredVolume). The size of the voxels are [0.0500 0.0500 0.0500].
The number of slides from which the volume was created are 181.
So I was trying to write it like this:



cli_imagewrite(double.FilteredVolume, img);

But then the error appears:

The class double has no Constant property or Static method named ‘FilteredVolume’.

Error in matrixmatlab1 (line 40)

I know that I´m missing out something, could you maybe give me a suggestion. I would be really pleased!

I just realized that I was posting just:

instead what I wrote:

Hi again,
I was trying also the way which was described in


and load the following code:

igtlConnection = igtlConnect(’…’,18944);
transform.name = ‘NeedleToTracker’;

startTime = igtlTimestampNow();

for t=1:1000
transform.matrix = FilteredVolume;
transform.timestamp = igtlTimestampNow();
igtlSendTransform(igtlConnection, transform);


And the Transform was loaded into the Slicer, but not as I expected
the volume is displayed like this:
instead of showing me a structure of vessels…

I am thankful for all help and information!

MatlabOpenIGTLinkInterface in PlusMatlabUtils can only send transformation matrices, not images.

There are two approaches to transfer images from Matlab to Slicer:

  1. MatlabBridge (I would recommend this)

Slicer drives Matlab, so you must not even start Matlab, but Slicer will, when the user runs your Matlab module in Slicer. You need to create a module using Matlab module generator, customize the XML file to specify user interface for your module, and customize the m file to customize the behavior of your module.

  1. Manually transfer through files

Save volume to file from Matlab using nrrdwrite.m and load the created .nrrd file into Slicer manually or by running a command like this:

"C:\Program Files\Slicer 4.9.0-2018-06-01\Slicer.exe" --python-code "slicer.util.loadVolume(r'c:\Users\msliv\Documents\Data\MRHead.nrrd')"

Thank you for your suggestions!

I was creating a new Module in Slicer, which worked (called ‘Matrix1’)
I was saving the needed Matrix in a mat file (‘matrixfilteredvolume.mat’) and I was loadinng it into the .m file from Matrix1 module. Then I was saving the mat file (FilteredVolume matrix) under a new variable called ‘Matrix’. I was changing the XML file like this:

<?xml version="1.0" encoding="utf-8"?>
  <description><![CDATA[Perform a simple image processing and image statistics computation in Matlab.]]></description>
  <contributor>Andras Lasso (PerkLab)</contributor>
  <acknowledgements><![CDATA[SparKit is a project funded by Cancer Care Ontarioand the Ontario Consortium for Adaptive Interventions in Radiation Oncology (OCAIRO) to provide free, open-source toolset for radiotherapy and related image-guided interventions.]]></acknowledgements>
    <label>Processing Parameters</label>
    <description><![CDATA[Parameters for the processing]]></description>
      <description><![CDATA[All voxels below this value will be set to zero]]></description>
    <description><![CDATA[Input/output parameters]]></description>
      <description><![CDATA[Input volume to be filtered]]></description>
      <description><![CDATA[Output filtered]]></description>
    <description>Matlab command output</description>
      <description><![CDATA[Image mininum]]></description>
      <description><![CDATA[Image maximum]]></description>

and where running the code of the .m file through the new Slicer module:

function outputParams=Matrix1(inputParams)

load 'matrixfilteredvolume.mat'

Matrix = FilteredVolume




cli_imagewrite(inputParams.'Matrix', img);

The following Error appeared:
Matrix1 standard error:

Failed to execute Matlab function: Matrix1, received the following error message:
ERROR: Command execution failed. Error: File: Matrix1.m Line: 16 Column: 32
Unexpected MATLAB expression.

Error in cli_commandserver (line 91)

Error in run (line 91)
evalin(‘caller’, strcat(script, ‘;’));

Did I load the matrix in a wrong way?

Have a closer look at the originally generated module skeleton. You refer to parameters by inputParams.variableName (not quoted like ‘variableName’).

Do you want to pass an image or a transformation matrix between Matlab and Slicer?

If you are just learning Matlab then I would recommend to consider using Python instead. While Matlab still has somewhat better IDE and documentation than Python but in most other things are Python is much more capable, it’s free, open-source, has huge community, many libraries, etc., so overall learning it is probably a better long-term investment.

I want to pass a matrix

Do you mean a 4x4 homogeneous transformation matrix?

it consists of the following parameters: 181x299x305 double

i= 181 are the slices from which it was created and the SizeVoxel are [0.050000000000000 0.050000000000000 0.050000000000000]

So, you want to pass an image volume. Then the default generated skeleton code should work as is. First don’t modify anything in it and see if it computes thresholded image. If everything is OK then modify the script step-by-step to see what changes cause errors.

i´m kind of stuck in the modifying part

I´m not sure wheter I should use this:

load ‘matrixfilteredvolume.mat’

Matrix = FilteredVolume




cli_imagewrite(Matrix, img);

or if I have to add infos about the slices and spaces too

Right now, your code has two issues:

  1. There is no such variable as Matrix. You’ve only set the label to Matrix in the XML file, but the variable name is generated from the longflag parameter.
  2. You added single-quotes around variable name. That’s invalid syntax, just remove the quotes.

Image origin, spacing, and axis directions are stored in img.ijkToLpsTransform. This matrix is a homogeneous transform, which transforms voxel (IJK) coordinates to physical (LPS) coordinates. Top-left 3x3 submatrix is axis direction and spacing. Top-right 3-element column vector is the image origin.

yes I was going over this function already but I don´t understand this part:

% Define origin, spacing, axis directions by a homogeneous transformation matrix:
img.ijkToLpsTransform = [ 1.2 0 0 10; 0 1.2 0 12; 0 0 3.0 -22; 0 0 0 1];

I was applying the following parameters:

load 'matrixfilteredvolume.mat'

Matrix = FilteredVolume

L = max(Matrix)

[X,Y,Z]=meshgrid(1:size(L,1), 1:size(L,2), 1:size(L,3))

img.pixelData = x/3+y/4+z/2;
% Define origin, spacing, axis directions by a homogeneous transformation matrix:
img.ijkToLpsTransform = [ 0.05 0 0 181; 0 0.05 0 299; 0 0 0.05 305; 0 0 0 1];
% Enable compression


nrrdwrite('testOutput.nrrd', img);

% Open file for writing
fid=fopen(outputFilename, 'w');

There is no such variable as Matrix. You’ve only set the label to Matrix in the XML file, but the variable name is generated from the longflag parameter.

so I have to change the location of ‘Matrix’ in the XML file to here:

      <description><![CDATA[All voxels below this value will be set to zero]]></description>

You added single-quotes around variable name. That’s invalid syntax, just remove the quotes.

where exactly do you mean?

sorry this part was lost in the reply:
so I have to change the location of ‘Matrix’ in the XML file to here:

      <description><![CDATA[All voxels below this value will be set to zero]]></description>