


Read file(NIFTI, ...) for REST by CHEN Gui-Wen and YAN Chao-Gan
%------------------------------------------------------------------------
Read MRI image file (imageIN) with format of Nifti 1.1. It will return data
of 3D matrix (Data) and infomation of the header (Head). Please set the volume
index (1 for the first volume) you want to load into memory if the image file
contains 4D data. The structure of header (Head) is the same with SPM5.
Usage: [Data, Head] = rest_ReadNiftiImage(imageIN, volumeIndex)
Input:
1. imageIN - the path and filename of image file, [path\*.img]
2. volumeIndex- the volume of 4D data to read, can be 1,2,..., not larger than
the number of total volumes, default: 1
Output:
1. Data - 3D matrix of data
2. Head - a structure containing image volume information
The elements in the structure are:
Head.fname - the filename of the image.
Head.dim - the x, y and z dimensions of the volume
Head.dt - A 1x2 array. First element is datatype (see spm_type).
The second is 1 or 0 depending on the endian-ness.
Head.mat - a 4x4 affine transformation matrix mapping from
voxel coordinates to real world coordinates.
Head.pinfo - plane info for each plane of the volume.
Head.pinfo(1,:) - scale for each plane
Head.pinfo(2,:) - offset for each plane
The true voxel intensities of the jth image are given
by: val*Head.pinfo(1,j) + Head.pinfo(2,j)
Head.pinfo(3,:) - offset into image (in bytes).
If the size of pinfo is 3x1, then the volume is assumed
to be contiguous and each plane has the same scalefactor
and offset.
Head.private - a structure containing complete information in the header
%------------------------------------------------------------------------
Copyright (C) 2007 Neuroimage Computing Group, State Key Laboratory of
Cognitive Neuroscience and Learning
Guiwen Chen, gwenchill@gmail.com
@(#)rest_ReadNiftiImage.m ver 2.0, 07/11/24
%------------------------------------------------------------------------
Revised by YAN Chao-Gan 080621
ycg.yan@gmail.com


0001 function [Data, Head] = rest_ReadNiftiImage(imageIN, volumeIndex) 0002 % Read file(NIFTI, ...) for REST by CHEN Gui-Wen and YAN Chao-Gan 0003 % %------------------------------------------------------------------------ 0004 % Read MRI image file (imageIN) with format of Nifti 1.1. It will return data 0005 % of 3D matrix (Data) and infomation of the header (Head). Please set the volume 0006 % index (1 for the first volume) you want to load into memory if the image file 0007 % contains 4D data. The structure of header (Head) is the same with SPM5. 0008 % 0009 % Usage: [Data, Head] = rest_ReadNiftiImage(imageIN, volumeIndex) 0010 % 0011 % Input: 0012 % 1. imageIN - the path and filename of image file, [path\*.img] 0013 % 2. volumeIndex- the volume of 4D data to read, can be 1,2,..., not larger than 0014 % the number of total volumes, default: 1 0015 % Output: 0016 % 1. Data - 3D matrix of data 0017 % 2. Head - a structure containing image volume information 0018 % The elements in the structure are: 0019 % Head.fname - the filename of the image. 0020 % Head.dim - the x, y and z dimensions of the volume 0021 % Head.dt - A 1x2 array. First element is datatype (see spm_type). 0022 % The second is 1 or 0 depending on the endian-ness. 0023 % Head.mat - a 4x4 affine transformation matrix mapping from 0024 % voxel coordinates to real world coordinates. 0025 % Head.pinfo - plane info for each plane of the volume. 0026 % Head.pinfo(1,:) - scale for each plane 0027 % Head.pinfo(2,:) - offset for each plane 0028 % The true voxel intensities of the jth image are given 0029 % by: val*Head.pinfo(1,j) + Head.pinfo(2,j) 0030 % Head.pinfo(3,:) - offset into image (in bytes). 0031 % If the size of pinfo is 3x1, then the volume is assumed 0032 % to be contiguous and each plane has the same scalefactor 0033 % and offset. 0034 % Head.private - a structure containing complete information in the header 0035 % %------------------------------------------------------------------------ 0036 % Copyright (C) 2007 Neuroimage Computing Group, State Key Laboratory of 0037 % Cognitive Neuroscience and Learning 0038 % Guiwen Chen, gwenchill@gmail.com 0039 % @(#)rest_ReadNiftiImage.m ver 2.0, 07/11/24 0040 % %------------------------------------------------------------------------ 0041 % Revised by YAN Chao-Gan 080621 0042 % ycg.yan@gmail.com 0043 0044 if ~exist('volumeIndex', 'var') 0045 volumeIndex=1; 0046 end 0047 0048 % get the SPM path 0049 FilePath = which('rest.m'); 0050 [giftPath, fileN, extn] = fileparts(FilePath); 0051 spmPath = fullfile(giftPath, 'rest_spm5_files'); 0052 oldDir = pwd; 0053 0054 addpath(spmPath); 0055 0056 try 0057 % % cd(spmPath); %Revised by YAN Chao-Gan 0058 filename=imageIN; 0059 %Added by YAN Chao-Gan 0060 if length(filename)>4 0061 if strcmpi(filename(end-3:end), '.hdr') 0062 filename = filename(1:end-4); 0063 filename=[filename,'.img']; 0064 end 0065 end 0066 0067 % construct the file name 0068 [pathstr, fName, extn] = fileparts(deblank(filename)); 0069 0070 if isempty(pathstr) 0071 filename = strcat(oldDir, filesep, filename); 0072 end 0073 0074 % Get info of header 0075 V = nic_spm_vol(filename); 0076 0077 % Initialise data 0078 Data = zeros([V(1).dim(1:3), 1]); 0079 0080 % vol can't be larger than the length of structure 0081 if(volumeIndex>length(V)) 0082 error('The volume is error, please set the right volume to read'); 0083 end 0084 0085 % read volumes 0086 Data(:, :, :,1) = nic_spm_read_vols(V(volumeIndex)); 0087 0088 % replace NaN with zero 0089 Data(isnan(Data)) = 0; 0090 0091 % save Header info in structure 0092 Head=V(volumeIndex); 0093 0094 % nic_ShowOrientation(Head);%show the oritention of data, you can call the function in other function 0095 0096 clear V; 0097 0098 rmpath(spmPath); 0099 % cd(oldDir); 0100 catch 0101 rmpath(spmPath); 0102 % cd(oldDir); 0103 error('Meet error while reading the data'); 0104 end 0105