freesurfer_surf2voxels - convert a surface into MRI voxel coordinates [VoxVertices,T] = freesurfer_surf2voxels(SurfVertices) The input 'SurfVertices' are obtained from freesurfer_read_surf; they are Nx3 vertex coordinates in the freesurfer RAS coordinate framework. This function converts the RAS vertex coordinates into the FreeSurfer MRI voxel coordinates. FreeSurfer MRI volumes are 256^3 voxels, 1mm^3 each. The MRI volume index has an origin at the left, posterior, inferior voxel, such that: Sagital increases from left to right (+X Right) Coronal increases from posterior to anterior (+Y Anterior) Axial increases from inferior to superior (+Z Superior) The MRI RAS values have an origin at the middle of the volume, in approximately voxel 128, 128, 128. So, any given RAS coordinate can be transformed into the corresponding voxel index with the following transformation (T): T.fsRAS2Vox = [ [-1 0 0 128]' [0 0 -1 128]' [ 0 1 0 128]' [ 0 0 0 1]' ]; T.fsVox2RAS = [ [-1 0 0 128]' [0 0 1 -128]' [ 0 -1 0 128]' [ 0 0 0 0]' ]; These transform matrices are designed to be right multiplied with a matrix of vertex locations, such as: VoxVertices = SurfVertices * T.fsRAS2Vox; note that SurfVertices is padded out from an [N,3] matrix of vertices (in rows) and their XYZ values in columns, into an [N+1,4] matrix, where the bottom row is zeros and the right column is ones (except the last row). So VoxVertices(1:N,1:3) gives back just the transformed vertex coordinates.
0001 function [VoxVertices,T] = freesurfer_surf2voxels(SurfVertices) 0002 0003 % freesurfer_surf2voxels - convert a surface into MRI voxel coordinates 0004 % 0005 % [VoxVertices,T] = freesurfer_surf2voxels(SurfVertices) 0006 % 0007 % The input 'SurfVertices' are obtained from freesurfer_read_surf; they are Nx3 0008 % vertex coordinates in the freesurfer RAS coordinate framework. This 0009 % function converts the RAS vertex coordinates into the FreeSurfer MRI 0010 % voxel coordinates. 0011 % 0012 % FreeSurfer MRI volumes are 256^3 voxels, 1mm^3 each. 0013 % 0014 % The MRI volume index has an origin at the left, posterior, inferior 0015 % voxel, such that: 0016 % Sagital increases from left to right (+X Right) 0017 % Coronal increases from posterior to anterior (+Y Anterior) 0018 % Axial increases from inferior to superior (+Z Superior) 0019 % 0020 % The MRI RAS values have an origin at the middle of the volume, in 0021 % approximately voxel 128, 128, 128. So, any given RAS coordinate can be 0022 % transformed into the corresponding voxel index with the following 0023 % transformation (T): 0024 % 0025 % T.fsRAS2Vox = [ [-1 0 0 128]' [0 0 -1 128]' [ 0 1 0 128]' [ 0 0 0 1]' ]; 0026 % T.fsVox2RAS = [ [-1 0 0 128]' [0 0 1 -128]' [ 0 -1 0 128]' [ 0 0 0 0]' ]; 0027 % 0028 % These transform matrices are designed to be right multiplied with a 0029 % matrix of vertex locations, such as: 0030 % 0031 % VoxVertices = SurfVertices * T.fsRAS2Vox; 0032 % 0033 % note that SurfVertices is padded out from an [N,3] matrix of vertices (in 0034 % rows) and their XYZ values in columns, into an [N+1,4] matrix, where the 0035 % bottom row is zeros and the right column is ones (except the last row). 0036 % So VoxVertices(1:N,1:3) gives back just the transformed vertex 0037 % coordinates. 0038 % 0039 0040 % $Revision: 1.1 $ $Date: 2004/11/12 01:32:34 $ 0041 0042 % Licence: GNU GPL, no express or implied warranties 0043 % History: 02/2004, Darren.Weber_at_radiology.ucsf.edu 0044 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0045 0046 ver = '$Revision: 1.1 $'; 0047 fprintf('FREESURFER_SURF2VOXELS [v %s]\n',ver(11:15)); tic; 0048 0049 %-------------------------------------------------------------------- 0050 % Pad out the SurfVertices to Nx4 matrix 0051 0052 Nvertices = size(SurfVertices,1); 0053 0054 right_column = [ ones( Nvertices, 1 ); 0 ]; 0055 0056 SurfVertices = [ [SurfVertices; 0 0 0] right_column ]; 0057 0058 %-------------------------------------------------------------------- 0059 % Convert FreeSurfer RAS values into voxel indices 0060 0061 T.fsRAS2Vox = [ [-1 0 0 128]' [0 0 -1 128]' [ 0 1 0 128]' [ 0 0 0 1]' ]; 0062 T.fsVox2RAS = [ [-1 0 0 128]' [0 0 1 -128]' [ 0 -1 0 128]' [ 0 0 0 0]' ]; 0063 0064 % T.fsVox2RAS = 0065 % 0066 % -1 0 0 0 0067 % 0 0 -1 0 0068 % 0 1 0 0 0069 % 128 -128 128 0 0070 0071 % T.fsRAS2Vox = 0072 % 0073 % -1 0 0 0 0074 % 0 0 1 0 0075 % 0 -1 0 0 0076 % 128 128 128 1 0077 0078 VoxVertices = SurfVertices * T.fsRAS2Vox; 0079 VoxVertices = VoxVertices(1:Nvertices,1:3); 0080 0081 t = toc; fprintf('...done (%6.2f sec).\n\n',t); 0082 0083 return 0084 0085 0086 0087 0088 0089 0090 0091 %-------------------------------------------------------------------------- 0092 % TESTING CODE 0093 0094 % %------------------------------------------------------------------------ 0095 % % Test the conversion from FreeSurfer RAS to Voxel indices 0096 0097 % % FreeSurfer volume_index is: Sag, Axi, Cor (256^3, 1mm^3 voxels) 0098 % fs.volume_index.nas = [128 130 221]; 0099 % fs.volume_index.lpa = [203 152 133]; 0100 % fs.volume_index.rpa = [ 49 152 133]; 0101 % 0102 % fs.volume_index.mat = [fs.volume_index.nas; fs.volume_index.lpa; fs.volume_index.rpa ]; 0103 % 0104 % fsVox = [ [fs.volume_index.mat; 0 0 0] [1 1 1 0]'] 0105 0106 % eg, 0107 % fsVox = 0108 % 0109 % 128 130 221 1 0110 % 203 152 133 1 0111 % 49 152 133 1 0112 % 0 0 0 0 0113 % 0114 % % FreeSurfer volume_xyz is +X right, +Y anterior, +Z superior; this 0115 % % is the RAS values from the tkmedit viewer of FreeSurfer, where the volume 0116 % % index values are ordered: Sag, Axi, Cor 0117 % fs.volume_xyz.nas = [ 0 93 -2]; 0118 % fs.volume_xyz.lpa = [-75 5 -24]; 0119 % fs.volume_xyz.rpa = [ 79 5 -24]; 0120 % 0121 % fs.volume_xyz.mat = [fs.volume_xyz.nas; fs.volume_xyz.lpa; fs.volume_xyz.rpa ]; 0122 % 0123 % fsRAS = [ [fs.volume_xyz.mat; 0 0 0] [1 1 1 0]'] 0124 % 0125 % T.fsVox2RAS = [ [-1 0 0 128]' [0 0 1 -128]' [ 0 -1 0 128]' [ 0 0 0 0]' ]; 0126 % T.fsRAS2Vox = [ [-1 0 0 128]' [0 0 -1 128]' [ 0 1 0 128]' [ 0 0 0 1]' ]; 0127 % 0128 % % T.fsVox2RAS = 0129 % % 0130 % % -1 0 0 0 0131 % % 0 0 -1 0 0132 % % 0 1 0 0 0133 % % 128 -128 128 0 0134 % 0135 % % T.fsRAS2Vox = 0136 % % 0137 % % -1 0 0 0 0138 % % 0 0 1 0 0139 % % 0 -1 0 0 0140 % % 128 128 128 1 0141 % 0142 % fsVox2ras = fsVox * T.fsVox2RAS; 0143 % fsVox2ras = fsVox2ras(1:3,1:3) 0144 % 0145 % fsRAS2vox = fsRAS * T.fsRAS2Vox; 0146 % fsRAS2vox = fsRAS2vox(1:3,1:3)