批量nii文件转换成png格式(用于处理胰腺数据)

这篇博客代码在胰腺数据中可以用,但是用于ADNI数据保存的图像都是噪声,不能用。

下面的链接是用于ADNI数据集的代码,个人觉得还是下面链接中的代码稍微好用些,因为不会报错:

https://blog.csdn.net/weixin_43330946/article/details/89576759


from PIL import Image
import numpy as np
import os
import nibabel as nib
import imageio

def read_and_save_niifile(niifile):
    filenames = os.listdir(filepath)
    img_k_list = []

    for f in filenames:
        img_path = os.path.join(filepath, f)
        img = nib.load(img_path)
        img_fdata = img.get_fdata() #(512, 512, 110)
        fname = f.replace('.nii', '')

        img_k_path = os.path.join(imgfile,fname)
        if not os.path.exists(img_k_path):
            os.mkdir(img_k_path)

        for i in range(img.shape[2]):
            fn = fname+'_'+str(i).zfill(3)+'.png'
            img_ks_path = os.path.join(img_k_path,fn)

            img_ks = img[:,:,i]

            img_ks_trans = np.uint8(img_ks)#max value is 2, need to *100
            image_k_save = imageio.imwrite(img_ks_path,img_ks_trans)

    return image_k_save



if __name__ == '__main__':

    filepath = 'nii文件夹的路径'
    imgfile = 'Image文件夹的路径'

    read_and_save_niifile(filepath)

 

你可能感兴趣的:(nii文件处理)