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, R0, T] = get_orient_info(avw); 0055 R = get_rot_from_orient(orient); 0056 else 0057 error('Orient is necessary for Analyze file ') 0058 end 0059 0060 %if ftype == 0, 0061 % % Flip x-asis for Analyze file (x+ : left) 0062 % orient(1) = - orient(1); 0063 %end 0064 0065 % --- change axis of image 0066 img = permute(avw.img, abs(orient)); 0067 0068 % orient = [3 1 2] 0069 % img(j3,j1,j2) = img(j1,j2,j3) 0070 0071 dim = size(img); 0072 0073 % --- flip image 0074 if orient(1) < 0 0075 img(1:dim(1),:,:) = img(dim(1):-1:1,:,:); 0076 end 0077 if orient(2) < 0 0078 img(:,1:dim(2),:) = img(:,dim(2):-1:1,:); 0079 end 0080 if orient(3) < 0 0081 img(:,:,1:dim(3)) = img(:,:,dim(3):-1:1); 0082 end 0083 0084 avw.img = img; 0085 0086 % --- change voxcel size & origin of image 0087 0088 % [x ; y ; z] = R * [i ; j ; k] + T 0089 0090 % No flip case 0091 % x = i + T1 (i=0:N1-1) 0092 % = ii + X0 (ii=1:N1, X0=T1-1) 0093 % Flip case 0094 % x = -i + T1 (i=0:N1-1) 0095 % ii= N1 - i (ii=1:N1) 0096 % x = ii + X0 (ii=1:N1, X0=T1-N1) 0097 0098 % originator 0099 % i0 = T1 (centor) 0100 % Flip case 0101 % ii = N1 - i 0102 % ii0 = N1 - i0 = N1 - T1 0103 0104 if ftype == 1 0105 % NIFTI data format 0106 vsize = hdr.dime.pixdim(2:4); 0107 vsize = abs(R * vsize(:)); 0108 vox = sum(R,2); 0109 vox = 0.5 * (1 - sign(vox)); 0110 0111 R = R0*R'; 0112 T = T - R * (vox .* (dim(:) - 1)); 0113 0114 avw.hdr.hist.srow_x = [R(1,:) T(1)]; 0115 avw.hdr.hist.srow_y = [R(2,:) T(2)]; 0116 avw.hdr.hist.srow_z = [R(3,:) T(3)]; 0117 0118 avw.hdr.hist.originator(1:3) = - (T./vsize)'; 0119 0120 else 0121 % Analyze data format 0122 % hdr.hist.srow_x(1) = hdr.dime.pixdim(2); 0123 % hdr.hist.srow_y(2) = hdr.dime.pixdim(3); 0124 % hdr.hist.srow_z(3) = hdr.dime.pixdim(4); 0125 % hdr.hist.srow_x(4) = (1-hdr.hist.originator(1))*hdr.dime.pixdim(2); 0126 % hdr.hist.srow_y(4) = (1-hdr.hist.originator(2))*hdr.dime.pixdim(3); 0127 % hdr.hist.srow_z(4) = (1-hdr.hist.originator(3))*hdr.dime.pixdim(4); 0128 % 0129 vsize = hdr.dime.pixdim(2:4); 0130 vsize = abs(R * vsize(:)); 0131 vox = sum(R,2); 0132 0133 for j=1:3 0134 if vox(j) > 0 0135 T(j) = T(j); 0136 else 0137 T(j) = dim(j) - T(j); 0138 end 0139 end 0140 0141 avw.hdr.hist.srow_x = [1 0 0 -T(1)]*vsize(1); 0142 avw.hdr.hist.srow_y = [0 1 0 -T(2)]*vsize(2); 0143 avw.hdr.hist.srow_z = [0 0 1 -T(3)]*vsize(3); 0144 0145 avw.hdr.hist.originator(1:3) = T'; 0146 end 0147 0148 avw.hdr.dime.dim(2:4) = dim; 0149 avw.hdr.dime.pixdim(2:4) = vsize'; 0150 0151 avw.hdr.hist.qform_code = 0; 0152 avw.hdr.hist.sform_code = 1; 0153 0154 return 0155 0156 %avw. 0157 %avw. 0158 %avw. 0159 0160 % DIM = avw.hdr.dime.dim(2:4) % 画像サイズ 0161 % VOX = avw.hdr.dime.pixdim(2:4) % voxelサイズ 0162 % 0163 % origin in voxcel-space -> origin in MNI-mm space 0164 %origin = vox(:) .* origin(:); 0165 % 0166 %Trans = [vox(1) 0 0 -origin(1) ; ... 0167 % 0 vox(2) 0 -origin(2) ; ... 0168 % 0 0 vox(3) -origin(3) ; ... 0169 % 0 0 0 1 ]; 0170 0171 return