Load NIFTI dataset. Support both *.nii and *.hdr/*.img file extension. If file extension is not provided, *.hdr/*.img will be used as default. --------------------------------------- Notice from VBMEG developers (2013/11/18) --------------------------------------- Process implemented in xform_nii.m has been commented. Thus an image contained in nii.img is not RAS format as mentioned below but it is an original image without any formation in header. --------------------------------------- A subset of NIFTI sform/qform transform is performed in load_nii to translate, flip, rotate (limited to N*90 degree), intensity scaling etc. Other transforms (any degree rotation, shears, etc.) are not supported, because in those transforms, each voxel has to be repositioned, interpolated, and whole image(s) will have to be reconstructed. If an input data (nii) can not be handled, The program will exit with an error message "Transform of this NIFTI data is not supported by the program". After the transform, nii will be in RAS orientation, i.e. X axis from Left to Right, Y axis from Posterior to Anterior, and Z axis from Inferior to Superior. The RAS orientation system sometimes is also referred as right-hand coordinate system, or Neurologist preferred system. Usage: [nii] = load_nii(filename, [img_idx], [old_RGB]) filename - NIFTI file name. img_idx - a numerical array of image indices. Only the specified images will be loaded. If there is no img_idx, all available images will be loaded. old_RGB - a boolean variable to tell difference from new RGB24 from old RGB24. New RGB24 uses RGB triple sequentially for each voxel, like [R1 G1 B1 R2 G2 B2 ...]. Analyze 6.0 developed by AnalyzeDirect uses old RGB24, in a way like [R1 R2 ... G1 G2 ... B1 B2 ...] for each slices. If the image that you view is garbled, try to set old_RGB variable to 1 and try again, because it could be in old RGB24. The number of images scans can be obtained from get_nii_frame, or simply: hdr.dime.dim(5) Returned values: nii.hdr - struct with NIFTI header fields. nii.filetype - Analyze format (0); NIFTI .hdr/.img (1); NIFTI .nii (2) nii.fileprefix - NIFTI filename without extension. nii.machine - machine string variable. nii.img_idx - Indices of images to be loaded. nii.img - 3D (or 4D) matrix of NIFTI data. Part of this file is copied and modified under GNU license from MRI_TOOLBOX developed by CNSP in Flinders University, Australia NIFTI data format can be found on: http://nifti.nimh.nih.gov - Jimmy Shen (pls@rotman-baycrest.on.ca)
0001 % Load NIFTI dataset. Support both *.nii and *.hdr/*.img file extension. 0002 % If file extension is not provided, *.hdr/*.img will be used as default. 0003 % --------------------------------------- 0004 % Notice from VBMEG developers (2013/11/18) 0005 % --------------------------------------- 0006 % Process implemented in xform_nii.m has been commented. 0007 % Thus an image contained in nii.img is not RAS format as mentioned below but 0008 % it is an original image without any formation in header. 0009 % --------------------------------------- 0010 % 0011 % A subset of NIFTI sform/qform transform is performed in load_nii 0012 % to translate, flip, rotate (limited to N*90 degree), intensity 0013 % scaling etc. Other transforms (any degree rotation, shears, etc.) 0014 % are not supported, because in those transforms, each voxel has 0015 % to be repositioned, interpolated, and whole image(s) will have 0016 % to be reconstructed. If an input data (nii) can not be handled, 0017 % The program will exit with an error message "Transform of this 0018 % NIFTI data is not supported by the program". After the transform, 0019 % nii will be in RAS orientation, i.e. X axis from Left to Right, 0020 % Y axis from Posterior to Anterior, and Z axis from Inferior to 0021 % Superior. The RAS orientation system sometimes is also referred 0022 % as right-hand coordinate system, or Neurologist preferred system. 0023 % 0024 % Usage: [nii] = load_nii(filename, [img_idx], [old_RGB]) 0025 % 0026 % filename - NIFTI file name. 0027 % 0028 % img_idx - a numerical array of image indices. Only the specified 0029 % images will be loaded. If there is no img_idx, all available 0030 % images will be loaded. 0031 % 0032 % old_RGB - a boolean variable to tell difference from new RGB24 from old 0033 % RGB24. New RGB24 uses RGB triple sequentially for each voxel, like 0034 % [R1 G1 B1 R2 G2 B2 ...]. Analyze 6.0 developed by AnalyzeDirect uses 0035 % old RGB24, in a way like [R1 R2 ... G1 G2 ... B1 B2 ...] for each 0036 % slices. If the image that you view is garbled, try to set old_RGB 0037 % variable to 1 and try again, because it could be in old RGB24. 0038 % 0039 % The number of images scans can be obtained from get_nii_frame, or 0040 % simply: hdr.dime.dim(5) 0041 % 0042 % Returned values: 0043 % 0044 % nii.hdr - struct with NIFTI header fields. 0045 % nii.filetype - Analyze format (0); NIFTI .hdr/.img (1); NIFTI .nii (2) 0046 % nii.fileprefix - NIFTI filename without extension. 0047 % nii.machine - machine string variable. 0048 % nii.img_idx - Indices of images to be loaded. 0049 % nii.img - 3D (or 4D) matrix of NIFTI data. 0050 % 0051 % Part of this file is copied and modified under GNU license from 0052 % MRI_TOOLBOX developed by CNSP in Flinders University, Australia 0053 % 0054 % NIFTI data format can be found on: http://nifti.nimh.nih.gov 0055 % 0056 % - Jimmy Shen (pls@rotman-baycrest.on.ca) 0057 % 0058 function [nii] = load_nii(filename, img_idx, old_RGB) 0059 0060 if ~exist('filename','var'), 0061 error('Usage: [nii] = load_nii(filename, [img_idx])'); 0062 end 0063 0064 if ~exist('img_idx','var'), img_idx = []; end 0065 if ~exist('old_RGB','var'), old_RGB = 0; end 0066 0067 % Read the dataset header 0068 % 0069 [nii.hdr, nii.filetype, nii.fileprefix, nii.machine] = ... 0070 load_nii_hdr_cbi(filename); 0071 0072 % Read the dataset body 0073 % 0074 [nii.img, nii.hdr] = load_nii_img_cbi(nii.hdr, nii.filetype, nii.fileprefix, ... 0075 nii.machine, img_idx, old_RGB); 0076 0077 % Modified by M. Sato 0078 nii.img = double(nii.img); 0079 0080 % Perform some of sform/qform transform 0081 % 0082 % nii = xform_nii(nii); 0083 0084 return % load_nii 0085