Hi Alle,
See attached code
Regards,
Michael T Rubens
--
Research Associate
Gazzaley Lab
Department of Neurology
University of California, San Francisco
% convert 3d nifti to 4d nifti
nii_in = dir(fullfile(export_dir,'*.nii'));
if length(nii_in)>1
fprintf('...compressing 3-D NIFTI files into 4-D NIFTI file...\n')
nii_file = {};
nii_out = fullfile(export_dir,sprintf('%s.nii',protocol));
for b=1:length(nii_in)
nii_file{b} = fullfile(export_dir,nii_in(b).name);
end
nii_3Dto4D(nii_file,nii_out);
for b=1:length(nii_file)
delete(nii_file{b});
end
end
%% FUNCTION FOR CREATING 4-D NIFTI FILES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% MODIFIED FROM spm_config_3Dto4D.m (20 june 2007)
%
% Copyright (C) 2005 Wellcome Department of Imaging Neuroscience
%
% John Ashburner
% $Id: spm_config_3Dto4D.m 245 2005-09-27 14:16:41Z guillaume $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function nii_3Dto4D(files,save_name)
V = spm_vol(strvcat(files{:}));
ind = cat(1,V.n);
N = cat(1,V.private);
mx = -Inf;
mn = Inf;
for i=1:numel(V),
dat = V(i).private.dat(:,:,:,ind(i,1),ind(i,2));
dat = dat(finite(dat));
mx = max(mx,max(dat(:)));
mn = min(mn,min(dat(:)));
end
sf = max(mx,-mn)/32767;
ni = nifti;
ni.dat = file_array(sprintf('%s',save_name),[V(1).dim
numel(V)],'INT16-BE',0,sf,0);
ni.mat = N(1).mat;
ni.mat0 = N(1).mat;
ni.descrip = '4D image';
create(ni);
for i=1:size(ni.dat,4),
ni.dat(:,:,:,i) = N(i).dat(:,:,:,ind(i,1),ind(i,2));
spm_get_space([ni.dat.fname ',' num2str(i)], V(i).mat);
end
end
On Tue, Apr 8, 2008 at 8:45 AM, Alle Meije Wink <[log in to unmask]> wrote:
> Dear all,
>
> Is it possible, with spm command line functions, to read and write 4D NifTI
> files?
>
> After
> V=spm_vol('fmri_TR2.nii');
> with fmri_TR2.nii a 4D NifTI data set, I get an array V of records about
> the individual volumes in the 4D data set.
>
> The fastest way that I know to read data is spm_read_vols, but due to the
> NifTI record of my file being split into N separate records (???),
> spm_read_vols only load one volume at a time.
>
> For fast reading and writing of 4D data, is there a way to not do this
> splitting of NifTI records, and load/save the data as a 4D array?
>
> Many thanks
> Alle Meije
>
|