请将我的数据集里面的3D图像变成2D、三通道、png格式的图像(要求调整窗宽和窗位,以突出所需区域的细节)。数据集里面的3D标签变成(要求背景灰度值0、肝脏灰度值1、肝脏肿瘤灰度值2的)2D的png格式标签。 LiTS2017数据集里面包含Training Batch 1和Training Batch 2两个文件夹。Training Batch 1文件夹里面有图像,图像的拓展名为.nii。Training Batch 2文件夹里面有标签,标签拓展名为.nii。 LiTS2017数据集的路径为C:\Users\Administrator\Desktop\LiTS2017。 保存路径为C:\Users\Administrator\Desktop\2D-LiTS2017,请帮我创建好对应文件夹,并且将转换好的数据保存到对应的文件夹。
时间: 2024-02-03 09:14:08 浏览: 26
好的,可以使用Python中的SimpleITK库来完成这个任务。请先确保已经安装了SimpleITK库。
以下是代码实现:
```python
import os
import SimpleITK as sitk
from skimage import exposure # 用于调整窗宽和窗位
import numpy as np
from tqdm import tqdm
# 定义路径
data_path = r'C:\Users\Administrator\Desktop\LiTS2017'
save_path = r'C:\Users\Administrator\Desktop\2D-LiTS2017'
# 创建对应文件夹
if not os.path.exists(save_path):
os.mkdir(save_path)
os.mkdir(os.path.join(save_path, 'image'))
os.mkdir(os.path.join(save_path, 'label'))
# 调整窗宽和窗位的函数
def window_image(image, level=40, width=400):
img_min = level - width // 2
img_max = level + width // 2
windowed = exposure.rescale_intensity(image, in_range=(img_min, img_max))
return windowed
# 读取数据
image_path = os.path.join(data_path, 'Training Batch 1')
label_path = os.path.join(data_path, 'Training Batch 2')
image_files = os.listdir(image_path)
label_files = os.listdir(label_path)
# 遍历所有图像和标签
for i in tqdm(range(len(image_files))):
# 读取图像和标签
image = sitk.ReadImage(os.path.join(image_path, image_files[i]))
label = sitk.ReadImage(os.path.join(label_path, label_files[i]))
# 将图像和标签转换成numpy数组
image_array = sitk.GetArrayFromImage(image)
label_array = sitk.GetArrayFromImage(label)
# 遍历所有切片
for j in range(image_array.shape[0]):
# 读取当前切片的图像和标签
image_slice = image_array[j,:,:]
label_slice = label_array[j,:,:]
# 调整窗宽和窗位,突出所需区域的细节
windowed_image = window_image(image_slice)
# 将标签的灰度值调整为0,1,2
label_slice[label_slice == 1] = 1
label_slice[label_slice == 2] = 2
# 将图像和标签保存为png格式的文件
sitk.WriteImage(sitk.GetImageFromArray(windowed_image), os.path.join(save_path, 'image', f'{i:03d}_{j:03d}.png'))
sitk.WriteImage(sitk.GetImageFromArray(label_slice), os.path.join(save_path, 'label', f'{i:03d}_{j:03d}.png'))
```
上述代码中,我们首先读取LiTS2017数据集中的图像和标签,然后遍历所有切片,将其转换成2D、三通道、png格式的图像和标签,并保存到对应的文件夹中。在转换过程中,我们还使用了skimage库的exposure模块来调整窗宽和窗位,以突出所需区域的细节。
注意:代码运行时间可能较长,请耐心等待。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)