Home > rest_20090422 > rest_WriteAnalyzeImage.m

rest_WriteAnalyzeImage

PURPOSE ^

Write ANALYZE 7.5 file by Yong He and Xiao-Wei Song

SYNOPSIS ^

function [] = rest_WriteAnalyzeImage(data,filename,isize,vsize,AOrigin,dtype)

DESCRIPTION ^

Write ANALYZE 7.5 file by Yong He and Xiao-Wei Song
 %------------------------------------------------------------------------------------------------------------------------------
 Write analyze format file.
 FORMAT function [] = rest_writefile(data,filename,isize,vsize,dtype)
           filename - Analyze file (*.{hdr, img})
               data - .mat data file.                            
              isize - the size of the data
              vsize - the size of the voxel
              dtype - the data type.

 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, 
 %------------------------------------------------------------------------------------------------------------------------------

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

rest_WriteAnalyzeImage.m

SOURCE CODE ^

0001 function [] = rest_WriteAnalyzeImage(data,filename,isize,vsize,AOrigin,dtype)
0002 %Write ANALYZE 7.5 file by Yong He and Xiao-Wei Song
0003 % %------------------------------------------------------------------------------------------------------------------------------
0004 % Write analyze format file.
0005 % FORMAT function [] = rest_writefile(data,filename,isize,vsize,dtype)
0006 %           filename - Analyze file (*.{hdr, img})
0007 %               data - .mat data file.
0008 %              isize - the size of the data
0009 %              vsize - the size of the voxel
0010 %              dtype - the data type.
0011 %
0012 % Written by Yong He, April,2004
0013 % Medical Imaging and Computing Group (MIC), National Laboratory of Pattern Recognition (NLPR),
0014 % Chinese Academy of Sciences (CAS), China.
0015 % E-mail: yhe@nlpr.ia.ac.cn
0016 % Copywrite (c) 2004,
0017 % %------------------------------------------------------------------------------------------------------------------------------
0018 
0019 % remove file extension if exists
0020 % if filename(end-3:end) == '.hdr'
0021 %   filename = filename(1:end-4);
0022 % end
0023 % if filename(end-3:end) == '.img'
0024 %   filename = filename(1:end-4);
0025 % end
0026 
0027 %20071031, Dawnwei.Song revised for Scaling
0028 if strcmpi(lower(dtype),'int16'),
0029     tmpData =double(data(0~=data));
0030     if length(tmpData)>1000 && mean(abs(tmpData))<100,    %This is a random value I set for supress data-type conversion!
0031         dtype='double';
0032     end
0033 end
0034 
0035 % write img
0036 fid = fopen([filename,'.img'],'w');
0037 if fid<0, error(sprintf('Failed to Open file:%s', [filename,'.img'])); end
0038 fwrite(fid,data,dtype);
0039 fclose(fid);
0040 
0041 %DawnWei.Song 20071021 Revised
0042 switch lower(dtype),
0043     case 'double',
0044         datatype = 64;
0045         bitvox = 64; 
0046     case 'float',
0047         datatype = 16;
0048         bitvox = 16;
0049     case 'float32',
0050         datatype = 8;
0051         bitvox = 8;
0052     case 'int16',
0053         datatype = 4;
0054         bitvox = 4;
0055     case 'uint8',
0056         datatype = 2;
0057         bitvox = 2;    
0058     otherwise % need to add other decription
0059         error('unsupported data format now.');
0060 end
0061 
0062 scale = 1;
0063 %write hdr
0064 fid = fopen([filename,'.hdr'],'w');
0065 fwrite(fid,zeros(1,348),'uint8');
0066 fseek(fid,0,'bof');
0067 fwrite(fid,348,'int16');
0068 fseek(fid,32,'bof');
0069 fwrite(fid,16384,'int16');
0070 fseek(fid,38,'bof');
0071 fwrite(fid,'r','char');
0072 fseek(fid,40,'bof');
0073 fwrite(fid,[4,isize,1],'int16');
0074 fseek(fid,40+30,'bof');
0075 fwrite(fid,datatype,'int16');
0076 fseek(fid,40+32,'bof');
0077 fwrite(fid,bitvox,'int16');
0078 fseek(fid,40+36,'bof');
0079 fwrite(fid,[0,vsize],'float32');
0080 fseek(fid,40+72,'bof');
0081 fwrite(fid,scale,'float');
0082 
0083 
0084 switch int2str(isize)
0085     case '79  95  69'   % 'default''[2 2 2]'
0086         if vsize(1) == 2 & vsize(2) == 2 & vsize(3) == 2 
0087             origin = [40 57 26];
0088         else  error ('invalid voxel size.'); end            
0089     case '54  64  50'   % 'default''[3 3 3]'
0090         if vsize(1) == 3 & vsize(2) == 3 & vsize(3) == 3 
0091             origin = [28 28 23];
0092         else  error ('invalid voxel size.'); end                    
0093     case '91  109  91'  % 'template''[2 2 2]'
0094         if vsize(1) == 2 & vsize(2) == 2 & vsize(3) == 2 
0095             origin = [46 64 37];
0096         else  error ('invalid voxel size.'); end
0097     case '181  217  181'  % 'template''[1 1 1]'
0098         if vsize(1) == 1 & vsize(2) == 1 & vsize(3) == 1 
0099             origin = [91 127 73];
0100         else  error ('invalid voxel size.'); end 
0101     case '61  73  61'   % 'template' '[3 3 3]'
0102         if vsize(1) == 3 & vsize(2) == 3 & vsize(3) == 3 
0103             origin = [31 43 25];
0104         else  origin = [31 43 25]; end
0105     case '53  63  46'   % 'template' '[3 3 3]'
0106         if vsize(1) == 3 & vsize(2) == 3 & vsize(3) == 3 
0107             origin = [27 38 18];
0108         else  error ('invalid voxel size.'); end
0109     case '161 191 151'   % 'template' '[1 1 1]'
0110         if vsize(1) == 1 & vsize(2) == 1 & vsize(3) == 1
0111             origin = [82 82 66];
0112         else  error ('invalid voxel size.'); end
0113     % case '128 128 25'   % 'template' '[1.72 1.72 5]'
0114         % if vsize(1) == 1.72 & vsize(2) == 1.72 & vsize(3) == 5
0115             % origin = [0 0 0];
0116         % else  error ('invalid voxel size.'); end
0117     otherwise
0118         %origin = [0 0 0];
0119 end
0120 origin =AOrigin;%Revised by Dawnsong
0121 origin =reshape(origin, [1 3]);
0122 
0123 fseek(fid,148+105,'bof');
0124 fwrite(fid,[origin,0,0],'int16');
0125 fclose(fid);

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