Conversion of coordinate system from SCS to world coordinate when exporting the mesh

Hi,
I am new to brainstorm. I need a transform for converting the fem mesh coordinate system.

Is there a way to convert the coordinate system to RAS or LPS?

Is there any trnasform available to do conversion of FEM mesh generated by roast.

Regards,
Saima Safdar

Converting between coordinate systems is always complicated, especially if you are trying to convert with conventions that are not directly supported in Brainstorm.

For converting between the systems of coordinates in Brainstorm, use cs_convert/cs_compute, as explained in the tutorial. If you need more information about these functions, please refer directly to the comments in the code.
https://neuroimage.usc.edu/brainstorm/CoordinateSystems

The easiest way to get the FEM meshes from ROAST into Brainstorm is to start ROAST from Brainstorm, all the outputs should be converted automatically:
https://neuroimage.usc.edu/brainstorm/Tutorials/FemMesh#ROAST

If you want to do this manually, you can check how this is done in the code:

I am working on this with Saima. Basically we want to save the mesh in the same coordinate system as the MRI that is used to generate the mesh. In 3D Slicer this is the RAS coordinate system which I think corresponds to the world coordinate system in Brainstorm. Is this the correct way to save the mesh in the world coordinate system:

% NOTE: First do these steps:
% 1. Export to Matlab the MRI_T1 as 'mri'.
% 2. Export to Matlab the mesh as 'mesh'.

% Output file to save mesh
OutputFile = 'mesh.msh'

% Convert CSC to World coordinate system
% See: https://github.com/brainstorm-tools/brainstorm3/blob/master/toolbox/process/functions/process_fem_mesh.m#L899
[mesh.Vertices, Transf] = cs_convert(mri, 'scs', 'world', mesh.Vertices(:,1:3));

% Save mesh as Gmsh format
out_fem_msh(mesh, OutputFile)
1 Like

Yes, this code converts the FEM mesh vertices to "Brainstorm world coordinates".
You can replace mesh.Vertices(:,1:3) with simply mesh.Vertices.
And if you need to script this, you can make database requests to get the MRI and mesh file names, and then load them from your script. Read the Scripting tutorial for help with this: https://neuroimage.usc.edu/brainstorm/Tutorials/Scripting

However, these coordinates depend on the information saved in the MRI structure at the time of the export. If there was only the qform transformation defined in the original .nii file and the MRI was not modified in Brainstorm, there should be no ambiguities, and the output FEM mesh should be the space defined by this qform matrix.

All the other cases can lead to unwanted results and must be checked carefully step by step. Examples of unsafe cases: the original file was not a .nii file, the MRI was edited or resampled in Brainstorm, there was both a qform and a sform defined in the original .nii...

Furthermore, I can't confirm that this is what you are supposed to use in 3D Slicer, as I have no experience with this software.

Hi,

  1. we converted the mri T1 and T2 to niffti format using 3D slicer
  2. We loaded the mri T1 and mri T2 as niifti files in a new subject
  3. we did the segmentation using roast
  4. we exported the mri and mesh into matlab
  5. we multiplied the vertices with 1000 to get millimeters to view in 3D Slicer
  6. We exported the mesh vtk file after transforming the msh using gmsh to vtk
    and it does not corresponds to the coordinates in the mri file as seen in the image below.

Is there any step we are missing to do conversion. We do not use scs coordinate system.

Did you start the segmentation with ROAST from Brainstorm?
If you overlay the FEM mesh with the anatomy in a 3D figure in Brainstorm: does it look correctly aligned?

I'm still a bit confused by the way the coordinates of the ROAST results are handled, as it goes through iso2mesh as well... See the code:

But if it looks good within Brainstorm, then this is taken care of correctly by Brainstorm and you should not have to worry about the fact that there are many intermediates.

we multiplied the vertices with 1000 to get millimeters to view in 3D Slicer

All the coordinates in Brainstorm are saved in SCS coordinates.
If you are using surfaces files that you found in the Brainstorm database, they are in SCS coordinates.
In order to use then somewhere else, you need to convert them to "world" or "mni" coordinates.

We exported the mesh vtk file after transforming the msh using gmsh to vtk

I have no expertise to comment on this part.

Good luck!

Hi,
I am writing in detail all the steps we did, we may be not doing the right way,

  1. Create the default protocol

  2. load mri T1 and T2 into the default protocol

  3. select T1 and create a segmentation using brain2Mesh

  4. the segmentation overlaid with mri_t1

    align_anotomy

  5. converting the coordinate system from scs to world using the code which Ben posted above

  6. Exporting mesh to 3d slicer it now looks like

Regards,
Saima Safdar

Hi,
How can I have images original coordinate system LPS, instead of world, or world and LPS are same.

Regards,
Saima Safdar

Thank you, we found the solution to it. We just selected the coordinate system from within the slicer to RAS and now its like this.

In some unusual cases (e.g. when there is both a sform and a qform in the MRI, none of them, or when the MRI is not coming), the MRI 'world' coordinates are modified within Brainstorm. In that case, you need to export again the MRI as a .nii from Brainstorm.
Try using a .nii file exported from Brainstorm instead of the original .nii file.

The export to .msh might require some additional transformations, in order to bring the coordinates to a different convention. Each program does a different thing, making this transformation of coordinate systems always very complicated.
I'm sorry, I have no direct experience with .msh files or Slicer, I can't help you further with this topic.

1 Like

I am a bit confused about this. What happens when the SCS coordinate system is not defined? It looks like when this is the case there is no transform applied when converting from voxel to SCS coordinates on exporting the mesh:

The transform is here:

If the SCS field is empty then the transform is P=[]. To me it appears that the mesh is saved in voxel coordinates when the SCS coordinates are not defined. Is this correct?

In our case we have already registered the T1 and T2 images using different software and it seems that defining the SCS coordinate system will force the images to be registered again and resampled which we do not want. What is the best way to load the MRIs to ensure that the output remains compatible with the input images?

The SCS coordinates MUST be defined before generating or importing meshes, otherwise nothing in Brainstorm can work correctly.
In most combinations of inputs, it the NAS/LPA/RPA points are not defined in the MRI, Brainstorm starts by computing a basic linear MNI registration in order to get default fiducials, and therefore a default SCS transform.

If you find processes that you can run completely without errors and that do not define the SCS on the fly, please let me know, we need to add either errors or a computation of default MNI-based fiducials.

To me it appears that the mesh is saved in voxel coordinates when the SCS coordinates are not defined

"voxel" and "mri" coordinates are internal conventions, they should not be compared with anything outside. The only two correct options for comparing coordinates with other programs are : 1) "world coordinates" with a .nii volume exported from Brainstorm and 2) MNI coordinates.

In our case we have already registered the T1 and T2 images using different software and it seems that defining the SCS coordinate system will force the images to be registered again and resampled which we do not want. What is the best way to load the MRIs to ensure that the output remains compatible with the input images?

Defining SCS coordinates is only about setting NAS/LPA/RPA points (manually or automatically) in the first MRI volume you import in the database (the T1 image). It does not change anything to the "world" coordinates, it does not require to re-register or re-resample the volumes (you just have to answer NO/IGNORE when importing the second volume, the T2).

Thank you for this very helpful clarification.

How can we automatically create the NAS/LPA/RPA points? Is it done using the MNI normalization?

I was able to succesfully create a FEM mesh that corresponds to the images in 3D Slicer using the following steps:

  1. Create a new subject using "individual anatomy".

  2. Import the T1 MRI in niftii format, applying the image transform.

  3. Perform MNI normalization of the T1 MRI to Create SCS coordinate system using the maff8 method.

  4. Import the T2 MRI, applying the image transform, registering it to the T1 MRI using the SPM method and reslicing this volume. Without registration and resampling the brain2mesh step fails when using both the T1 and T2 MRIs.

  5. Select both the T1 and T2 MRIs and generate the FEM mesh using the brain2mesh method.

  6. Export the mesh and T1 MRI to Matlab variables called 'mesh' and 'mri'.

  7. Convert the coordinates from SCS to world coordinates using this code:

    [mesh.Vertices, Transf] = cs_convert(mri, 'scs', 'world', mesh.Vertices(:,1:3));
    
  8. Scale the coordinates of the mesh using this code:

    mesh.Vertices = 1000 * mesh.Vertices
    
  9. Save the mesh to Gmsh format using this code:

    out_fem_msh(mesh, 'mesh.msh')
    
  10. Open "mesh.msh" in Gmsh and export to "mesh.vtk".

  11. Open "mesh.vtk" in Slicer using RAS coordinate system.

The mesh overalyed on the T1 MRI looks like this:

1 Like

Yes. It sets automatically all the fiducials if there are not defined yet.

I was able to succesfully create a FEM mesh that corresponds to the images in 3D Slicer using the following steps:

Looks good!
Thanks for reporting your findings, it may help other users in the future.

Hi Francois,
Is there any method to convert the hexahedral mesh generated by fieldtrip to .msh file format similar to out_fem_msh.

The file out_fem_msh only converts tetrahedral meshes to .msh file format.

Thank you

Regards,
Saima Safdar

You can right-click on the FieldTrip hexa mesh > Convert tetra/hexa.
This calls function process_fem_mesh('SwitchHexaTetra', FemFile) to split the hexahedrons into tetrahedrons.
Then you can export the result to a .msh file.