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')
医学图像的数据预处理
最新推荐文章于 2023-11-07 16:16:13 发布