Home > fmri > utils > GE2SPM > GE_convertT2PD.m

GE_convertT2PD

PURPOSE ^

SYNOPSIS ^

function [outVol] = GE_convertT2PD(inDir,outstub)

DESCRIPTION ^

 [vols] = GE_convertT2PD(inDir,outstub)

 Converts a series of GE slices obtained using a dual-echo protocol into two
 Analyze format files. The first file contains the T2-weighted image, and the
 other contains the proton density (PD) image.  The images are interleaved
 during acquisition.

 09/03/03 Petr Janata

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [outVol] = GE_convertT2PD(inDir,outstub)
0002 %
0003 % [vols] = GE_convertT2PD(inDir,outstub)
0004 %
0005 % Converts a series of GE slices obtained using a dual-echo protocol into two
0006 % Analyze format files. The first file contains the T2-weighted image, and the
0007 % other contains the proton density (PD) image.  The images are interleaved
0008 % during acquisition.
0009 %
0010 % 09/03/03 Petr Janata
0011 %
0012 
0013 % This script was adapted from GE_convertVolume.m:
0014 %
0015 % Converts a series of GE slices into Analyze format for SPM.
0016 % inDir is the name of the directory containing the first file of
0017 % the series, e.g. 003
0018 % runnum is the run number of the set you want to convert
0019 % outName is used for naming the Analyze files
0020 % status = 1 if there is an error.
0021 %
0022 % Souheil J. Inati
0023 % Dartmouth College
0024 % May 2000
0025 % souheil.inati@dartmouth.edu
0026 %
0027 
0028 img_types = {'PD','T2'};
0029 ntypes = length(img_types);
0030 
0031 status = 1;
0032 
0033 if (nargin < 2)
0034         error('Not enough input arguments.')
0035         return
0036 end
0037 
0038 for itype = 1:ntypes
0039   fprintf('Working on conversion of %s image...\n', img_types{itype});
0040   
0041   % Create the name of the first file in inDir
0042   firstfile = fullfile(inDir,sprintf('I.%03d', itype));
0043 
0044   % Generate the Analyze header and other info from the first file
0045   fprintf('Getting header info from: %s\n', firstfile);
0046   [header, orient, im_offset, adwcount] = GE_createSPMHeader(firstfile);
0047   volSize =  [header.dim(2) header.dim(3) header.dim(4)];
0048   
0049   % Reorient the header to SPM orientation
0050   header = GE_reorientHeader(header, orient); 
0051 
0052   imageVol = zeros(volSize);
0053   for islice = 1:volSize(3)
0054     % Determine name of current file
0055     curr_fname = fullfile(inDir,sprintf('I.%03d',(islice-1)*2+itype));
0056     
0057     % Open the file
0058     fid = fopen(curr_fname,'rb');
0059     if fid == -1
0060       fprintf('Failed to open: %s\n', curr_fname);
0061       imageVol = [];
0062       status = 0;
0063       return
0064     end
0065     
0066     % Read slice from file
0067     fseek(fid,im_offset+1,-1);
0068     buffer = fread(fid,volSize(1:2),sprintf('ubit%d',header.bitpix));
0069     
0070     % Append slice to image volume
0071     imageVol(:,:,islice) = reshape(buffer,volSize(1),volSize(2));
0072     
0073     % Close the file
0074     fclose(fid);
0075   end % for islice =
0076 
0077   % Reorient the volume to SPM orientation
0078   imageVol = GE_reorientImage(imageVol, orient);
0079 
0080   outName = sprintf('%s_%s',outstub,img_types{itype});
0081   
0082   % Write the SPM header file
0083   outFile = strcat(outName,'.hdr');
0084   status = GE_writeSPMHeader(outFile,header);
0085 
0086   % Write out the Analyze File
0087   outFile = [outName sprintf('.img')];
0088   [fid,message] = fopen(outFile,'w');
0089   if (fid == -1)
0090     fprintf('Cannot Open %s for writing.\n',outFile);
0091     error(message);
0092   end
0093 
0094   fwrite(fid,reshape(imageVol,1,prod(size(imageVol))),'int16');
0095   status = fclose(fid);
0096 
0097   % Done with vol
0098   fprintf('Wrote %s.img.\n',outName);
0099 end %for itype
0100 
0101 % Done
0102 fprintf('\nConversion Finished. \n');
0103 
0104 return

Generated on Sun 25-Aug-2019 04:00:39 by m2html © 2003