source: misclib/nifti3dto4d.m @ 40

Last change on this file since 40 was 40, checked in by tjniskan, 11 years ago
  • Rewrote the handling of the old SUR and MRI formats

(an2_smiswrite.m,an2_smisread.m). Consider this as "legacy support"...

  • Removed depricated functions write_mri.m, an2_surread.m and

an2_mriread.m

  • Now (hopefully) the name change is complete and all the license

issues are resolved...

A an2_smiswrite.m
M misclib/spectrsum.m
M misclib/shiftind.m
M misclib/nifti4dto3d.m
M misclib/nifti3dto4d.m
M misclib/dcm_info.m
M misclib/addicon.m
M misclib/makelcmraw.m
M an2_revision.m
D write_mri.m
M an2_data_read.m
D an2_surread.m
A an2_smisread.m
M aedes.m
D an2_mriread.m

File size: 4.2 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, (or 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 = 0;
34
35% Parse input arguments --------------------------
36if nargin==0
37  % Prompt files to read
38  [fname,fpath,findex] = an2_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
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 = an2_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!!!)
73description = tmp_data.HDR.FileHeader.hist.descrip;
74ind=strfind(tmp_data.HDR.FileHeader.hist.descrip,'TR=');
75TR = str2num(tmp_data.HDR.FileHeader.hist.descrip(ind+3:ind+6))/1000;
76
77data_sz(1)=size(tmp_data.FTDATA,1);
78data_sz(2)=size(tmp_data.FTDATA,2);
79data_sz(3)=size(tmp_data.FTDATA,3);
80
81% Parse varargin
82for ii=1:2:length(varargin)
83  switch lower(varargin{ii})
84        case 'tr'
85          TR = varargin{ii+1};
86        case 'timeunits'
87          TimeUnits = varargin{ii+1};
88        case 'wbar'
89          if strcmpi(varargin{ii+1},'off')
90                showWbar = false;
91          end
92        otherwise
93          error('Unknown property "%s"',lower(varargin{ii}))
94  end
95end
96vox_size(end+1) = TR;
97
98% Allocate space for the 4D nifti
99DATA.DataFormat = tmp_data.DataFormat;
100DATA.HDR.FileHeader = tmp_data.HDR.FileHeader;
101DATA.FTDATA = zeros(data_sz(1),data_sz(2),data_sz(3),length(infiles),...
102  class(tmp_data.FTDATA));
103DATA.FTDATA(:,:,:,1) = tmp_data.FTDATA;
104
105if showWbar
106  wbh=an2_wbar(1/length(infiles),...
107        sprintf('Reading 3D NIfTI files...1/%d',length(infiles)));
108end
109for ii=2:length(infiles)
110  % Read the nifti file
111  tmp_data = an2_read_nifti(infiles{ii});
112  sz(1)=size(tmp_data.FTDATA,1);
113  sz(2)=size(tmp_data.FTDATA,2);
114  sz(3)=size(tmp_data.FTDATA,3);
115  if ~isequal(sz,data_sz)
116        error('Data size doesn''t match in file %s%s',infiles{ii})
117  end
118  DATA.FTDATA(:,:,:,ii) = tmp_data.FTDATA;
119  if showWbar
120        an2_wbar(ii/length(infiles),wbh,...
121          sprintf('Reading 3D NIfTI files...%d/%d',ii,length(infiles)))
122  end
123end
124if showWbar
125  close(wbh)
126  drawnow
127end
128
129% % High pass filtering
130% [B2,A2]=butter(6,0.02,'high');
131% counter=1;
132% sz=size(DATA);
133% nVox = prod(sz(1:3));
134% fprintf(1,'\nHigh-pass filtering:\n');
135% for ii=1:size(DATA,1)
136%   for kk=1:size(DATA,2)
137%       for zz=1:size(DATA,3)
138%         tmp = squeeze(double(DATA(ii,kk,zz,:)));
139%         DATA(ii,kk,zz,:) = filtfilt(B2,A2,tmp)+mean(tmp);
140%         if counter~=1
141%               fprintf(1,'\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b')
142%         end
143%         fprintf(1,'Filtering: %03d %%',round(100*counter/nVox))
144%         counter=counter+1;
145%       end
146%   end
147% end
148% fprintf(1,'\n');
149
150% Write the resulting 4D NIfTI file
151if showWbar
152  cwh = an2_calc_wait(sprintf('Writing 4D NIfTI file:\n%s',outfile));
153end
154an2_write_nifti(DATA,outfile,...
155  'VoxelSize',vox_size,...
156  'XYZUnits',xyzunits,...
157  'TimeUnits',TimeUnits,...
158  'Description',description,...
159  'machine',byteorder);
160if showWbar
161  delete(cwh)
162end
163
164
165
Note: See TracBrowser for help on using the repository browser.

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