2024年最全医学图像处理(一)使用U-Net进行MRI的肝脏分割_chaos数据集(1)

本文介绍了使用U-Net网络进行医学图像处理,特别是针对MRI的肝脏分割。首先,详细展示了如何从chaos数据集中分离肝脏,并将其划分为训练和测试集。接着,讨论了 dicom文件转为png格式的过程以及数据增强的方法。最后,文章涵盖了U-Net网络的搭建,自定义Dataset的实现,以及训练、预测和评估Dice系数的主要步骤。
摘要由CSDN通过智能技术生成

下面是分离数据集的代码:

"""
@ Date: 2020/6/29
@ Author: Xiao Zhuo
@ Brief: Split CHAOS DataSet into my directory
@ Filename: split\_dataset\_1.py
"""
# -\*- coding: utf-8 -\*-
import os
import shutil
import random

dst_TrainData = "./data/train/Data"
dst_TrainGround = "./data/train/Ground"
dst_TestData = "./data/val/Data"
dst_TestGround = "./data/val/Ground"


def collect\_T1\_name(patient_dir):
    ground_paths = list()
    inphase_paths = list()

    t1_datadir = os.path.join(patient_dir, "T1DUAL")
    ground_dir = os.path.join(t1_datadir, "Ground")
    ground_names = os.listdir(ground_dir)
    nums_ground = len(ground_names)
    # 拼接Ground文件夹的文件,存入到ground\_paths列表中
    for i in range(nums_ground):
        ground_paths.append(os.path.join(ground_dir, ground_names[i]))

    inphase_dir = os.path.join(t1_datadir, "DICOM\_anon", "InPhase")
    inphase_names = os.listdir(inphase_dir)
    nums_inphase = len(inphase_names)

    # 拼接inphase文件夹的文件,存入到inphase\_paths列表中
    for i in range(nums_inphase):
        inphase_paths.append(os.path.join(inphase_dir, inphase_names[i]))

    return ground_paths, inphase_paths


if __name__ == '\_\_main\_\_':
    dataset_dir = os.path.join("CHAOS\_Train\_Sets", "Train\_Sets", "MR")
    train_pct = 0.8
    test_pct = 0.2

    for root, dirs, files in os.walk(dataset_dir):
        random.shuffle(dirs)
        dir_count = len(dirs)
        train_point = int(dir_count \* train_pct)
        i = 0
        for sub_dir in dirs:  # sub\_dir代表病人编号
            if i < train_point:
                patient_dir = os.path.join(root, sub_dir)
                ground_paths, inphase_paths = collect_T1_name(patient_dir)
                for num in range(len(ground_paths)):
                    dst_groundpath = os.path.join(dst_TrainGround, "T1\_Patient%s\_No%d.png" % (sub_dir, num))
                    shutil.copy(ground_paths[num], dst_groundpath)

                ## 下面待修改
                for num in range(len(inphase_paths)):
                    dst_inphasepath = os.path.join(dst_TrainData, "T1\_Patient%s\_No%d.dcm" % (sub_dir, num))
                    shutil.copy(inphase_paths[num], dst_inphasepath)

                i += 1
            else:
                patient_dir = os.path.join(root, sub_dir)
                ground_paths, inphase_paths = collect_T1_name(patient_dir)
                for num in range(len(ground_paths)):
                    dst_groundpath = os.path.join(dst_TestGround, "T1\_Patient%s\_No%d.png" % (sub_dir, num))
                    shutil.copy(ground_paths[num], dst_groundpath)

                for num in range(len(inphase_paths)):
                    dst_inphasepath = os.path.join(dst_TestData, "T1\_Patient%s\_No%d.dcm" % (sub_dir, num))
                    shutil.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值