将一组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_nii_volume(allImg, './testImg.nii.gz')
print(np.shape(allImg))
img = allImg[:, :, 55]
# plt.imshow(img, cmap='gray')
# plt.show()
  • 8
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 25
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 25
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值