Nii切片->2D ndarray灰色图->PIL灰色图->PIL RGB彩色图

一、NII切片切成2D ndarray灰色图

NII为三维医学图像数据,但有时候网络需要2D数据,因此需要对其进行切片,这里最后以np.ndarray保存。

读进来的NII像素值很大,有些上千的,而灰色图数值在0-255,所以在转的时候很容易越界丢失数据,因此这里需要进行归一化,

示例代码:

cbct_subset_path = CBCT_Folder + "/" + str(CBCT_list[subsetindex]) + "/" + str(CBCT_list[subsetindex]) + ".nii.gz"
cbct_src = sitk.ReadImage(cbct_subset_path)
cbct_array = sitk.GetArrayViewFromImage(cbct_src)
for n_slice in range(cbct_array.shape[0]):
        cbct_slice = cbct_array[n_slice,:,:]
        cbct_slice = ((cbct_slice - cbct_slice.min()) / (cbct_slice.max() - cbct_slice.min()))*255
        cbct_slice = cbct_slice.astype("uint8")
        savepath = trainA_Folder + "/" + str(CBCT_list[subsetindex]) + "_" + str(n_slice) + ".npy"
        np.save(savepath,cbct_slice)#(512, 512),dtype('uint8')

二、2D ndarray灰色图 -> PIL灰色图

示例代码:

npout = Image.fromarray(npimage)

三、PIL灰色图->PIL RGB彩色图

示例代码:

rgbout = npout.convert('RGB')

 

  • 5
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我明白您的要求。以下是我为您编写的Python代码,可以实现您的要求: ```python import os import numpy as np import SimpleITK as sitk from PIL import Image # 创建文件夹函数 def create_folder(folder): if not os.path.exists(folder): os.makedirs(folder) # 将3D切片成2D、RGB三通道的png格式的像 def slice_image(image_path, label_path, save_path): # 读取像和标签数据 image = sitk.ReadImage(image_path) label = sitk.ReadImage(label_path) image_array = sitk.GetArrayFromImage(image) label_array = sitk.GetArrayFromImage(label) # 切片并保存 for i in range(image_array.shape[0]): # 判断肝脏和肿瘤的面积占比是否大于2% mask = label_array[i] mask_area = np.sum(mask) if mask_area > mask.size * 0.02: # 将像归一化到0-255之间 image_slice = image_array[i] image_slice = (image_slice - np.min(image_slice)) / (np.max(image_slice) - np.min(image_slice)) image_slice = (image_slice * 255).astype(np.uint8) # 将灰度转换为RGB像 image_slice = np.stack((image_slice, image_slice, image_slice), axis=-1) # 保存像和标签 Image.fromarray(image_slice).save(os.path.join(save_path, 'image', 'img_%03d.png' % i)) Image.fromarray(mask).save(os.path.join(save_path, 'label', 'label_%03d.png' % i)) # 设置数据集路径和保存路径 data_folder = 'E:\\LiTS2017' save_folder = 'E:\\2D-LiTS2017' # 创建保存像和标签的文件夹 create_folder(os.path.join(save_folder, 'image')) create_folder(os.path.join(save_folder, 'label')) # 循环处理每个数据批次 for batch_num in range(1, 3): # 获取像和标签的文件路径 image_path = os.path.join(data_folder, 'Training Batch %d' % batch_num, 'volume-%d.nii' % batch_num) label_path = os.path.join(data_folder, 'Training Batch %d' % batch_num, 'segmentation-%d.nii' % batch_num) # 将3D切片成2D像并保存 slice_image(image_path, label_path, save_folder) ``` 运行以上代码后,您将得到E:\2D-LiTS2017文件夹,其中包含image文件夹和label文件夹。image文件夹中包含切片像,label文件夹中包含切片标签。其中,保留了肝脏加上肿瘤在中面积占比大于2%的切片
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玖零猴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值