source: aedes_readswiftsgl.m

Last change on this file was 132, checked in by tjniskan, 7 years ago
  • Added read support for SWIFT reconstruction files (*.sgl)
  • Changed the layout of results viewer GUI. Also added more options

and all options should now be "remembered"

M aedes_getfilefilter.m
A aedes_readswiftsgl.m
M aedes_data_read.m
M aedes_resviewer.m
M aedes.m
M aedes_revision.m
M aedes_saveres.m
M aedes_getdataformat.m

File size: 5.0 KB
Line 
1function [DATA,msg] = aedes_readswiftsgl(filename,varargin)
2% AEDES_READSWIFTSGL - Read SWIFT reconstruction (*.sgl) files
3%   
4%
5% Synopsis:
6%       [DATA,msg]=aedes_readswiftsgl(filename,varargin)
7%
8% Description:
9%       The function reads SWIFT reconstruction SGL files. If not
10%       specified, [256 256 256] data size is assumed. The DATASIZE
11%       property can be used to specify the data size, and the INTERPSIZE
12%       property can be used to upsample data. The DATAINTERPSIZEPROMPT
13%       property can be used to prompt for these data sizes.
14%
15%       If called without input arguments or if the FILENAME arguments is
16%       an empty string, the user is prompted to input file.
17%
18%       property/value pairs
19%
20%       'DataSize'   - Size of the data matrix (default: [256 256 256])
21%       
22%       'InterpSize' - Interpolated size (default: no interpolation, empty)
23%
24%       'DataInterpSizePrompt' - Prompt for Data/Interp sizes (default: 0)
25%
26%       'Wbar' - Show/hide waitbar (default: false)
27%       
28% Examples:
29%       [DATA,msg]=aedes_readswiftsgl(filename,'prop1',value1,'prop2',value2,...);
30%
31% See also:
32%       AEDES_READFID, AEDES_READ_NIFTI, AEDES
33
34% This function is a part of Aedes - A graphical tool for analyzing
35% medical images
36%
37% Copyright (C) 2006 Juha-Pekka Niskanen <Juha-Pekka.Niskanen@uku.fi>
38%
39% Department of Physics and Mathematics, Department of Neurobiology
40% University of Eastern Finland, FINLAND
41%
42% This program may be used under the terms of the GNU General Public
43% License version 2.0 as published by the Free Software Foundation
44% and appearing in the file LICENSE.TXT included in the packaging of
45% this program.
46%
47% This program is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
48% WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
49
50% Defaults
51DATA = [];
52msg = '';
53InterpSize = [];  % If empty, don't interpolate
54DataSize = [256 256 256];
55DataInterpSizePrompt = false;
56ShowWbar = false;
57
58% Check if we need to be silent about errors
59if nargout>1
60  silent = true;
61else
62  silent = false;
63end
64
65%% Parse varargin
66for ii=1:2:length(varargin)
67  switch lower(varargin{ii})
68   case {'interpsize'}
69     InterpSize = varargin{ii+1};
70     
71    case 'datasize'
72      DataSize = varargin{ii+1};
73     
74    case 'datainterpsizeprompt'
75      DataInterpSizePrompt = varargin{ii+1};
76     
77    case 'wbar'
78      ShowWbar = varargin{ii+1};
79     
80   otherwise
81    done=false;
82    msg=sprintf('Unknown parameter "%s"!',varargin{ii});
83    return
84  end
85end
86
87% Prompt for file if not given as an input argument
88if nargin==0 || isempty(filename)
89  [fname, fpath, findex] = uigetfile( ...
90    {'*.sgl','SWIFT SGL-files (*.sgl)'; ...
91    '*.*',  'All Files (*.*)'}, ...
92    'Select SWIFT SGL file','');
93  if isequal(fname,0)
94    msg = 'Canceled';
95    return
96  end
97  filename = [fpath,fname];
98end
99
100% Get file name and path
101[fp,fn,fe]=fileparts(filename);
102
103
104% Prompt for data/interpolation size
105if DataInterpSizePrompt
106  done=false;
107  while ~done
108    answer = inputdlg({'Data size','Interpolated size'},...
109      'Input data/interp size',1,...
110      {'[256 256 256]','[256 256 256]'});
111    if isempty(answer)
112      msg = 'Canceled';
113      return
114    end
115    DataSize = str2num(answer{1});
116    InterpSize = str2num(answer{2});
117    if length(DataSize)~=3 || length(InterpSize)~=3
118      h=errordlg('Invalid Data/Interpolation size!',...
119        'Invalid size','modal');
120      uiwait(h)
121    else
122      done = true;
123    end
124  end
125  if all(DataSize==InterpSize)
126    InterpSize = [];
127  end
128end
129
130% Check interpolation size
131if ~isempty(InterpSize)
132  if length(InterpSize)~=3
133    msg = 'Invalid interpolation size!';
134    if ~silent
135      error(msg)
136    end
137    return
138  end
139end
140
141% Show waitbar
142if ShowWbar
143  [wbh,txh] = aedes_calc_wait('Reading data in SWIFT SGL format...');
144end
145
146% Open file for reading
147fid=fopen(filename,'r');
148if fid<0
149  msg = sprintf('Could not open file %s for reading',filename);
150  if ~silent
151    error(msg)
152  end
153  return
154end
155
156% Read data
157I=fread(fid,inf,'float32','ieee-be');
158
159% Close file
160fclose(fid);
161
162% Reshape to correct size
163try
164  I=reshape(I,DataSize);
165  I=permute(I,[2 1 3]);
166catch
167  msg = sprintf('Could not reshape data to size [%d %d %d]',...
168    DataSize);
169  if ~silent
170    error(msg)
171  end
172  return
173end
174
175% Interpolate data if requested
176if ~isempty(InterpSize)
177 
178  % Update waitbar
179  set(txh,'string',sprintf('Interpolating data to [%d %d %d]',...
180    InterpSize))
181  drawnow
182 
183  % Fix the InterpSize to be greater or equal to the DataSize
184  for ii=1:3
185    InterpSize(ii) = max(InterpSize(ii),DataSize(ii));
186  end
187 
188  if ~all(InterpSize==DataSize)
189 
190    % Scale to positive
191    min_val = min(I(:));
192    I = I+min_val;
193   
194    % Interpolate to InterpSize using Fourier interpolation
195    I = fftshift(fftn(I));
196    I(InterpSize(1),InterpSize(2),InterpSize(3))=single(0);
197    I=abs(ifftn(ifftshift(I)));
198   
199    % Scale back to original minimum
200    I=I-min_val;
201  end
202end
203
204% Construct DATA structure
205DATA.DataFormat = 'swift_sgl';
206DATA.HDR.fname = [fn,fe];
207DATA.HDR.fpath = [fp,filesep];
208DATA.FTDATA = I;
209
210if ShowWbar
211  delete(wbh)
212end
213
214
215
Note: See TracBrowser for help on using the repository browser.

Powered by Trac 1.0.9.Copyright © Juha-Pekka Niskanen 2008