Home > rest_20090422 > rest_ReadAnalyzeImage.m

rest_ReadAnalyzeImage

PURPOSE ^

Read file(ANALYZE 7.5, ...) for REST by Yong He and Xiao-Wei Song

SYNOPSIS ^

function [Outdata,VoxDim, Origin] = rest_ReadAnalyzeImage(filename)

DESCRIPTION ^

 Read file(ANALYZE 7.5, ...) for REST by Yong He and Xiao-Wei Song
 %------------------------------------------------------------------------------------------------------------------------------
 Read analyze format file.
 FORMAT function [Outdata,voxdim, Origin] = rest_readfile(filename)
                 filename - Analyze file (*.{hdr, img})
                 Outdata  - data file.                            
                 VoxDim   - the size of the voxel.

 Written by Yong He, April,2004
 Medical Imaging and Computing Group (MIC), National Laboratory of Pattern Recognition (NLPR),
 Chinese Academy of Sciences (CAS), China.
 E-mail: yhe@nlpr.ia.ac.cn
 Copywrite (c) 2004, 
 %------------------------------------------------------------------------------------------------------------------------------
Revised by dawnsong, 20070511
Todo:
1. read the origin and revise the rest_writefile.m

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

rest_ReadAnalyzeImage.m

SOURCE CODE ^

0001 function [Outdata,VoxDim, Origin] = rest_ReadAnalyzeImage(filename)
0002 % Read file(ANALYZE 7.5, ...) for REST by Yong He and Xiao-Wei Song
0003 % %------------------------------------------------------------------------------------------------------------------------------
0004 % Read analyze format file.
0005 % FORMAT function [Outdata,voxdim, Origin] = rest_readfile(filename)
0006 %                 filename - Analyze file (*.{hdr, img})
0007 %                 Outdata  - data file.
0008 %                 VoxDim   - the size of the voxel.
0009 %
0010 % Written by Yong He, April,2004
0011 % Medical Imaging and Computing Group (MIC), National Laboratory of Pattern Recognition (NLPR),
0012 % Chinese Academy of Sciences (CAS), China.
0013 % E-mail: yhe@nlpr.ia.ac.cn
0014 % Copywrite (c) 2004,
0015 % %------------------------------------------------------------------------------------------------------------------------------
0016 %Revised by dawnsong, 20070511
0017 %Todo:
0018 %1. read the origin and revise the rest_writefile.m
0019 
0020 % remove file extension if exists
0021 if length(filename)>4
0022     if strcmpi(filename(end-3:end), '.hdr')
0023       filename = filename(1:end-4);
0024     end
0025     if strcmpi(filename(end-3:end), '.img')
0026       filename = filename(1:end-4);
0027     end
0028 end
0029 
0030 % open .hdr file
0031 fid = fopen([filename,'.hdr'],'r');
0032 if fid > 0
0033     fseek(fid,40,'bof');
0034     dim = fread(fid,8,'int16');
0035     byteswap = 'native';
0036     % byte swapping
0037     if (dim(1) > 15 | dim(1) < 0)
0038         byteswap = 'ieee-be';
0039         fclose(fid);
0040         fid = fopen([filename,'.hdr'],'r','ieee-be');
0041         fseek(fid,40,'bof');
0042         dim = fread(fid,8,'int16');
0043         if (dim(1) > 15 | dim(1) < 0)
0044             byteswap = 'ieee-le';
0045             fclose(fid);
0046             fid = fopen([filename,'.hdr'],'r','ieee-le');
0047             fseek(fid,40,'bof');
0048             dim = fread(fid,8,'int16');
0049             if (dim(1) > 15 | dim(1) < 0)
0050                 error('Error opening header file. Dimension error');end
0051         end
0052     end
0053 else error(sprintf('Error opening header file. Please check whether the %s.hdr file exist.',filename));end
0054 
0055 fseek(fid,40+30,'bof');
0056 DataType = fread(fid,1,'int16');
0057 fseek(fid,40+36,'bof');
0058 VoxDim = fread(fid,8,'float');
0059 fseek(fid,40+72,'bof');
0060 Scale = fread(fid,1,'float');
0061 
0062 %Dawnsong added, 20070904, for ROI coordinate transformation
0063 fseek(fid,148+105,'bof');
0064 Origin = fread(fid,3,'int16');
0065 
0066 fclose(fid);
0067 
0068 % open .img file
0069 fid = fopen([filename,'.img'],'r',byteswap);
0070 if fid < 0
0071   error(sprintf('Error opening data file. Please check whether the %s.img file exist',filename));
0072 end
0073 
0074 switch DataType
0075     case 2
0076         dtype = 'uint8';
0077     case 4
0078         dtype = 'int16';
0079     case 8
0080         dtype = 'float32';
0081     case 16
0082         dtype = 'float';
0083     case 32
0084         dtype = 'float32';
0085     case 64
0086         dtype = 'double';
0087     otherwise
0088         error('Invalid data type!');
0089 end
0090 
0091 switch dim(1)
0092   case 4
0093       len = dim(2)*dim(3)*dim(4)*dim(5);
0094       Outdata = fread(fid,len,dtype);
0095       if dim(5) == 1  Outdata = reshape(Outdata,dim(2),dim(3),dim(4));      
0096       else            Outdata = reshape(Outdata,dim(2),dim(3),dim(4),dim(5));end
0097   case 3
0098       len = dim(2)*dim(3)*dim(4);
0099       Outdata = fread(fid,len,dtype);
0100       Outdata = reshape(Outdata,dim(2),dim(3),dim(4));    
0101 end
0102 
0103 fclose(fid);
0104 if Scale ~= 1 & Scale ~= 0
0105     Outdata = Scale*Outdata; end
0106 VoxDim = VoxDim(2:4);
0107

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