change_orientation [avw] = change_orient_ras(avw,orient) --- Input avw : NIFTI/Analyze data structure orient : axis dim to get RAS coordinate - necessary for Analyze - unnecessary for NIFTI --- orient orient : axis dim to get RAS coordinate = [orient_x orient_y orient_z] orient_x : Left to Right axis dim of current image orient_y : Posterior to Anterior axis dim of current image orient_z : Inferior to Superior axis dim of current image current image axis dim is [+-1/+-2/+-3] for [+-x/+-y/+-z] --- sform transform i = 0 .. dim[1]-1 j = 0 .. dim[2]-1 k = 0 .. dim[3]-1 x = srow_x[0] * i + srow_x[1] * j + srow_x[2] * k + srow_x[3] y = srow_y[0] * i + srow_y[1] * j + srow_y[2] * k + srow_y[3] z = srow_z[0] * i + srow_z[1] * j + srow_z[2] * k + srow_z[3] --- matrix form [x ; y ; z] = R * [i ; j ; k] + T Part of this file is copied and modified under GNU license from NIFTI TOOLBOX developed by Jimmy Shen Made by Masa-aki Sato 2008-02-17
0001 function [avw] = change_orient_ras(avw,orient) 0002 % change_orientation 0003 % [avw] = change_orient_ras(avw,orient) 0004 % --- Input 0005 % avw : NIFTI/Analyze data structure 0006 % orient : axis dim to get RAS coordinate 0007 % - necessary for Analyze 0008 % - unnecessary for NIFTI 0009 % --- orient 0010 % orient : axis dim to get RAS coordinate 0011 % = [orient_x orient_y orient_z] 0012 % orient_x : Left to Right axis dim of current image 0013 % orient_y : Posterior to Anterior axis dim of current image 0014 % orient_z : Inferior to Superior axis dim of current image 0015 % current image axis dim is [+-1/+-2/+-3] for [+-x/+-y/+-z] 0016 % --- sform transform 0017 % i = 0 .. dim[1]-1 0018 % j = 0 .. dim[2]-1 0019 % k = 0 .. dim[3]-1 0020 % x = srow_x[0] * i + srow_x[1] * j + srow_x[2] * k + srow_x[3] 0021 % y = srow_y[0] * i + srow_y[1] * j + srow_y[2] * k + srow_y[3] 0022 % z = srow_z[0] * i + srow_z[1] * j + srow_z[2] * k + srow_z[3] 0023 % --- matrix form 0024 % [x ; y ; z] = R * [i ; j ; k] + T 0025 % 0026 % Part of this file is copied and modified under GNU license from 0027 % NIFTI TOOLBOX developed by Jimmy Shen 0028 % 0029 % Made by Masa-aki Sato 2008-02-17 0030 0031 0032 hdr = avw.hdr; 0033 0034 if isfield(avw,'filetype') & avw.filetype > 0 0035 % NIFTI data format 0036 ftype = 1; 0037 else 0038 % Analyze data format 0039 ftype = 0; 0040 end 0041 0042 if exist('orient','var') 0043 R = get_rot_from_orient(orient); 0044 0045 if ftype == 0 0046 % Analyze data format 0047 T = hdr.hist.originator(1:3); 0048 T = abs( R * T(:) ); 0049 else 0050 error('No orient is necessary for NIFTI file ') 0051 end 0052 elseif ftype == 1 0053 % NIFTI data format 0054 % orient : axis dim to get RAS coordinate 0055 [orient, R0, T] = get_orient_info(avw); 0056 % rotation matrix from orient 0057 R = get_rot_from_orient(orient); 0058 else 0059 error('Orient is necessary for Analyze file ') 0060 end 0061 0062 % --- change axis of image to RAS according to orient 0063 img = permute(avw.img, abs(orient)); 0064 0065 % orient = [3 1 2] 0066 % img(j3,j1,j2) = img(j1,j2,j3) 0067 0068 dim = size(img); 0069 0070 % --- flip image according to orient 0071 if orient(1) < 0 0072 img(1:dim(1),:,:) = img(dim(1):-1:1,:,:); 0073 end 0074 if orient(2) < 0 0075 img(:,1:dim(2),:) = img(:,dim(2):-1:1,:); 0076 end 0077 if orient(3) < 0 0078 img(:,:,1:dim(3)) = img(:,:,dim(3):-1:1); 0079 end 0080 0081 % image with RAS orientation 0082 avw.img = img; 0083 0084 % --- transform voxcel size 0085 vsize = hdr.dime.pixdim(2:4); % pixdim of original image 0086 vsize = abs(R * vsize(:)); % pixdim of transformed RAS image 0087 0088 avw.hdr.dime.dim(2:4) = dim; 0089 avw.hdr.dime.pixdim(2:4) = vsize'; 0090 0091 avw.hdr.hist.sform_code = 1; 0092 0093 % --- No translation of origine 0094 % --- sform transform 0095 % i = 0 .. dim[1]-1 0096 % j = 0 .. dim[2]-1 0097 % k = 0 .. dim[3]-1 0098 % x = srow_x[0] * i + srow_x[1] * j + srow_x[2] * k + srow_x[3] 0099 % y = srow_y[0] * i + srow_y[1] * j + srow_y[2] * k + srow_y[3] 0100 % z = srow_z[0] * i + srow_z[1] * j + srow_z[2] * k + srow_z[3] 0101 0102 avw.hdr.hist.srow_x = [1 0 0 0]*vsize(1); 0103 avw.hdr.hist.srow_y = [0 1 0 0]*vsize(2); 0104 avw.hdr.hist.srow_z = [0 0 1 0]*vsize(3); 0105 0106 % origine = image center 0107 % 0 = srow_x[0] * dim(1)/2 + srow_x[3] 0108 avw.hdr.hist.srow_x(4) = - avw.hdr.hist.srow_x(1) * dim(1)/2; 0109 avw.hdr.hist.srow_y(4) = - avw.hdr.hist.srow_y(2) * dim(2)/2; 0110 avw.hdr.hist.srow_z(4) = - avw.hdr.hist.srow_z(3) * dim(3)/2; 0111 0112 avw.hdr.hist.originator(1:3) = 0; 0113 0114 0115 avw.hdr.hist.qform_code = 0; 0116 0117 avw.hdr.hist.quatern_b = 0; 0118 avw.hdr.hist.quatern_c = 0; 0119 avw.hdr.hist.quatern_d = 0; 0120 avw.hdr.hist.qoffset_x = 0; 0121 avw.hdr.hist.qoffset_y = 0; 0122 avw.hdr.hist.qoffset_z = 0; 0123 0124 return 0125 0126 %avw. 0127 %avw. 0128 %avw. 0129 0130 % DIM = avw.hdr.dime.dim(2:4) % 画像サイズ 0131 % VOX = avw.hdr.dime.pixdim(2:4) % voxelサイズ 0132 % 0133 % origin in voxcel-space -> origin in MNI-mm space 0134 %origin = vox(:) .* origin(:); 0135 % 0136 %Trans = [vox(1) 0 0 -origin(1) ; ... 0137 % 0 vox(2) 0 -origin(2) ; ... 0138 % 0 0 vox(3) -origin(3) ; ... 0139 % 0 0 0 1 ]; 0140 0141 return