0001 function [ ge, lastfile ] = ge_series_read(examPath, series)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053 version = '[$Revision: 1.1 $]';
0054 fprintf('\nGE_SERIES_READ [v%s]\n',version(12:16)); tic;
0055
0056
0057
0058
0059
0060
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
0078 if ~ dirFiles(i).isdir,
0079
0080 if findstr(dirFiles(i).name,'.MR'),
0081
0082 n = n + 1;
0083 seriesFileNames{n} = dirFiles(i).name;
0084 imageFormat = 'MR';
0085 elseif findstr(dirFiles(i).name,'I.'),
0086
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
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
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
0143 firstfile = fullfile(seriesPath,sortedFileNames{1});
0144
0145
0146
0147
0148 ge = ge_hdr_read(firstfile);
0149
0150
0151
0152 im_offset = ge.img.offset;
0153
0154
0155
0156
0157
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
0164 nX = ge.hdr.image.imatrix_X;
0165 nY = ge.hdr.image.imatrix_Y;
0166 nZ = ge.hdr.image.slquant;
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
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
0188 [fid,message] = fopen(imageFile,'r','b');
0189 if (fid == -1)
0190 fprintf('Cannot Open %s (big endian).\n',imageFile);
0191
0192
0193 fprintf('Trying to read little endian\n');
0194 [fid,message] = fopen(imageFile,'r','l');
0195 if (fid == -1),
0196 fprintf('Cannot Open %s (little endian).\n',imageFile);
0197 break
0198 end
0199 end
0200
0201
0202 fseek(fid,im_offset,-1);
0203
0204 buffer = fread(fid,sliceSize,sprintf('int%d',ge.hdr.image.screenformat));
0205
0206 ge.img(:,:,i) = reshape(buffer, nX, nY);
0207
0208
0209 status = fclose(fid);
0210
0211
0212 lastfile = imageFile;
0213 end
0214
0215 t=toc; fprintf('\n...done (%5.2f sec).\n',t);
0216
0217 return