当前位置:网站首页>Png to NII

Png to NII

2022-06-25 20:07:00 Orange cedar

import SimpleITK as sitk
import glob
import numpy as np
from PIL import Image
import cv2


def save_array_as_nii_volume(data, filename, reference_name=None):

    img = sitk.GetImageFromArray(data)
    if (reference_name is not None):
        img_ref = sitk.ReadImage(reference_name)
        img.CopyInformation(img_ref)
    sitk.WriteImage(img, filename)


image_path = './1/DICOM_anon/Ground/Liver'
image_arr = glob.glob(str(image_path) + str("/*"))
image_arr.sort()

print(image_arr)
# print(image_arr[0].shape)
print(len(image_arr))
allImg = []
allImg = np.zeros([len(image_arr), 256, 256], dtype='uint8')
for i in range(len(image_arr)):
    single_image_name = image_arr[i]
    img_as_img = Image.open(single_image_name)
    # img_as_img.show()
    img_as_np = np.asarray(img_as_img)
    img_as_np.squeeze(2)
    allImg[i, :, :] = img_as_np

# np.transpose(allImg,[2,0,1])
save_array_as_nii_volume(allImg, './S1/1.nii')
print(np.shape(allImg))
img = allImg[:, :, 55]

原网站

版权声明
本文为[Orange cedar]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202190507547227.html