将numpy数组保存成png标签

# 将图像一张一张抽出来保存成png标签

import numpy as np
import SimpleITK as sitk
import matplotlib.pyplot as plt
import os
from PIL import Image
from tqdm import tqdm


if __name__ == '__main__':
    image_path = "D:/test/CASE01_Segmentation.nii"
    filename = os.path.basename(image_path).split('.')[0]
    filename = filename.replace("_Segmentation","")
    print(filename) # CASE01

    image = sitk.ReadImage(image_path)
    img = sitk.GetArrayFromImage(image)
    print(img.shape)
    print(np.unique(img))
    # a = input("暂停")

    for d in tqdm(range(270,img.shape[0])):
        image = Image.fromarray(img[d]).convert('P')
        image.save(f'case01_label/{filename}_{d:04}.png')

将numpy数组保存成png标签_第1张图片

通过图片查看器看它是全黑的,但是用matplotlib画出来就可以看到了

将numpy数组保存成png标签_第2张图片

 

 

你可能感兴趣的:(图像分割,python,计算机视觉,深度学习)