可以下载ITK-SNAP(医学图像2D/3D可视化)进行查看,下载链接
安装:直接双击.exe文件即可
1、导入医学图像
直接把图像拖入ITK-SNAP
2、导入相应的mask
import os
import nibabel as nib
from PIL import Image
nii_folder = '/datasets/LiTs17' # 替换为包含NIfTI文件的文件夹路径
output_folder = '/datasets/LiTs17_PNG/' # 替换为输出PNG文件的文件夹路径
os.makedirs(output_folder, exist_ok=True)
# 遍历输入文件夹中的每个 .nii 文件
for nii_file_name in os.listdir(nii_folder):
if nii_file_name.endswith('.nii') or nii_file_name.endswith('.nii.gz'):
nii_file_path = os.path.join(nii_folder, nii_file_name)
# 加载NIfTI文件
nii_data = nib.load(nii_file_path)
nii_array = nii_data.get_fdata()
# 将像素值范围归一化到0-255之间
nii_array_normalized = ((nii_array - nii_array.min()) / (nii_array.max() - nii_array.min())) * 255
nii_array_normalized = nii_array_normalized.astype('uint8')
# 创建PIL图像对象
for slice_idx in range(nii_array.shape[2]):
slice_data = nii_array_normalized[:, :, slice_idx]
png_image = Image.fromarray(slice_data, mode='L')
# 构建PNG文件名,并保存为PNG文件
png_file_name = f'{os.path.splitext(nii_file_name)[0]}_{slice_idx:03d}.png'
png_file_path = os.path.join(output_folder, png_file_name)
png_image.save(png_file_path)
import os
import numpy as np
import nibabel as nib
from PIL import Image
input_folder = '/root/autodl-tmp/datasets/LiTs17' # 输入NIfTI文件夹路径
output_folder = '/root/autodl-tmp/datasets/LiTs17_png' # 输出PNG文件夹路径
os.makedirs(output_folder, exist_ok=True)
# 遍历输入文件夹中的每个 .nii.gz 文件
for nii_file_name in os.listdir(input_folder):
if nii_file_name.endswith('.nii.gz'):
nii_file_path = os.path.join(input_folder, nii_file_name)
# 加载NIfTI文件
nii_image = nib.load(nii_file_path)
nii_data = nii_image.get_fdata()
# 创建输出文件夹
output_subfolder = os.path.join(output_folder, nii_file_name.replace('.nii.gz', ''))
os.makedirs(output_subfolder, exist_ok=True)
# 将NIfTI数据转换为PNG图像并保存在输出子文件夹中
for slice_idx in range(nii_data.shape[-1]):
slice_data = nii_data[..., slice_idx]
slice_data_normalized = ((slice_data - np.min(slice_data)) / (
np.max(slice_data) - np.min(slice_data))) * 255
slice_data_normalized = slice_data_normalized.astype(np.uint8)
png_image = Image.fromarray(slice_data_normalized, mode='L')
png_file_path = os.path.join(output_subfolder, f'slice_{slice_idx:03d}.png')
png_image.save(png_file_path)