Home > bioelectromagnetism > ge_series_read.m

ge_series_read

PURPOSE ^

ge_series_read - reads a volume of images from a GE series

SYNOPSIS ^

function [ ge, lastfile ] = ge_series_read(examPath, series)

DESCRIPTION ^

 ge_series_read - reads a volume of images from a GE series
 
 [ ge, lastfile ] = ge_series_read(examPath, seriesPath)
 
 examPath    - string path to exam directory
 seriesPath  - string path to series directory, 
               relative to examPath
 
 reads the volume of files in seriesPath, which is stored
 under examPath (it can return the name of the last file read).
 
 The files are assumed to be arranged in series subdirectories
 below the examPath, so series files would be located as such:
 examPath/seriesN/*.MR or examPath/seriesN/I.*.  The function 
 will try to find all *.MR or I.* files and determine the 
 correct numerical order of the image files.
 
 The function assumes the GE files are big endian.  This is
 an example of the returned struct, where ge.img contains
 the image volume data:
 
 ge = 
 
      pix_hdr: [1x1 struct]
    img.offset: 8432
       su_hdr: [1x1 struct]
       ex_hdr: [1x1 struct]
       se_hdr: [1x1 struct]
       hdr.image: [1x1 struct]
          img: [256x256x11 double]
 
 see also ge_hdr_read, ge_series2avw

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [ ge, lastfile ] = ge_series_read(examPath, series)
0002 
0003 % ge_series_read - reads a volume of images from a GE series
0004 %
0005 % [ ge, lastfile ] = ge_series_read(examPath, seriesPath)
0006 %
0007 % examPath    - string path to exam directory
0008 % seriesPath  - string path to series directory,
0009 %               relative to examPath
0010 %
0011 % reads the volume of files in seriesPath, which is stored
0012 % under examPath (it can return the name of the last file read).
0013 %
0014 % The files are assumed to be arranged in series subdirectories
0015 % below the examPath, so series files would be located as such:
0016 % examPath/seriesN/*.MR or examPath/seriesN/I.*.  The function
0017 % will try to find all *.MR or I.* files and determine the
0018 % correct numerical order of the image files.
0019 %
0020 % The function assumes the GE files are big endian.  This is
0021 % an example of the returned struct, where ge.img contains
0022 % the image volume data:
0023 %
0024 % ge =
0025 %
0026 %      pix_hdr: [1x1 struct]
0027 %    img.offset: 8432
0028 %       su_hdr: [1x1 struct]
0029 %       ex_hdr: [1x1 struct]
0030 %       se_hdr: [1x1 struct]
0031 %       hdr.image: [1x1 struct]
0032 %          img: [256x256x11 double]
0033 %
0034 % see also ge_hdr_read, ge_series2avw
0035 %
0036 
0037 
0038 % $Revision: 1.1 $ $Date: 2004/11/12 01:32:35 $
0039 
0040 % Licence: GPL, no express or implied warranties
0041 %
0042 % Souheil J. Inati  <souheil.inati@nyu.edu> at 03/2003
0043 % Dartmouth College, May 2000
0044 %
0045 % Darren.Weber@flinders.edu.au, March 2003
0046 % - Substantially redesigned file handling and function
0047 %   call structures for integration with mri_toolbox at
0048 %   http://eeg.sf.net
0049 % - Requested permission to distribute code under GPL licence
0050 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0051 
0052 
0053 version = '[$Revision: 1.1 $]';
0054 fprintf('\nGE_SERIES_READ [v%s]\n',version(12:16));  tic;
0055 
0056 
0057 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0058 % --- Extract filenames and exam/series/image components
0059 
0060 % check if series is int
0061 if isnumeric(series),
0062     series = num2str(series);
0063 end
0064 
0065 seriesPath  = [examPath,filesep,series,filesep];
0066 
0067 fprintf('...searching for files in: %s\n',seriesPath);
0068 
0069 if exist(seriesPath) ~= 7,
0070     msg = sprintf('...cannot find path: %s!\n',seriesPath);
0071     error(msg);
0072 end
0073 
0074 dirFiles = dir(seriesPath);
0075 n = 0;
0076 for i = 1:length(dirFiles),
0077     % check whether this file is a directory
0078     if ~ dirFiles(i).isdir,
0079         % check whether it ends with .MR
0080         if findstr(dirFiles(i).name,'.MR'),
0081             %fprintf(sprintf('found *.MR file: %s\n',dirFiles(i).name));
0082             n = n + 1;
0083             seriesFileNames{n} = dirFiles(i).name;
0084             imageFormat = 'MR';
0085         elseif findstr(dirFiles(i).name,'I.'),
0086             %fprintf(sprintf('found I. file: %s\n',dirFiles(i).name));
0087             n = n + 1;
0088             seriesFileNames{n} = dirFiles(i).name;
0089             imageFormat = 'I';
0090         end
0091     end
0092 end
0093 
0094 if n == 0,
0095     msg = sprintf('...Found %d image files (I.* or *.MR)!\n',n);
0096     error(msg);
0097 else
0098     fprintf('...Found %d image files\n',n);
0099 end
0100 
0101 if isequal(imageFormat,'MR'),
0102     
0103     fprintf('...sorting *.MR image files into numerical order\n');
0104     
0105     % extract exam/series/image numbers and sort into numerical order
0106     fileName.position.exam   = findstr(seriesFileNames{1},'E');
0107     fileName.position.series = findstr(seriesFileNames{1},'S');
0108     fileName.position.image  = findstr(seriesFileNames{1},'I');
0109     
0110     range = [ (fileName.position.exam + 1) : (fileName.position.series - 1) ];
0111     examN = seriesFileNames{1}(range);
0112     
0113     range   = [ (fileName.position.series + 1) : (fileName.position.image - 1) ];
0114     seriesN = seriesFileNames{1}(range);
0115     
0116     for i = 1:length(seriesFileNames),
0117         range = [ fileName.position.image + 1 ];
0118         imageNumbers{i} = seriesFileNames{i}(range:end);
0119         imageNumbers{i} = strrep(imageNumbers{i},'.MR','');
0120     end
0121     imageNumbers = str2double(imageNumbers);
0122     [n,i] = sort(imageNumbers);
0123     
0124     sortedFileNames = seriesFileNames(i);
0125     
0126 elseif isequal(imageFormat, 'I'),
0127     
0128     fprintf('...sorting I.* image files into numerical order\n');
0129     
0130     % extract image numbers, which might already be in numerical order
0131     
0132     for i = 1:length(seriesFileNames),
0133         imageNumbers{i} = seriesFileNames{i}(3:end);
0134     end
0135     imageNumbers = str2double(imageNumbers);
0136     [n,i] = sort(imageNumbers);
0137     
0138     sortedFileNames = seriesFileNames(i);
0139     
0140 end
0141 
0142 % --- Create the name of the first file in examPath
0143 firstfile = fullfile(seriesPath,sortedFileNames{1});
0144 
0145 
0146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0147 % --- Read the header from the first file in the series
0148 ge = ge_hdr_read(firstfile);
0149 
0150 
0151 
0152 im_offset = ge.img.offset;  % this is not so good, as the ge.img is replaced below!
0153 
0154 
0155 
0156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0157 % --- Read the image data from the GE series files
0158 
0159 version = '[$Revision: 1.1 $]';
0160 fprintf('\nGE_SERIES_READ [v%s]\n',version(12:16));
0161 fprintf('...reading image data\n');
0162 
0163 % initialize some variables
0164 nX = ge.hdr.image.imatrix_X; % X Voxels
0165 nY = ge.hdr.image.imatrix_Y; % Y Voxels
0166 nZ = ge.hdr.image.slquant;   % Z Voxels (slice quantity)
0167 
0168 sliceSize = nX*nY;
0169 ge.img = zeros(nX, nY, nZ);
0170 
0171 fprintf('...reading ');
0172 
0173 for i = 1:nZ,
0174     
0175     imageFile = fullfile(seriesPath,sortedFileNames{i});
0176     
0177     % output filename to indicate progress
0178     if i == 1,
0179         backspaces = '';
0180     else
0181         filePreviousLength = length(fullfile(seriesPath,sortedFileNames{i-1}));
0182         backspaces = repmat('\b',1,filePreviousLength);
0183     end
0184     fprintf([backspaces,'%s'],imageFile);
0185     
0186     
0187     % Open the file
0188     [fid,message] = fopen(imageFile,'r','b'); % big endian
0189     if (fid == -1)
0190         fprintf('Cannot Open %s (big endian).\n',imageFile);
0191         
0192         % Can try to read little endian (shouldn't be necessary!)
0193         fprintf('Trying to read little endian\n');
0194         [fid,message] = fopen(imageFile,'r','l'); % little endian
0195         if (fid == -1),
0196             fprintf('Cannot Open %s (little endian).\n',imageFile);
0197             break
0198         end
0199     end
0200     
0201     % Skip the header, goto the data
0202     fseek(fid,im_offset,-1);
0203     % Read the slice data
0204     buffer = fread(fid,sliceSize,sprintf('int%d',ge.hdr.image.screenformat));
0205     % append the slice to the imageSet
0206     ge.img(:,:,i) = reshape(buffer, nX, nY);
0207     
0208     % Close the file
0209     status = fclose(fid);
0210     
0211     % Set the lastfile
0212     lastfile = imageFile;
0213 end
0214 
0215 t=toc; fprintf('\n...done (%5.2f sec).\n',t);
0216 
0217 return

Generated on Mon 15-Aug-2005 15:36:19 by m2html © 2003