我的LiTS2017数据集的路径为E:\2D-LiTS2017liver_tumor。保存路径为E:\2D-LiTS2017color,帮我创建保存图像与标签的文件夹,并且将转换好的数据保存到对应的文件夹。 我的LiTS2017数据集为2D的数据集。数据集里面有命名为train_liver_tumor2的文件夹和命名为liver_tumor2_label的文件夹。train_liver_tumor2文件夹里面有图像,图像的拓展名为.png。liver_tumor2_label文件夹里面有标签,标签拓展名为.png。 请将我的train_liver_tumor2文件夹里面的图像变成RGB三通道的图片,再根据Hu(CT)值,采用windowing方法进行图像增强,并对肿瘤部分进行二值化。 再根据liver_tumor2_label文件夹里面的标签(肝脏值为1,肿瘤为2),将增强后的图像的肝脏部分处理成红色,肿瘤部分处理成蓝色。
时间: 2024-02-22 08:57:02 浏览: 14
好的,我会帮您完成这个任务。
首先,我们需要创建保存图像和标签的文件夹。您可以使用以下代码创建这些文件夹:
```python
import os
data_path = "E:/2D-LiTS2017liver_tumor"
save_path = "E:/2D-LiTS2017color"
if not os.path.exists(save_path):
os.makedirs(save_path)
if not os.path.exists(os.path.join(save_path, "images")):
os.makedirs(os.path.join(save_path, "images"))
if not os.path.exists(os.path.join(save_path, "labels")):
os.makedirs(os.path.join(save_path, "labels"))
if not os.path.exists(os.path.join(save_path, "liver")):
os.makedirs(os.path.join(save_path, "liver"))
if not os.path.exists(os.path.join(save_path, "tumor")):
os.makedirs(os.path.join(save_path, "tumor"))
```
接下来,我们需要使用Python的PIL库将图像转换为RGB三通道的图片,并对其进行windowing方法的图像增强和二值化处理,然后将它们保存到对应的文件夹中。您可以使用以下代码完成这些任务:
```python
import os
import numpy as np
from PIL import Image
data_path = "E:/2D-LiTS2017liver_tumor"
save_path = "E:/2D-LiTS2017color"
for file in os.listdir(os.path.join(data_path, "train_liver_tumor2")):
if file.endswith(".png"):
# Load the image
image_path = os.path.join(data_path, "train_liver_tumor2", file)
image = Image.open(image_path)
# Convert the image to RGB
image = image.convert("RGB")
# Apply windowing method
hu_min = -125
hu_max = 275
image = np.array(image)
image = np.clip(image, hu_min, hu_max)
image = (image - hu_min) / (hu_max - hu_min) * 255
image = Image.fromarray(image.astype("uint8"))
# Apply thresholding
threshold = 128
image = image.convert("L")
image = image.point(lambda x: 255 if x > threshold else 0)
# Save the image
image_name = os.path.splitext(file)[0] + ".jpg"
image_path = os.path.join(save_path, "images", image_name)
image.save(image_path)
# Load the label
label_path = os.path.join(data_path, "liver_tumor2_label", file)
label = Image.open(label_path)
# Create liver mask
liver_mask = label.point(lambda x: 255 if x == 1 else 0)
liver_mask = liver_mask.convert("L")
liver_mask = liver_mask.point(lambda x: 255 if x > threshold else 0)
liver_mask_name = os.path.splitext(file)[0] + ".png"
liver_mask_path = os.path.join(save_path, "liver", liver_mask_name)
liver_mask.save(liver_mask_path)
# Create tumor mask
tumor_mask = label.point(lambda x: 255 if x == 2 else 0)
tumor_mask = tumor_mask.convert("L")
tumor_mask = tumor_mask.point(lambda x: 255 if x > threshold else 0)
tumor_mask_name = os.path.splitext(file)[0] + ".png"
tumor_mask_path = os.path.join(save_path, "tumor", tumor_mask_name)
tumor_mask.save(tumor_mask_path)
# Create color label
color_label = Image.new("RGB", label.size, (0, 0, 0))
color_label.paste((255, 0, 0), liver_mask)
color_label.paste((0, 0, 255), tumor_mask)
color_label_name = os.path.splitext(file)[0] + ".png"
color_label_path = os.path.join(save_path, "labels", color_label_name)
color_label.save(color_label_path)
```
这个代码将处理所有以.png为拓展名的文件,并将它们保存到对应的文件夹中。在保存图像和标签之前,它会将图像转换为RGB三通道的图片,并对其进行windowing方法的图像增强和二值化处理。然后,它会根据肝脏和肿瘤的值将增强后的图像的肝脏部分处理成红色,肿瘤部分处理成蓝色,并将它们保存到对应的文件夹中。