Scripts to convert hdr/img format into nii format

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.

%%img2nii.m--------------------------------------------
%Script to convert hdr/img files to nii.
%This script uses SPM function, so you need to install SPM5 or later.
%Kiyotaka Nemoto 05-Nov-2014

%select files
f = spm_select(Inf,'img$','Select img files to be converted');

%convert img files to nii
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.

#!/bin/sh

#Image file type converter using fslchfiletype
#This script converts various image files into NIFTI format (.nii) files.
#K. Nemoto 19 Jan 2013

if [ $# -lt 1 ] ; then
	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).