source: misclib/nifti3dto4d.m @ 80

Last change on this file since 80 was 80, checked in by tjniskan, 10 years ago
  • Changed the historical "an2_" prefix to "aedes_" in all files. NOTE:

Any script or function relying to Aedes functions will be broken
because of this. Just do a search/replace from "an2_" to "aedes_" in
your files and all should be well...

  • Changed the name of an2_readtab.m to a more informative

aedes_readphasetable.m

File size: 4.3 KB
Line 
1function nifti3dto4d(infiles,outfile,varargin)
2% NIFTI3DTO4D - Convert a series of 3D NIfTI volumes to a single 4D NIfTI
3%               file
4%
5% Synopsis:
6%       data = nifti3dto4d(infiles,outfile,param1,value1,param2,value2,...)
7%
8% Description:
9%
10% Examples:
11%
12% See also:
13%       
14
15% This function is a part of Aedes - A graphical tool for analyzing
16% medical images
17%
18% Copyright (C) 2006 Juha-Pekka Niskanen <Juha-Pekka.Niskanen@uku.fi>
19%
20% Department of Physics, Department of Neurobiology
21% University of Kuopio, FINLAND
22%
23% This program may be used under the terms of the GNU General Public
24% License version 2.0 as published by the Free Software Foundation
25% and appearing in the file LICENSE.TXT included in the packaging of
26% this program.
27%
28% This program is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29% WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30
31% Defaults ---------------------------------------
32showWbar = true;
33TR = [];
34
35% Parse input arguments --------------------------
36if nargin==0 || isempty(infiles)
37  % Prompt files to read
38  [fname,fpath,findex] = aedes_juigetfiles({'*.nii','NIfTI files (*.nii)';...
39        '*.*','All Files (*.*)'},'Select volumes',pwd);
40 
41  if isequal(fname,0)
42        % Canceled
43        return
44  end
45  tmp={fpath{:};fname{:}};
46  infiles = cell(1,size(tmp,2));
47  for ii=1:size(tmp,2)
48        infiles{ii} = [tmp{:,ii}];
49  end
50end
51
52if nargin<2 || isempty(outfile)
53  % Select output file
54  [fn,fp] = uiputfile({'*.nii','NIfTI files (*.nii)';...
55        '*.*','All Files (*.*)'},'Select output file',...
56        '4D_nifti.nii');
57  if isequal(fn,0)
58    % Canceled
59    return
60  end
61  outfile = fullfile(fp,fn);
62end
63
64% Read the first image and get header parameters
65tmp_data = aedes_read_nifti(infiles{1});
66TimeUnits = tmp_data.HDR.timeunits;
67xyzunits = tmp_data.HDR.xyzunits;
68byteorder = tmp_data.HDR.byteorder;
69vox_size = tmp_data.HDR.FileHeader.dime.pixdim(2:4);
70
71% Try to determine the TR value from the description field (SPM writes this
72% information here when converting DICOMs, NOTE: THIS MAY FAIL!!!)
73if isempty(TR)
74  description = tmp_data.HDR.FileHeader.hist.descrip;
75  ind=strfind(tmp_data.HDR.FileHeader.hist.descrip,'TR=');
76  TR = str2num(tmp_data.HDR.FileHeader.hist.descrip(ind+3:ind+6))/1000;
77end
78 
79
80
81data_sz(1)=size(tmp_data.FTDATA,1);
82data_sz(2)=size(tmp_data.FTDATA,2);
83data_sz(3)=size(tmp_data.FTDATA,3);
84
85% Parse varargin
86for ii=1:2:length(varargin)
87  switch lower(varargin{ii})
88        case 'tr'
89          TR = varargin{ii+1};
90        case 'timeunits'
91          TimeUnits = varargin{ii+1};
92        case 'wbar'
93          if strcmpi(varargin{ii+1},'off')
94                showWbar = false;
95          end
96        otherwise
97          error('Unknown property "%s"',lower(varargin{ii}))
98  end
99end
100vox_size(end+1) = TR;
101
102% Allocate space for the 4D nifti
103DATA.DataFormat = tmp_data.DataFormat;
104DATA.HDR.FileHeader = tmp_data.HDR.FileHeader;
105DATA.FTDATA = zeros(data_sz(1),data_sz(2),data_sz(3),length(infiles),...
106  class(tmp_data.FTDATA));
107DATA.FTDATA(:,:,:,1) = tmp_data.FTDATA;
108
109if showWbar
110  wbh=aedes_wbar(1/length(infiles),...
111        sprintf('Reading 3D NIfTI files...1/%d',length(infiles)));
112end
113for ii=2:length(infiles)
114  % Read the nifti file
115  tmp_data = aedes_read_nifti(infiles{ii});
116  sz(1)=size(tmp_data.FTDATA,1);
117  sz(2)=size(tmp_data.FTDATA,2);
118  sz(3)=size(tmp_data.FTDATA,3);
119  if ~isequal(sz,data_sz)
120        error('Data size doesn''t match in file %s%s',infiles{ii})
121  end
122  DATA.FTDATA(:,:,:,ii) = tmp_data.FTDATA;
123  if showWbar
124        aedes_wbar(ii/length(infiles),wbh,...
125          sprintf('Reading 3D NIfTI files...%d/%d',ii,length(infiles)))
126  end
127end
128if showWbar
129  close(wbh)
130  drawnow
131end
132
133% % High pass filtering
134% [B2,A2]=butter(6,0.02,'high');
135% counter=1;
136% sz=size(DATA);
137% nVox = prod(sz(1:3));
138% fprintf(1,'\nHigh-pass filtering:\n');
139% for ii=1:size(DATA,1)
140%   for kk=1:size(DATA,2)
141%       for zz=1:size(DATA,3)
142%         tmp = squeeze(double(DATA(ii,kk,zz,:)));
143%         DATA(ii,kk,zz,:) = filtfilt(B2,A2,tmp)+mean(tmp);
144%         if counter~=1
145%               fprintf(1,'\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b')
146%         end
147%         fprintf(1,'Filtering: %03d %%',round(100*counter/nVox))
148%         counter=counter+1;
149%       end
150%   end
151% end
152% fprintf(1,'\n');
153
154% Write the resulting 4D NIfTI file
155if showWbar
156  cwh = aedes_calc_wait(sprintf('Writing 4D NIfTI file:\n%s',outfile));
157end
158aedes_write_nifti(DATA,outfile,...
159  'VoxelSize',vox_size,...
160  'XYZUnits',xyzunits,...
161  'TimeUnits',TimeUnits,...
162  'Description',description,...
163  'machine',byteorder);
164if showWbar
165  delete(cwh)
166end
167
168
169
Note: See TracBrowser for help on using the repository browser.

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