How to apply Convert3D transform file in MATLAB

890 views
Skip to first unread message

j.cos...@jatneurolab.com

unread,
May 30, 2018, 3:14:47 PM5/30/18
to itksnap-users
Is there a way to apply a saved Convert3D transformation file (.mat) to a Nifti image in MATLAB? I attempted using 'affine3d' then 'imwarp', however I receive the following error with 'affine3d'.

>> A

A =

    1.0000   -0.0020    0.0026   -1.1383
    0.0024    0.9848   -0.1737    8.7917
   -0.0022    0.1737    0.9848  -12.2488
         0         0         0    1.0000

>> tform = affine3d(A)
Error using affine3d/set.T (line 340)
The final column of an affine transformation matrix must consist of zeroes, except for a one in the last row.

Error in affine3d (line 117)
                self.T = A;

I feel like the fix should be easy, but I am hitting a wall :/

Much appreciated - Jamie

PS How do I decode the transformation matrix 'A'? I imagine the diagonals are xx, yy, and zz - but I am uncertain how to interpret the fourth row/column.


j.cos...@jatneurolab.com

unread,
May 30, 2018, 3:27:20 PM5/30/18
to itksnap-users
BTW - The Convert3D file originated in ITK-SNAP's registration tool.

mfk16

unread,
Jun 6, 2018, 9:19:40 PM6/6/18
to itksnap-users

It appears that you're trying to apply the generated transformation to the image in MATLAB (?), but there may be an easier option:

1. Using c3d to apply the transformation before importing the nii to matlab: https://sourceforge.net/p/c3d/git/ci/master/tree/doc/c3d.md#-reslice-matrix-resample-image-using-affine-transform
2. If the nii needs some processing imaging prior to the transformation, do that in MATLAB, then call the same code in the link, but use the dos/system command to call c3d using the command line. Be sure the c3d executable is in your path.


Also, not sure what you mean by the 'diagonals', but the 4x4 transformation matrix is made up of a 3x3 Rotation matrix and a 3x1 translation vector

HTH.

Paul Yushkevich

unread,
Jun 7, 2018, 6:33:26 AM6/7/18
to itksna...@googlegroups.com
I can't give you the code for MATLAB but I can explain what the matrix stored in the .mat file is:

If x is the NIFTI RAS coordinate in the space of the fixed image, then the NIFTI RAS coordinate of the corresponding location in the moving image, after affine registration, is 

Ax+b

Where the matrix stored in the .mat file is in the form
[A11 A12 A13 b1]
[A21 A22 A23 b2]
[A31 A32 A33 b3]
[0 0 0 1]

The NIFTI RAS coordinate of the image index k=(i,j,k) can be obtained as

x=Sk

Where S is the 'sform' matrix stored in the NIFTI image header. 

Best,
Paul


--
You received this message because you are subscribed to the Google Groups "itksnap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to itksnap-user...@googlegroups.com.
To post to this group, send email to itksna...@googlegroups.com.
Visit this group at https://groups.google.com/group/itksnap-users.
For more options, visit https://groups.google.com/d/optout.


--
Paul A. Yushkevich, Ph.D.
Associate Professor
Penn Image Computing and Science Laboratory
Department of Radiology
University of Pennsylvania

Dorian P.

unread,
Jun 7, 2018, 6:27:09 PM6/7/18
to itksnap-users
I am also interested in something like this, but I have the opposite problem. The transformation is generated in MIDAS software, but I want to apply it to images using ANTs/R. I am comfortable with computing physical coordinates from voxel indices in ITK-based software. But can't seem to find how to apply the MIDAS transformation in ANTsR or c3d. The transformation is stored as a text file with nine values, it's just a rigid transformation according to the authors. Here is an example of two images and their registration/transformation parameters. Any help is appreciated.

```
# native 
> ss.mri
antsImage
  Pixel Type          : float 
  Components Per Pixel: 1 
  Dimensions          : 256x256x208 
  Voxel Spacing       : 1.10156202316284x1.10156202316284x1.10000598430634 
  Origin              : 114.4006 -140.9999 140.9999 
  Direction           : 0 1 0 0 0 -1 -1 0 0 

# template
> ss.mni
antsImage
  Pixel Type          : float 
  Components Per Pixel: 1 
  Dimensions          : 220x156x172 
  Voxel Spacing       : 1x1x1 
  Origin              : 86 -110 78 
  Direction           : 0 1 0 0 0 -1 -1 0 0 

# transformation values
-9.178665e-02
-9.624395e-02
-3.031423e-01
-1.126689e+01
2.856828e+00
-7.156745e+00
9.595318e-01
9.468308e-01
1.017382e+00 
```

To unsubscribe from this group and stop receiving emails from it, send an email to itksnap-users+unsubscribe@googlegroups.com.

To post to this group, send email to itksna...@googlegroups.com.
Visit this group at https://groups.google.com/group/itksnap-users.
For more options, visit https://groups.google.com/d/optout.
--
Paul A. Yushkevich, Ph.D.
Associate Professor
Penn Image Computing and Science Laboratory
Department of Radiology
University of Pennsylvania

--
You received this message because you are subscribed to the Google Groups "itksnap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to itksnap-users+unsubscribe@googlegroups.com.
Message has been deleted

Tom Nelson

unread,
Apr 23, 2020, 7:23:16 AM4/23/20
to itksnap-users
Just found this via Google so I'm aware it probably won't help the OP but in case anyone else finds this, I think the answer lies in the fact that Matlab (at least the 'Affine3d' function) requires the transformation matrix to be in an unconventional orientation. Try transposing it (so the zeros are on the right column, rather than the bottom row).

A =
   
1.0000   -0.0020    0.0026   -1.1383
   
0.0024    0.9848   -0.1737    8.7917
   
-0.0022    0.1737    0.9848  -12.2488
         
0         0         0    1.0000


B = A
'

    1.0000    0.0024   -0.0022         0
   -0.0020    0.9848    0.1737         0
    0.0026   -0.1737    0.9848         0
   -1.1383    8.7917  -12.2488    1.0000

= >> tform = affine3d(B)

Reply all
Reply to author
Forward
0 new messages