此代码import os import numpy as np from PIL import Image def process_image(image_path, save_path): # 读取nii文件 image_array = np.load(image_path).astype(np.float32) # 归一化到0-255之间 image_array = (image_array - np.min(image_array)) / (np.max(image_array) - np.min(image_array)) * 255 # 将数据类型转换为uint8 image_array = image_array.astype(np.uint8) # 将三维图像分成若干个二维图像 for i in range(image_array.shape[0]): image = Image.fromarray(image_array[i]) image.save(os.path.join(save_path, f"{i}.png")) def process_label(label_path, save_path): # 读取nii文件 label_array = np.load(label_path).astype(np.uint8) # 将标签转换为灰度图 label_array[label_array == 1] = 255 label_array[label_array == 2] = 128 # 将三维标签分成若干个二维标签 for i in range(label_array.shape[0]): label = Image.fromarray(label_array[i]) label.save(os.path.join(save_path, f"{i}.png")) # LiTS2017数据集路径 data_path = "C:\\Users\\Administrator\\Desktop\\LiTS2017" # 保存路径 save_path = "C:\\Users\\Administrator\\Desktop\\2D-LiTS2017" # 创建保存路径 os.makedirs(save_path, exist_ok=True) os.makedirs(os.path.join(save_path, "image"), exist_ok=True) os.makedirs(os.path.join(save_path, "mask"), exist_ok=True) # 处理Training Batch 1 image_path = os.path.join(data_path, "Training Batch 1", "volume-{}.npy") for i in range(131): process_image(image_path.format(i), os.path.join(save_path, "image")) # 处理Training Batch 2 label_path = os.path.join(data_path, "Training Batch 2", "segmentation-{}.npy") for i in range(131): process_label(label_path.format(i), os.path.join(save_path, "mask"))出现FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Administrator\\Desktop\\LiTS2017\\Training Batch 1\\volume-0.npy',修复它,并给出完整代码
时间: 2023-11-22 11:56:37 浏览: 89
tensorflow下的图片标准化函数per_image_standardization用法
首先,这个错误提示表明程序找不到第一个npy文件。因此,需要检查路径是否正确。
同时,还需要确保所有的路径和文件名都是正确的。下面是修复后的完整代码:
```python
import os
import numpy as np
from PIL import Image
def process_image(image_path, save_path):
# 读取nii文件
image_array = np.load(image_path).astype(np.float32)
# 归一化到0-255之间
image_array = (image_array - np.min(image_array)) / (np.max(image_array) - np.min(image_array)) * 255
# 将数据类型转换为uint8
image_array = image_array.astype(np.uint8)
# 将三维图像分成若干个二维图像
for i in range(image_array.shape[0]):
image = Image.fromarray(image_array[i])
image.save(os.path.join(save_path, f"{i}.png"))
def process_label(label_path, save_path):
# 读取nii文件
label_array = np.load(label_path).astype(np.uint8)
# 将标签转换为灰度图
label_array[label_array == 1] = 255
label_array[label_array == 2] = 128
# 将三维标签分成若干个二维标签
for i in range(label_array.shape[0]):
label = Image.fromarray(label_array[i])
label.save(os.path.join(save_path, f"{i}.png"))
# LiTS2017数据集路径
data_path = "C:\\Users\\Administrator\\Desktop\\LiTS2017"
# 保存路径
save_path = "C:\\Users\\Administrator\\Desktop\\2D-LiTS2017"
# 创建保存路径
os.makedirs(save_path, exist_ok=True)
os.makedirs(os.path.join(save_path, "image"), exist_ok=True)
os.makedirs(os.path.join(save_path, "mask"), exist_ok=True)
# 处理Training Batch 1
image_path = os.path.join(data_path, "Training Batch 1", "volume-{}.npy")
for i in range(131):
if os.path.exists(image_path.format(i)):
process_image(image_path.format(i), os.path.join(save_path, "image"))
else:
print(f"{image_path.format(i)} not exists!")
# 处理Training Batch 2
label_path = os.path.join(data_path, "Training Batch 2", "segmentation-{}.npy")
for i in range(131):
if os.path.exists(label_path.format(i)):
process_label(label_path.format(i), os.path.join(save_path, "mask"))
else:
print(f"{label_path.format(i)} not exists!")
```
在这个修复后的代码中,我们添加了对文件是否存在的检查,并输出了相应的提示信息。现在我们可以运行代码进行处理,同时会得到相应的提示信息帮助我们快速定位错误。
阅读全文