source: misclib/nifti3dto4d.m @ 37

Last change on this file since 37 was 37, checked in by tjniskan, 11 years ago
  • Renamed analyze2.m to aedes.m
  • Changed all references to "Analyze2" -> "Aedes" (hopefully I didn't

break anything...)

  • Added some nifti-tools to misclib
  • Fixed bug in an2_readfid.m that occurred when reading EPI-data

(thanks Teemu)

M an2_export_gui.m
D analyze2.m
M an2_getfilefilter.m
M an2_calc_wait.m
A misclib/nifti4dto3d.m
A misclib/nifti3dto4d.m
A misclib/filter_fmri_data.m
M misclib/dcm_info.m
M an2_cellwrite.m
M an2_revision.m
M startup.m
M an2_readprocpar.m
M an2_fitmaps.m
M an2_read_nifti.m
M an2_data_read.m
D about_analyze2.png
M an2_resviewer.m
M an2_maptool.m
A aedes.m
M an2_helpabout.m
M plugins/save_roi_as_mask.m
M plugins/calculate_t2_map.m
M an2_inputdlg.m
M an2_readctdata.m
M an2_readfid.m
M an2_readfidprefs.m
M an2_readtab.m
M an2_check_updates.m
A about_aedes.png
M an2_saveres.m
M an2_editstack.m
M an2_errordump.m
M an2_update.m
M an2_write_nifti.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 Analyze 2.0 - 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, (or Department of Neurobiology)
21% University of Kuopio,
22% P.O. Box 1627,
23% FIN-70211, Kuopio
24% FINLAND
25%
26% This program may be used under the terms of the GNU General Public
27% License version 2.0 as published by the Free Software Foundation
28% and appearing in the file LICENSE.TXT included in the packaging of
29% this program.
30%
31% This program is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
32% WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
33
34% Defaults ---------------------------------------
35showWbar = true;
36TR = 0;
37
38% Parse input arguments --------------------------
39if nargin==0
40  % Prompt files to read
41  [fname,fpath,findex] = an2_juigetfiles({'*.nii','NIfTI files (*.nii)';...
42        '*.*','All Files (*.*)'},'Select volumes',pwd);
43 
44  if isequal(fname,0)
45        % Canceled
46        return
47  end
48  tmp={fpath{:};fname{:}};
49  infiles = cell(1,size(tmp,2));
50  for ii=1:size(tmp,2)
51        infiles{ii} = [tmp{:,ii}];
52  end
53end
54
55if nargin<2
56  % Select output file
57  [fn,fp] = uiputfile({'*.nii','NIfTI files (*.nii)';...
58        '*.*','All Files (*.*)'},'Select output file',...
59        '4D_nifti.nii');
60  if isequal(fn,0)
61        % Canceled
62        return
63  end
64  outfile = fullfile(fp,fn);
65end
66
67% Read the first image and get header parameters
68tmp_data = an2_read_nifti(infiles{1});
69TimeUnits = tmp_data.HDR.timeunits;
70xyzunits = tmp_data.HDR.xyzunits;
71byteorder = tmp_data.HDR.byteorder;
72vox_size = tmp_data.HDR.FileHeader.dime.pixdim(2:4);
73
74% Try to determine the TR value from the description field (SPM writes this
75% information here when converting DICOMs, NOTE: THIS MAY FAIL!!!)
76description = tmp_data.HDR.FileHeader.hist.descrip;
77ind=strfind(tmp_data.HDR.FileHeader.hist.descrip,'TR=');
78TR = str2num(tmp_data.HDR.FileHeader.hist.descrip(ind+3:ind+6))/1000;
79
80data_sz(1)=size(tmp_data.FTDATA,1);
81data_sz(2)=size(tmp_data.FTDATA,2);
82data_sz(3)=size(tmp_data.FTDATA,3);
83
84% Parse varargin
85for ii=1:2:length(varargin)
86  switch lower(varargin{ii})
87        case 'tr'
88          TR = varargin{ii+1};
89        case 'timeunits'
90          TimeUnits = varargin{ii+1};
91        case 'wbar'
92          if strcmpi(varargin{ii+1},'off')
93                showWbar = false;
94          end
95        otherwise
96          error('Unknown property "%s"',lower(varargin{ii}))
97  end
98end
99vox_size(end+1) = TR;
100
101% Allocate space for the 4D nifti
102DATA.DataFormat = tmp_data.DataFormat;
103DATA.HDR.FileHeader = tmp_data.HDR.FileHeader;
104DATA.FTDATA = zeros(data_sz(1),data_sz(2),data_sz(3),length(infiles),...
105  class(tmp_data.FTDATA));
106DATA.FTDATA(:,:,:,1) = tmp_data.FTDATA;
107
108if showWbar
109  wbh=an2_wbar(1/length(infiles),...
110        sprintf('Reading 3D NIfTI files...1/%d',length(infiles)));
111end
112for ii=2:length(infiles)
113  % Read the nifti file
114  tmp_data = an2_read_nifti(infiles{ii});
115  sz(1)=size(tmp_data.FTDATA,1);
116  sz(2)=size(tmp_data.FTDATA,2);
117  sz(3)=size(tmp_data.FTDATA,3);
118  if ~isequal(sz,data_sz)
119        error('Data size doesn''t match in file %s%s',infiles{ii})
120  end
121  DATA.FTDATA(:,:,:,ii) = tmp_data.FTDATA;
122  if showWbar
123        an2_wbar(ii/length(infiles),wbh,...
124          sprintf('Reading 3D NIfTI files...%d/%d',ii,length(infiles)))
125  end
126end
127if showWbar
128  close(wbh)
129  drawnow
130end
131
132% % High pass filtering
133% [B2,A2]=butter(6,0.02,'high');
134% counter=1;
135% sz=size(DATA);
136% nVox = prod(sz(1:3));
137% fprintf(1,'\nHigh-pass filtering:\n');
138% for ii=1:size(DATA,1)
139%   for kk=1:size(DATA,2)
140%       for zz=1:size(DATA,3)
141%         tmp = squeeze(double(DATA(ii,kk,zz,:)));
142%         DATA(ii,kk,zz,:) = filtfilt(B2,A2,tmp)+mean(tmp);
143%         if counter~=1
144%               fprintf(1,'\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b')
145%         end
146%         fprintf(1,'Filtering: %03d %%',round(100*counter/nVox))
147%         counter=counter+1;
148%       end
149%   end
150% end
151% fprintf(1,'\n');
152
153% Write the resulting 4D NIfTI file
154if showWbar
155  cwh = an2_calc_wait(sprintf('Writing 4D NIfTI file:\n%s',outfile));
156end
157an2_write_nifti(DATA,outfile,...
158  'VoxelSize',vox_size,...
159  'XYZUnits',xyzunits,...
160  'TimeUnits',TimeUnits,...
161  'Description',description,...
162  'machine',byteorder);
163if showWbar
164  delete(cwh)
165end
166
167
168
Note: See TracBrowser for help on using the repository browser.

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