將一組png圖片轉爲nii.gz

主要之前使用matlab 對numpy數組存放方式不是很瞭解.應該是[z,x,y]這樣在itksnamp上看就對了

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

import matplotlib.pyplot as plt # plt 用於顯示圖片


def save_array_as_nii_volume(data, filename, reference_name = None):
    """
    save a numpy array as nifty image
    inputs:
        data: a numpy array with shape [Depth, Height, Width]
        filename: the ouput file name
        reference_name: file name of the reference image of which affine and header are used
    outputs: 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 = './oriCvLab/testCvlab/img/'
image_arr = glob.glob(str(image_path) + str("/*"))
image_arr.sort()

print(image_arr, len(image_arr))
allImg = []
allImg = np.zeros([165, 768,1024], 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)
    allImg[i, :, :] = img_as_np


# np.transpose(allImg,[2,0,1])
save_array_as_nifty_volume(allImg, './testImg.nii.gz')
print(np.shape(allImg))
img = allImg[:, :, 55]
# plt.imshow(img, cmap='gray')
# plt.show()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章