具体API函数请参考文档:https://docs.monai.io/en/latest/transforms.html
普通变换又可以说是基于数组的变换:image和label是以数组形式给到Dataset。字典变换是基于字典的变换(image和label是一个字典对)。
普通变换和字典变换的功能是一样的,只是字典变换在每个transform后面都加了一个"d", 也可以写成”D“。如LoadImage/LoadImaged, Resize/Resized
使用字典变换时,必须指明该变换是对image做,还是label做。如,LoadImaged(keys=‘image’),表明只加载image
# -*- coding : UTF-8 -*-
# @file : monai_03.py
# @Time : 2023-08-02 18:17
# @Author : wmz
import os
import glob
import matplotlib.pyplot as plt
from monai.transforms import (
AsDiscreted,
AddChanneld,
Compose,
CropForegroundd,
SpatialPadd,
ResizeWithPadOrCropd,
LoadImaged,
Orientationd,
RandCropByPosNegLabeld,
ScaleIntensityRanged,
KeepLargestConnectedComponentd,
Spacingd,
ToTensord,
RandAffined,
RandFlipd,
RandCropByPosNegLabeld,
RandShiftIntensityd,
RandRotate90d,
EnsureTyped,
Invertd,
KeepLargestConnectedComponentd,
SaveImaged,
Activationsd
)
if __name__ == "__main__":
print("processing")
data_dir = r"E:\BaiduNetdiskDownload\Flare2021"
train_images = sorted(
glob.glob(os.path.join(data_dir, "imagesTr", "*.nii.gz")))
train_labels = sorted(
glob.glob(os.path.join(data_dir, "labelsTr", "*.nii.gz")))
data_dicts = [
{"image": image_name, "label": label_name}
for image_name, label_name in zip(train_images[:4], train_labels[:4])
]
train_data_dicts, val_data_dicts = data_dicts[:2], data_dicts[-2:]
# 1. 加载数据与打印信息
loader = LoadImaged(keys=("image", "label"))
data_dict = loader(train_data_dicts[0])
print(f"image shape: {data_dict['image'].shape}")
print(f"label shape: {data_dict['label'].shape}")
print("max pixel val:{}".format(data_dict["image"].max()))
print("min pixel val:{}".format(data_dict["image"].min()))
print(f"image pixdim:\n{data_dict['image_meta_dict']['pixdim'][2]}")
print(f"image dim:\n{data_dict['image_meta_dict']['dim']}")
print(f"data_dict keys:{data_dict.keys()}")
print(f"data_dict img_meta_dict :{data_dict['image_meta_dict'].keys()}\n")
print(f"data_dict img_meta_dict :{data_dict['image_meta_dict'].values()}\n")
# 2. 显示
image, label = data_dict["image"], data_dict["label"]
plt.figure("visualize", (8, 4))
plt.subplot(1, 2, 1)
plt.title("image")
plt.imshow(image[:, :, 132], cmap="gray")
plt.subplot(1, 2, 2)
plt.title("label")
plt.imshow(label[:, :, 132])
plt.show()
参考:MONAI Transform 分析和使用
参考:MONAI-如何加载图像
官方参考文档
MONAI API 文档
[MONAI(3)—一文看懂各种Transform用法(上)_51CTO博客_transforms用法详解](https://blog.51cto.com/u_16159492/6481601#:~:text=MONAI (3)—一文看懂各种Transform用法(上) 1 1.数据准备 2 2. 加载NIfTI 格式的文件【,%2F ScaleIntensityRanged] 5 5 空间变换 [Rotate90d %2F Resized])
MONAI-如何加载图像_@左左@右右的博客-CSDN博客
MONAI(4)—一文看懂各种Transform用法(下)_cropforegroundd_Tina姐的博客-CSDN博客
tutorials/2d_classification/mednist_tutorial.ipynb at main · Project-MONAI/tutorials · GitHub
2.monai——transform数据处理_randcropbyposneglabeld_Jorko的浪漫宇宙的博客-CSDN博客
一款专门为医学图像定制的框架(MONAI),太好用了!_Tina姐的博客-CSDN博客
使用MONAI深度学习框架进行3D图像空间变换_不入流儿的博客-CSDN博客
医学图像深度学习3D数据增强之MONAI框架方法利用 - 知乎 (zhihu.com)
GitHub - JunMa11/AbdomenCT-1K: The official repository of “AbdomenCT-1K: Is Abdominal Organ Segmentation A Solved Problem?”
monai.tansforms.xxx 常用函数&作用_mri数据扩充randgaussiannoise_Tina姐的博客-CSDN博客