ANTsX / ANTs

Advanced Normalization Tools (ANTs)
Apache License 2.0
1.18k stars 381 forks source link

Writing ITK affine transformation from Matlab #1083

Closed jsebgiudice closed 4 years ago

jsebgiudice commented 4 years ago

Hello, I'm making modifications to the affine transformation in matlab and am having issues applying the affine transformation matrix that I am exporting from matlab. My workflow is:

  1. Load .mat affine transformation file into matlab
  2. Make modifications to the 15 parameters and separate them into variables that are named following the convention seen when loading the affine transformation .mat file
  3. Save a new .mat file with the "AffineTransform_double_3_3" and "fixed" variables. When I load this new .mat file it looks exactly the same as when I load the affine .mat file generated by ants/itk.

When I run the antsApplyTransforms command with this modified .mat file, I get an error saying that ITK can't read the .mat file. I'm guessing that this is due to the fact that the itk affine transformation .mat files are "matlab-like" and not strictly matlab files, but I'm not sure what the workaround is here, if there is one. Is there a way to generate affine transformation using matlab that plays nicely with the antsApplyTransforms function? Or maybe there's a way to edit an affine transformation file using ants?

Thanks! Sebastian

ntustison commented 4 years ago

I guess I don't quite understand why Matlab is being utilized at all. If you modify the .mat file, as a text file within a simple text editor, you should have no problem loading it into ANTs/ITK. What am I missing in understanding your workflow?

jsebgiudice commented 4 years ago

Hi Nick, thanks for getting back to me!

The reason I'm using Matlab is that I'm doing a PCA on the brain registration outputs, including the affine transform parameters and the deformation field image. In my previous comment, in step 2 I'm making modifications to the 15 parameters based on the PCA results and then I'm hoping to apply that transformation to the images to visualize the different principal components. So essentially I'm creating "new" affine transformation matrices that I'm trying to apply to an image using the antsApplyTransforms function.

This might be a stupid question, but aren't the .mat files binary files? When I load it into a text editor it looks like a typical binary file. If there's a way to edit and save it as a text file that would be great, but I think I might be missing something here...

gdevenyi commented 4 years ago

You could use the "--minc" transforms instead, which are a text type with documentation: https://en.wikibooks.org/wiki/MINC/Tools/mni_autoreg

cookpa commented 4 years ago

.mat files can get confusing becaue of the various related contexts for .mat files.

The .mat files written by antsApplyTransforms are binary, but they are not Matlab files, they are ITK transforms that should be read with ITK transform I/O. The c3d command in ITK-SNAP can convert these to text.

There are other .mat files in text format used by other software, like FSL's flirt. These can be converted (with some caveats) by c3d to an ITK text format.

gdevenyi commented 4 years ago

alternatively, this appears to be a matlab->ITK transform writer: https://github.com/PerkLab/SlicerMatlabBridge/blob/master/MatlabCommander/commandserver/cli_lineartransformwrite.m

ntustison commented 4 years ago

No need for external packages. Just use ConvertTransformFile in ANTs to convert to text format, modify with a text editor, and then read it into antsApplyTransforms.

jsebgiudice commented 4 years ago

Thanks everyone for your input!

Nick - that's perfect. I dont even need to convert any files. As long as I can see the file format I can just create a new file with that format. Thanks!