Nifti file havs two kinds of extensions, hdr/img or nii.
Sometimes hdr/file nifti files can be confusing because the extension is the same as ANALYZE format.
There are some ways to convert hdr/img into nifti.
One is to use fslchfiletype included in FSL. This is the easiest, but it requires FSL.
The other is to use SPM. However, some scripting is needed.
So I wrote tiny scripts for converting hdr/img format into nii format.
If you prefer SPM, use img2nii_spm.m. Save the script into a folder under MATLAB path and type “img2nii_spm” from MATLAB command window.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 | f = spm_select(Inf, 'img$' , 'Select img files to be converted' );
for i=1:size(f,1)
input = deblank(f(i,:));
[pathstr,fname,ext] = fileparts(input);
output = strcat(fname, '.nii' );
V=spm_vol(input);
ima=spm_read_vols(V);
V.fname=output;
spm_write_vol(V,ima);
end
|
If you prefer FSL, use img2nii_fsl.sh. Save the script into a folder, add executable attribute using “chmod 755 img2nii_fsl.sh”.
Wild card can be used for selecting files.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 | #!/bin/sh
if [ $
echo "Please specify the files you want to convert!"
echo "Usage: $0 filename"
exit 1
fi
for file in "$@" ; do
if [ -f $ file ] ; then
fslchfiletype NIFTI $ file
else
echo "$file: No such file"
fi
done
|
Download img2nii_spm.m (Right-click and save).
Download img2nii_fsl.sh (Right-click and save).