freesurfer_voxels2surf - convert voxel into surface coordinates [SurfVertices,T] = freesurfer_surf2voxels(VoxVertices) The input 'VoxVertices' are the MRI voxel indices for a Freesurfer MRI volume; they are Nx3 coordinates from the sagittal, coronal and axial slice planes, in that order. This function converts the MRI voxel indices into the FreeSurfer surface RAS 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 voxel or RAS coordinate can be transformed into the corresponding RAS or voxel index, respectively, 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: SurfVertices = VoxVertices * T.fsVox2RAS; note that VoxVertices 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 SurfVertices(1:N,1:3) gives back just the transformed vertex coordinates (this function does this for you!).
0001 function [SurfVertices,T] = freesurfer_surf2voxels(VoxVertices) 0002 0003 % freesurfer_voxels2surf - convert voxel into surface coordinates 0004 % 0005 % [SurfVertices,T] = freesurfer_surf2voxels(VoxVertices) 0006 % 0007 % The input 'VoxVertices' are the MRI voxel indices for a Freesurfer MRI 0008 % volume; they are Nx3 coordinates from the sagittal, coronal and axial 0009 % slice planes, in that order. This function converts the MRI voxel indices 0010 % into the FreeSurfer surface RAS 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 voxel or RAS coordinate 0022 % can be transformed into the corresponding RAS or voxel index, 0023 % respectively, with the following 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 % SurfVertices = VoxVertices * T.fsVox2RAS; 0032 % 0033 % note that VoxVertices 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 SurfVertices(1:N,1:3) gives back just the transformed vertex 0037 % coordinates (this function does this for you!). 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_VOXELS2SURF [v %s]\n',ver(11:15)); tic; 0048 0049 %-------------------------------------------------------------------- 0050 % Pad out the VoxVertices to Nx4 matrix 0051 0052 Nvertices = size(VoxVertices,1); 0053 0054 right_column = [ ones( Nvertices, 1 ); 0 ]'; 0055 0056 VoxVertices = [ [VoxVertices; 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 SurfVertices = VoxVertices * T.fsVox2RAS; 0079 SurfVertices = SurfVertices(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 0093 0094 %-------------------------------------------------------------------------- 0095 % TESTING CODE 0096 0097 % %------------------------------------------------------------------------ 0098 % % Test the conversion from FreeSurfer RAS to Voxel indices 0099 0100 % % FreeSurfer volume_index is: Sag, Axi, Cor (256^3, 1mm^3 voxels) 0101 % fs.volume_index.nas = [128 130 221]; 0102 % fs.volume_index.lpa = [203 152 133]; 0103 % fs.volume_index.rpa = [ 49 152 133]; 0104 % 0105 % fs.volume_index.mat = [fs.volume_index.nas; fs.volume_index.lpa; fs.volume_index.rpa ]; 0106 % 0107 % fsVox = [ [fs.volume_index.mat; 0 0 0] [1 1 1 0]'] 0108 0109 % eg, 0110 % fsVox = 0111 % 0112 % 128 130 221 1 0113 % 203 152 133 1 0114 % 49 152 133 1 0115 % 0 0 0 0 0116 % 0117 % % FreeSurfer volume_xyz is +X right, +Y anterior, +Z superior; this 0118 % % is the RAS values from the tkmedit viewer of FreeSurfer, where the volume 0119 % % index values are ordered: Sag, Axi, Cor 0120 % fs.volume_xyz.nas = [ 0 93 -2]; 0121 % fs.volume_xyz.lpa = [-75 5 -24]; 0122 % fs.volume_xyz.rpa = [ 79 5 -24]; 0123 % 0124 % fs.volume_xyz.mat = [fs.volume_xyz.nas; fs.volume_xyz.lpa; fs.volume_xyz.rpa ]; 0125 % 0126 % fsRAS = [ [fs.volume_xyz.mat; 0 0 0] [1 1 1 0]'] 0127 % 0128 % T.fsVox2RAS = [ [-1 0 0 128]' [0 0 1 -128]' [ 0 -1 0 128]' [ 0 0 0 0]' ]; 0129 % T.fsRAS2Vox = [ [-1 0 0 128]' [0 0 -1 128]' [ 0 1 0 128]' [ 0 0 0 1]' ]; 0130 % 0131 % % T.fsVox2RAS = 0132 % % 0133 % % -1 0 0 0 0134 % % 0 0 -1 0 0135 % % 0 1 0 0 0136 % % 128 -128 128 0 0137 % 0138 % % T.fsRAS2Vox = 0139 % % 0140 % % -1 0 0 0 0141 % % 0 0 1 0 0142 % % 0 -1 0 0 0143 % % 128 128 128 1 0144 % 0145 % fsVox2ras = fsVox * T.fsVox2RAS; 0146 % fsVox2ras = fsVox2ras(1:3,1:3) 0147 % 0148 % fsRAS2vox = fsRAS * T.fsRAS2Vox; 0149 % fsRAS2vox = fsRAS2vox(1:3,1:3)