【问题标题】:how to convert a nift folder to png images?如何将 nift 文件夹转换为 png 图像?
【发布时间】:2021-06-11 18:58:54
【问题描述】:

*

有一个从 NumPy 和 imageio 导入的最知名的库 将 NumPy 导入为 np 导入操作系统 将 nibabel 导入为 nib 导入图像 // 方法我有我编写代码将 nift 转换为 png
方法 将 nift(.nii) 图像转换为 png 图像 def nii_to_image(niifile): filenames = os.listdir(filepath) #读取 nii 文件夹 slice_trans = [] #filename是nii图片的路径

  for f in filenames:
                          #Start reading nii files
                   img_path = os.path.join(filepath, f)
                   img = nib.load(img_path) #read nii
                   img_fdata = img.get_fdata()
                   fname = f.replace('.nii','') 
# Remove the nickname of nii
                   img_f_path = os.path.join(imgfile, fname)
                          #Create a folder corresponding to the image of nii
                   if not os.path.exists(img_f_path):
                     os.mkdir(img_f_path) #New folder
          
                          #  to image
                   (x,y,z) = img.shape
                   for i in range(z): #x is the sequence of images
                     silce = img_fdata[i, :, :] #Select which direction the slice can be
                     imageio.imwrite(os.path.join(img_f_path,'{}.png'.format(i)), silce) #Save image
     #main function where fill path was gived
   

主要

 if __name__ == '__main__':
             filepath = '/content/drive/MyDrive/sem 8/dataset/pr' 
             imgfile = '/content/drive/MyDrive/sem 8/dataset/propi'
             nii_to_image(filepath)

【问题讨论】:

    标签: python python-imageio nibabel


    【解决方案1】:

    将 nifti 文件加载为 NumPy 数组后,在每个切片(来自 img.shape 的 z)上运行,然后将数组保存到 png。

    确保在每个切片上运行时只保存现有切片(z_slice_number):

        slice = img_fdata[:, :, z_slice_numer]
    

    要保存此切片,您可以执行以下操作(或here 的其他方式):

        matplotlib.image.imsave('name.png', slice)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-09
      • 2022-11-10
      • 1970-01-01
      • 2018-01-25
      • 1970-01-01
      相关资源
      最近更新 更多