Get orientation, rotation, translation info from NIFTI file [orient, R, T] = get_orient_info(nii) --- transform from voxcel to real coordinate R : 3 x 3 rotation matrix T : 3 x 1 translation matrix [x ; y ; z] = R * [i ; j ; k] + T i = 0 .. dim[1]-1 j = 0 .. dim[2]-1 k = 0 .. dim[3]-1 orient = [aixs_dir(1) ,aixs_dir(2) ,aixs_dir(3) ] aixs_dir is one of following number Left to Right 1 Posterior to Anterior 2 Inferior to Superior 3 Right to Left -1 Anterior to Posterior -2 Superior to Inferior -3 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 [orient, R, T] = get_orient_info(nii) 0002 % Get orientation, rotation, translation info from NIFTI file 0003 % [orient, R, T] = get_orient_info(nii) 0004 % 0005 % --- transform from voxcel to real coordinate 0006 % R : 3 x 3 rotation matrix 0007 % T : 3 x 1 translation matrix 0008 % [x ; y ; z] = R * [i ; j ; k] + T 0009 % 0010 % i = 0 .. dim[1]-1 0011 % j = 0 .. dim[2]-1 0012 % k = 0 .. dim[3]-1 0013 % 0014 % orient = [aixs_dir(1) ,aixs_dir(2) ,aixs_dir(3) ] 0015 % aixs_dir is one of following number 0016 % Left to Right 1 0017 % Posterior to Anterior 2 0018 % Inferior to Superior 3 0019 % Right to Left -1 0020 % Anterior to Posterior -2 0021 % Superior to Inferior -3 0022 % 0023 % Part of this file is copied and modified under GNU license from 0024 % NIFTI TOOLBOX developed by Jimmy Shen 0025 % 0026 % Made by Masa-aki Sato 2008-02-17 0027 0028 hdr = nii.hdr; 0029 0030 if ~isfield(nii,'filetype') | nii.filetype == 0 0031 % Analyze format 0032 % no sform/qform 0033 % assume LAS format : [-1 2 3] 0034 vsize = hdr.dime.pixdim(2:4); 0035 origin = hdr.hist.originator(1:3); 0036 0037 T = - origin.*vsize; 0038 T = T(:); 0039 R = [-vsize(1) 0 0; 0 vsize(2) 0; 0 0 vsize(3)]; 0040 orient = [-1 2 3]; 0041 return; 0042 end 0043 0044 % check sform_code or qform_code 0045 % 0046 if hdr.hist.sform_code > 0 0047 tmode = 0; 0048 elseif hdr.hist.qform_code > 0 0049 tmode = 1; 0050 else 0051 tmode = 0; 0052 end 0053 0054 if tmode == 0, 0055 % --- sform transform 0056 % i = 0 .. dim[1]-1 0057 % j = 0 .. dim[2]-1 0058 % k = 0 .. dim[3]-1 0059 % x = srow_x[0] * i + srow_x[1] * j + srow_x[2] * k + srow_x[3] 0060 % y = srow_y[0] * i + srow_y[1] * j + srow_y[2] * k + srow_y[3] 0061 % z = srow_z[0] * i + srow_z[1] * j + srow_z[2] * k + srow_z[3] 0062 % --- matrix form 0063 % [x ; y ; z] = R * [i ; j ; k] + T 0064 0065 R = [hdr.hist.srow_x(1:3) 0066 hdr.hist.srow_y(1:3) 0067 hdr.hist.srow_z(1:3)]; 0068 0069 T = [hdr.hist.srow_x(4) 0070 hdr.hist.srow_y(4) 0071 hdr.hist.srow_z(4)]; 0072 0073 else 0074 % --- qform transform 0075 b = hdr.hist.quatern_b; 0076 c = hdr.hist.quatern_c; 0077 d = hdr.hist.quatern_d; 0078 a = sqrt(1.0-(b*b+c*c+d*d)); 0079 0080 qfac = hdr.dime.pixdim(1); 0081 i = hdr.dime.pixdim(2); 0082 j = hdr.dime.pixdim(3); 0083 k = qfac * hdr.dime.pixdim(4); 0084 0085 R = [a*a+b*b-c*c-d*d 2*b*c-2*a*d 2*b*d+2*a*c 0086 2*b*c+2*a*d a*a+c*c-b*b-d*d 2*c*d-2*a*b 0087 2*b*d-2*a*c 2*c*d+2*a*b a*a+d*d-c*c-b*b]; 0088 0089 R = R * diag([i j k]); 0090 0091 T = [hdr.hist.qoffset_x 0092 hdr.hist.qoffset_y 0093 hdr.hist.qoffset_z]; 0094 end 0095 0096 % --- orient 0097 orient = get_orient_from_rot(R); 0098 0099 return; % orient_hdr