请写一段python代码使其实现读取一个文件夹下所有的niigz格式的CT文件并一个个进行可视化nifti数据读写建议使用标准库:SimpleITK最后加上加载mask的功能
import os import SimpleITK as sitk
定义文件夹路径
folder_path = "path_to_folder"
获取文件夹下所有.nii.gz文件的路径
file_paths = [os.path.join(folder_path, file) for file in os.listdir(folder_path) if file.endswith(".nii.gz")]
遍历每个文件进行可视化
for file_path in file_paths: # 读取nifti文件 image = sitk.ReadImage(file_path)
# 可视化
sitk.Show(image)
# 加载mask
mask_path = file_path.replace(".nii.gz", "_mask.nii.gz")
if os.path.exists(mask_path):
mask = sitk.ReadImage(mask_path)
sitk.Show(mask)
原文地址: https://www.cveoy.top/t/topic/iwHJ 著作权归作者所有。请勿转载和采集!