转换.png文件为.nii(NiFti文件)

2024-06-11 15:25:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在一个项目中工作,该项目处理来自neuro images.nii文件。我把那个文件转换成80个.png个文件。现在我需要再次将这些80.png合并到.nii文件中。在

请帮忙。在

谢谢。在


Tags: 文件项目pngneuroimagesnii
2条回答

希望这有帮助

    %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);

严格的回答是不,你做不到。因为png文件不包含NIfTI文件所需的信息。在

但是,如果不关心坐标和左右信息是否正确,则可能生成一个假的nii文件。您可以使用for循环读取png文件(我想它们具有相同的维度):

for i = 1:numberOfPNG_file
    img(:,:,i) = imread(png_Files{i});
end

可以使用MatlabNIfTI tool创建nii文件:

^{pr2}$

相关问题 更多 >