【Python】SimpleITK 针对于 LiTS 数据集,获取最大肝脏面积的切片

效果图
在这里插入图片描述

1. window_transform

可以让图片更好看一点

def window_transform(ct_array, windowWidth=400, windowCenter=40, normal=False):
    """
    return: trucated image according to window center and window width
    and normalized to [0,1]
    """
    minWindow = float(windowCenter) - 0.5 * float(windowWidth)
    newing = (ct_array - minWindow) / float(windowWidth)
    newing[newing < 0] = 0
    newing[newing > 1] = 1
    # 将值域转到0-255之间,例如要看头颅时, 我们只需将头颅的值域转换到 0-255 就行了
    if not normal:
        newing = (newing * 255).astype('uint8')
    return newing

2. extract_max_slice

获取最大肝脏面积的CT切片

def extract_max_slice(ct_path, seg_path, img_save_path=None):
    """
    根据标注获取最大肝脏切片
    """
    # 读取标注的肝脏图像
    seg_nii = sitk.ReadImage(seg_path)
    seg_img = sitk.GetArrayFromImage(seg_nii)

    # 获取肝脏图像面积最大的切片的索引
    index = np.argmax(seg_img.sum((1, 2)))

    # 读取CT图像
    ct_nii = sitk.ReadImage(ct_path)
    ct_img = sitk.GetArrayFromImage(ct_nii)

    # 截取CT图像中肝脏最大的切片
    img = ct_img[index, :, :]
    img = window_transform(img)

    # 保存图片
    if img_save_path is not None:
        save_name = os.path.basename(ct_path).split(".")[0] + "-index" + str(index) + ".png"
        cv2.imwrite(os.path.join(img_save_path, save_name), img)

效果图

请添加图片描述

3. extract_max_slice_with_seg

获取最大肝脏面积的CT切片,并且用红色标记处肝脏的位置

def extract_max_slice_with_seg(ct_path, seg_path, img_save_path=None):
    """
    根据标注获取带有标注的最大肝脏切片
    """
    # 读取标注的肝脏图像
    seg_nii = sitk.ReadImage(seg_path, sitk.sitkFloat32)
    seg_img = sitk.GetArrayFromImage(seg_nii)

    # 获取肝脏图像面积最大的切片的索引
    index = np.argmax(seg_img.sum((1, 2)))

    # 读取CT图像
    ct_nii = sitk.ReadImage(ct_path)
    ct_img = sitk.GetArrayFromImage(ct_nii)

    # 截取CT图像中肝脏最大的切片
    ext_ct_img = ct_img[index, :, :]
    ext_seg_img = seg_img[index, :, :]

    ext_ct_img = window_transform(ext_ct_img)  # 映射后图片效果更好
    ext_ct_img = np.array(ext_ct_img)

    ext_ct_img = cv2.cvtColor(np.array(ext_ct_img), cv2.COLOR_GRAY2RGB)

    # 肝脏位置标记颜色
    for i in range(512):
        for j in range(512):
            if ext_seg_img[i][j] >= 1.0:
                ext_ct_img[i][j][0] = 97
                ext_ct_img[i][j][1] = 100
                ext_ct_img[i][j][2] = 255

    # 保存图片
    if img_save_path is not None:
        save_name = os.path.basename(ct_path).split(".")[0] + "-index" + str(index) + ".png"
        cv2.imwrite(os.path.join(img_save_path, save_name), ext_ct_img)

效果图

请添加图片描述

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下是将LiTS2017数据集处理为2D png格式,并将标签转换为灰度图的完整代码。需要注意的是,本代码中使用的是Python3.6及以上版本。 ```python import os import numpy as np import SimpleITK as sitk from PIL import Image # 数据集路径 data_path = '/path/to/LiTS2017' # 保存路径 save_path = '/path/to/save' # 处理函数 def process_data(file_path, save_path): # 读取nii文件 itk_img = sitk.ReadImage(file_path) img_array = sitk.GetArrayFromImage(itk_img) # 获取每个切片的像素大小、间距和方向 spacing = itk_img.GetSpacing() origin = itk_img.GetOrigin() direction = itk_img.GetDirection() # 将像素值缩放到0-255之间 img_array = (img_array / np.max(img_array)) * 255 img_array = img_array.astype(np.uint8) # 将标签转换为灰度图 label_array = np.zeros_like(img_array) label_array[img_array == 0] = 0 # 背景 label_array[img_array == 1] = 1 # 肝脏 label_array[img_array == 2] = 2 # 肝脏肿瘤 # 保存2D png格式的图像和标签 for i in range(img_array.shape[0]): img = Image.fromarray(img_array[i]) img.save(os.path.join(save_path, f'{i+1:03d}.png')) label = Image.fromarray(label_array[i]) label.save(os.path.join(save_path, f'label_{i+1:03d}.png')) print(f'{file_path} processed successfully.') # 遍历数据集并处理数据 for file_name in os.listdir(data_path): if file_name.endswith('.nii'): file_path = os.path.join(data_path, file_name) save_folder = os.path.join(save_path, file_name.split('.')[0]) if not os.path.exists(save_folder): os.makedirs(save_folder) process_data(file_path, save_folder) ``` 在运行代码前需要先安装SimpleITK和Pillow库,可使用以下命令进行安装: ``` pip install SimpleITK Pillow ``` 执行完毕后,数据集将被处理成2D的png格式,并且标签将被转换为灰度图,保存在指定的路径下。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

望天边星宿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值