Home > rest_20090422 > rest_readfile.m

rest_readfile

PURPOSE ^

Read file(ANALYZE 7.5, NIFTI, ...) for REST by YAN Chao-Gan

SYNOPSIS ^

function [Outdata,VoxDim,Header]=rest_readfile(imageIN,volumeIndex)

DESCRIPTION ^

 Read file(ANALYZE 7.5, NIFTI, ...) for REST by YAN Chao-Gan
 FORMAT function [Outdata,voxdim, Origin] = rest_readfile(filename)
                 filename - Analyze file (*.{hdr, img, nii})
                 Outdata  - data file.                            
                 VoxDim   - the size of the voxel.
                 Header   - It's decided by the format of data file:
                            for ANALYZE 7.5 - Header.Origin - the origin of the image;
                            for NIFTI  - 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
                                         Header.Origin - the origin of the image;
-----------------------------------------------------------
    Copyright(c) 2008~2010
    State Key Laboratory of Cognitive Neuroscience and Learning in Beijing Normal University
    Written by YAN Chao-Gan
    http://resting-fmri.sourceforge.net
     Mail to Authors:  <a href="Dawnwei.Song@gmail.com">Xiaowei Song</a>; <a href="ycg.yan@gmail.com">Chaogan Yan</a> 
    Version=1.2;
    Release=20080926;
  Last Revised by YAN Chao-Gan 080926, fixed the bug when process ANALYZE format data.
-----------------------------------------------------------

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

rest_readfile.m

SOURCE CODE ^

0001  function [Outdata,VoxDim,Header]=rest_readfile(imageIN,volumeIndex)
0002 % Read file(ANALYZE 7.5, NIFTI, ...) for REST by YAN Chao-Gan
0003 % FORMAT function [Outdata,voxdim, Origin] = rest_readfile(filename)
0004 %                 filename - Analyze file (*.{hdr, img, nii})
0005 %                 Outdata  - data file.
0006 %                 VoxDim   - the size of the voxel.
0007 %                 Header   - It's decided by the format of data file:
0008 %                            for ANALYZE 7.5 - Header.Origin - the origin of the image;
0009 %                            for NIFTI  - Head.fname - the filename of the image.
0010 %                                         Head.dim   - the x, y and z dimensions of the volume
0011 %                                         Head.dt    - A 1x2 array.  First element is datatype (see spm_type).
0012 %                                                      The second is 1 or 0 depending on the endian-ness.
0013 %                                         Head.mat   - a 4x4 affine transformation matrix mapping from
0014 %                                                      voxel coordinates to real world coordinates.
0015 %                                         Head.pinfo - plane info for each plane of the volume.
0016 %                                         Head.pinfo(1,:) - scale for each plane
0017 %                                         Head.pinfo(2,:) - offset for each plane
0018 %                                                      The true voxel intensities of the jth image are given
0019 %                                                      by: val*Head.pinfo(1,j) + Head.pinfo(2,j)
0020 %                                         Head.pinfo(3,:) - offset into image (in bytes).
0021 %                                                     If the size of pinfo is 3x1, then the volume is assumed
0022 %                                                     to be contiguous and each plane has the same scalefactor
0023 %                                                     and offset.
0024 %                                         Head.private - a structure containing complete information in the
0025 %                                                     header
0026 %                                         Header.Origin - the origin of the image;
0027 %-----------------------------------------------------------
0028 %    Copyright(c) 2008~2010
0029 %    State Key Laboratory of Cognitive Neuroscience and Learning in Beijing Normal University
0030 %    Written by YAN Chao-Gan
0031 %    http://resting-fmri.sourceforge.net
0032 %     Mail to Authors:  <a href="Dawnwei.Song@gmail.com">Xiaowei Song</a>; <a href="ycg.yan@gmail.com">Chaogan Yan</a>
0033 %    Version=1.2;
0034 %    Release=20080926;
0035 %  Last Revised by YAN Chao-Gan 080926, fixed the bug when process ANALYZE format data.
0036 %-----------------------------------------------------------
0037  if ~exist('volumeIndex', 'var')
0038     volumeIndex=1;
0039  end
0040 
0041  if ~(strcmpi(imageIN(end-3:end), '.hdr') || strcmpi(imageIN(end-3:end), '.img') || strcmpi(imageIN(end-3:end), '.nii'))
0042      imageIN = [imageIN,'.img'];
0043  end
0044  
0045  [pth,nam,ext] = fileparts(imageIN);
0046 switch ext
0047 case {'.img','.hdr'}
0048     hname = fullfile(pth,[nam '.hdr']);
0049 case {'.nii'}
0050     hname = fullfile(pth,[nam '.nii']);
0051 otherwise
0052     hname = fullfile(pth,[nam '.hdr']);
0053 end;
0054 fp  = fopen(hname,'r','native');
0055 if(fp>0)
0056     fseek(fp,344,'bof');
0057     mgc = deblank(char(fread(fp,4,'uint8')'));
0058     fclose(fp);   %YAN Chao-Gan 080926, fixed the bug when process ANALYZE format data.
0059     switch mgc
0060         case {'ni1','n+1'}
0061             [Outdata,Header]= rest_ReadNiftiImage(imageIN,volumeIndex);
0062             if Header.mat(1,1)>0 % Because the Song's former edition use the radiology convention, So I treat all the Img and Head in RPI coordination. Chaogan Yan 080610
0063                 Outdata = flipdim(Outdata,1);
0064                 Header.mat(1,:) = -1*Header.mat(1,:);
0065             end
0066             if Header.mat(2,2)<0
0067                 Outdata = flipdim(Outdata,2);
0068                 Header.mat(2,:) = -1*Header.mat(2,:);
0069             end
0070             if Header.mat(3,3)<0
0071                 Outdata = flipdim(Outdata,3);
0072                 Header.mat(3,:) = -1*Header.mat(3,:);
0073             end
0074             temp=inv(Header.mat)*[0,0,0,1]';
0075             Header.Origin=temp(1:3)';
0076             VoxDim=abs([Header.mat(1,1),Header.mat(2,2),Header.mat(3,3)]);
0077         otherwise
0078             [Outdata,VoxDim,Origin]= rest_ReadAnalyzeImage(imageIN);
0079             Header.Origin=Origin;
0080          end;
0081 else
0082     error(sprintf('Error opening header file. Please check whether the %s file exist.',hname));
0083     fclose(fp);  %YAN Chao-Gan 080926, fixed the bug when process ANALYZE format data.
0084 end
0085 %fclose(fp);  %YAN Chao-Gan 080926, fixed the bug when process ANALYZE format data.

Generated on Wed 29-Apr-2009 01:06:38 by m2html © 2005