医学图像的数据预处理

import numpy as np
import torchio as tio
from torch.utils.data import DataLoader
import torch
import nibabel as nib
from torchio.transforms import (
    RandomFlip,
    RandomAffine,
    RandomElasticDeformation,
    RandomNoise,
    RandomMotion,
    RandomBiasField,
    RescaleIntensity,
    Resample,
    ToCanonical,
    ZNormalization,
    CropOrPad,
RandomSpike,
RandomBlur,
RandomSwap,
    HistogramStandardization,
    OneOf,
    Clamp,
    Compose,
    RandomGhosting,
)

landmarks = r'C:\Users\Admin\SIAT\model\caihaihua2d\nrrddatatotal.npy'
z=0

while z<99999:
    z = z+1

    subject = tio.Subject(
        chest_ct=tio.ScalarImage(fr'C:\Users\Admin\Desktop\EGFR total\EGFRzhong\EGFRzhong\data\1\{z}.nrrd'),
        # chest_ct=tio.ScalarImage(fr'C:\Users\Admin\PycharmProjects\pythonProject14\{z}.nii.gz'),
        # chest_ct=tio.ScalarImage(fr'C:\Users\Admin\Desktop\EGFR total\EGFRzhong\new\EGFRdata\0data\00C1108863_HU  JIAN  TING.nrrd'),

        heart_mask=tio.LabelMap(fr'C:\Users\Admin\Desktop\EGFR total\EGFRzhong\EGFRzhong\mask\1\{z}.nrrd'),
        # heart_mask1=tio.LabelMap(fr'C:\Users\Admin\Desktop\EGFR total\EGFRzhong\EGFRzhong\mask\1\{z}.nrrd'),
        # heart_mask=tio.LabelMap(fr'C:\Users\Admin\Desktop\EGFR total\EGFRzhong\new\EGFRmask\0mask\00C1108863_HU  JIAN  TING.seg.nrrd'),

    )


    # transform = tio.HistogramStandardization({'chest_ct': landmarks}),


    subject.chest_ct.shape
    # subject.heart_mask.shape = subject.chest_ct.shape
    # subject.heart_mask.shape
    #
    # print(subject.heart_mask)
    #
    # print(subject.chest_ct)

    # transform = tio.CropOrPad(
    #     # (64, 64, 32),
    #     mask_name='heart_mask',
    # )
    # ti = tio.Resample(2)
    # subject = ti(subject)


    transform = Compose([
        tio.ToCanonical(),
        # tio.Resample(),

        # tio.Resample(include=['chest_ct']),
        # tio.Resample(target='chest_ct', exclude=['chest_ct']),

        tio.HistogramStandardization({'chest_ct': landmarks}),
        tio.Mask(masking_method='heart_mask'),
        tio.CropOrPad(
            # (64, 64, 64),
            mask_name='heart_mask',),
        # tio.HistogramStandardization({'chest_ct': landmarks}),

    ])


    transformed = transform(subject)
    transformed.chest_ct.shape
    # print(k)
    # transformed.heart_mask.shape
    # print(l)

    a = transformed.get_first_image()
    a = a.numpy()
    x = a[0]
    # a = a.reshape([64,64,64,1]) #将之转化为数组查看
    # x = a.type(torch.FloatTensor).cuda()
    print(a[0].shape)
    img_t1 = nib.Nifti1Image(x, np.eye(4))
    nib.save(img_t1, f'{z}.nii.gz')  # 将之保存为nii查看
    # sitk.WriteImage(dcms_series, filepath + subName + '.nrrd')

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Mario cai

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

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

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

打赏作者

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

抵扣说明:

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

余额充值