请写一段python代码使其实现读取一个文件夹下所有的niigz格式的三维CT图像并进行可视化并在同一个窗口中同时加载mask每可视化一次就暂停由用户决定选择下一张图片使用SimpleITK库
以下是使用SimpleITK库读取一个文件夹下所有的(.nii.gz)格式的三维CT图像并进行可视化,并在同一个窗口中同时加载mask的Python代码:
import SimpleITK as sitk
import matplotlib.pyplot as plt
import os
# 读取文件夹下所有的(.nii.gz)格式的文件
folder_path = "path_to_folder"
files = [file for file in os.listdir(folder_path) if file.endswith('.nii.gz')]
# 创建一个窗口
plt.figure()
# 遍历每个文件
for file in files:
# 读取CT图像
image_path = os.path.join(folder_path, file)
image = sitk.ReadImage(image_path)
image_array = sitk.GetArrayFromImage(image)
# 读取mask图像
mask_path = "path_to_mask"
mask = sitk.ReadImage(mask_path)
mask_array = sitk.GetArrayFromImage(mask)
# 显示CT图像
plt.imshow(image_array[0, :, :], cmap='gray')
# 显示mask图像
plt.imshow(mask_array[0, :, :], cmap='jet', alpha=0.5)
# 暂停,等待用户选择下一张图片
plt.pause(0.001)
# 清除当前窗口
plt.clf()
# 关闭窗口
plt.close()
请将代码中的"path_to_folder"和"path_to_mask"替换为实际的文件夹路径和mask文件路径。
原文地址: http://www.cveoy.top/t/topic/iwMm 著作权归作者所有。请勿转载和采集!