questions
questions > RE: dcm2niix and SPM12
Feb 5, 2019 01:02 PM | Chris Rorden
RE: dcm2niix and SPM12
Hello-
dcm2niix will save your 4D time series as a single file. While most processing pipelines expect 4D files, SPM12 can use either 3D or 4D files. With SPM12's graphical interface you can select to open the volumes of a 4D byt typing "1:9999" into the edit field (see image). With scripts you specify the volume number (e.g. 3) as follows: "img.nii,3". Personally, I prefer using 4D files as they cause less clutter than 3D and allow me to use other tools that expect 4D images. If you really want to convert a 4D file into 3D files you can use the tool 'fslsplit' or a simple Matlab script (see below);
function nii_4dto3d(fnm)
%convert a 4D NIfTI image to3D volumes
% fnm : (optional) filename of 4D image to segment
%Example
% nii_segment4d('img.nii');
if ~exist('fnm','var') %no files specified
fnm = spm_select(1,'image','Select 4D volume');
end
[pth,nam,ext] = spm_fileparts(fnm);
image = fullfile(pth,[nam,ext]); %'img.nii,1' -> 'img.nii'
hdr = spm_vol(image);
img = spm_read_vols(hdr);
nvol = numel(hdr);
hdr = hdr(1);
for vol=1: nvol
hdr.fname = fullfile(pth, [nam, '_', num2str(vol,'%04d'), ext]);
spm_write_vol(hdr,img(:, :, :, vol));
end
%end nii_4dto3d()
dcm2niix will save your 4D time series as a single file. While most processing pipelines expect 4D files, SPM12 can use either 3D or 4D files. With SPM12's graphical interface you can select to open the volumes of a 4D byt typing "1:9999" into the edit field (see image). With scripts you specify the volume number (e.g. 3) as follows: "img.nii,3". Personally, I prefer using 4D files as they cause less clutter than 3D and allow me to use other tools that expect 4D images. If you really want to convert a 4D file into 3D files you can use the tool 'fslsplit' or a simple Matlab script (see below);
function nii_4dto3d(fnm)
%convert a 4D NIfTI image to3D volumes
% fnm : (optional) filename of 4D image to segment
%Example
% nii_segment4d('img.nii');
if ~exist('fnm','var') %no files specified
fnm = spm_select(1,'image','Select 4D volume');
end
[pth,nam,ext] = spm_fileparts(fnm);
image = fullfile(pth,[nam,ext]); %'img.nii,1' -> 'img.nii'
hdr = spm_vol(image);
img = spm_read_vols(hdr);
nvol = numel(hdr);
hdr = hdr(1);
for vol=1: nvol
hdr.fname = fullfile(pth, [nam, '_', num2str(vol,'%04d'), ext]);
spm_write_vol(hdr,img(:, :, :, vol));
end
%end nii_4dto3d()
Threaded View
Title | Author | Date |
---|---|---|
cchassain | Feb 5, 2019 | |
Chris Rorden | Feb 5, 2019 | |
cchassain | Feb 5, 2019 | |