医学图像的数据预处理

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
    评论
Python在医学图像数据预处理方面也有广泛的应用。通过使用Python的图像处理库,可以对医学图像进行裁剪、缩放、旋转、灰度化等操作,以满足不同算法的输入要求。例如,可以使用PIL库的ImageOps模块进行图像翻转。通过水平翻转、垂直翻转等操作,可以生成新的训练样本,从而提高数据集的大小。下面是一个示例代码: ```python from PIL import ImageOps # 打开图像 img = Image.open("input.jpg") # 水平翻转 img_h_flip = ImageOps.mirror(img) # 垂直翻转 img_v_flip = ImageOps.flip(img) # 显示翻转后的图像 img_h_flip.show() img_v_flip.show() ``` 除了图像翻转,还可以使用其他图像处理技术,如平滑、边缘检测、直方图均衡化等来增强医学图像的质量和信息。这些预处理技术可以帮助去除噪声、增强对比度、提取特征等,从而提高机器学习模型在医学图像分析中的准确性和效果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Python实践:图像预处理数据增强](https://blog.csdn.net/ai52learn/article/details/131255906)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Mario cai

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

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

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

打赏作者

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

抵扣说明:

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

余额充值