Convert .png Files To .nii (nifti Files)
I'm currently working in a project which handles .nii files from neuro images. I converted that file into 80 .png files. Now I need to combine those 80 .png into .nii files again.
Solution 1:
The strict answer is no, you can't do it. Because png files do not contain those information needed for NIfTI file.
However if you don't care whether the coordinate and left-right information is correct or not, you could generate a fake nii file. You can read your png files (I suppose they have the same dimension) using a for
loop:
fori=1:numberOfPNG_file
img(:,:,i) = imread(png_Files{i});
end
The you can use the Matlab NIfTI tool to create nii file:
nii = nii_tool('init', img);
nii_tool('save', nii, 'my_nii.nii');
Solution 2:
Hope this helps
%step 1: get the names of the files
files=dir('*.png');
file_names={files.name}';
%step 2: sort the files
%extract the numbers
%Here, the format of the name shoul be enterd and %d should replate the
%number, this is so that the files will be load in the right order
filenum = cellfun(@(x)sscanf(x,'%d.png'), file_names);
% sort them, and get the sorting order
[~,Sidx] = sort(filenum) ;
% use to this sorting order to sort the filenames
SortedFilenames = file_names(Sidx);
%step 3: combine images to single matrix:
%get number of files
num_of_files=numel(SortedFilenames);
for i=1:num_of_files
nifti_mat(:,:,i)=imread(SortedFilenames{i});
end
%step 4: conver to nifti and save:
filename='here_goes_the_name_of_the_file';
niftiwrite(nifti_mat,filename);
Post a Comment for "Convert .png Files To .nii (nifti Files)"