医学图像读取与绘制程序,适用于.nii.gz(.nii)格式、.pkl格式、.npz格式
1.文件.nii格式读取显示:
'''
Author: <qilin>
Email: <EMAIL>
Date: 2024-02-20
Description:
This script converts nifti images into an image with
the corresponding.
'''
mode = 1
if mode == 1:
'''
读取nii文件内容
'''
import nibabel as nib
import numpy as np
file_path = r'F:/program/database/Multimodal/muregpro_pkl/us/test_nii/case000067.nii.gz'
file_path = r'F:\program\database\Multimodal\muregpro\train\us_images\case000000.nii.gz'
file_path = r'F:\program\database\Multimodal\OASIS_L2R_2021_task03\Test_nii\img0438.nii.gz'
file_path = r'F:/program/database/Multimodal/muregpro/train/us_images/case000001.nii.gz'
# 读取NIfTI文件
nii_img = nib.load(file_path)
# 获取头信息
header = nii_img.header
# 获取图像数据
data = nii_img.get_fdata()
# 如果数据是浮点数类型,将其转换为整型
if data.dtype.kind == 'f':
data = (data * 255).astype(np.uint8)
# 输出标签和内容
print("文件路径:", file_path)
print("头信息:", header)
print("图像数据:", data)
print("图像维度: ",data.shape)
elif mode == 2:
'''
读取文件中元素频次
'''
import nibabel as nib
import numpy as np
# 读取NIfTI文件
file_path = '/home/adminer/qilin/program/voxelmorph-dev/data/muregpro/train/mr_labels/case000000.nii.gz'
img = nib.load(file_path)
data = img.get_fdata()
# 统计元素出现的频次
unique_elements, counts = np.unique(data, return_counts=True)
# 输出结果
print("出现的元素及其频次:")
for element, count in zip(unique_elements, counts):
print(f"元素 {element}: 出现 {count} 次")
elif mode == 3:
'''
检查数据维度和最大最小值
'''
import nibabel as nib
# 定义 NIfTI 文件路径
nii_file_path = 'F:/文件/麒麟/program/python/TransMorph_Transformer_for_Medical_Image_Registration-main/Data/OASIS/Test_nii/img0438.nii.gz'
label_file_path = 'F:/文件/麒麟/program/python/TransMorph_Transformer_for_Medical_Image_Registration-main/Data/muregpro/train/mr_labels/case000000.nii.gz'
# 加载 NIfTI 文件
nii_img = nib.load(label_file_path)
# 获取数据数组
data = nii_img.get_fdata()
# 获取数据维度
data_shape = data.shape
max = 0
min = 10000
for i in range(len(data)):
for j in range(len(data[i])):
for k in range(len(data[i][j])):
if data[i][j][k] > max:
max = data[i][j][k]
elif data[i][j][k] < min:
min = data[i][j][k]
print("max:",max)
print("min:",min)
print("数据维度:", data_shape)
print("数据切片:",data[100][100])
elif mode == 4:
'''
显示平面切片
'''
import matplotlib.pyplot as plt
import nibabel as nib
# 读取 NIfTI 文件
file_path = '/home/adminer/qilin/program/voxelmorph-dev/data/muregpro/val/mr_images/case000065.nii.gz'
img = nib.load(file_path)
data = img.get_fdata()
# 可视化立体图像
plt.figure(figsize=(8, 8))
# 显示三个轴的切片
plt.subplot(131)
plt.imshow(data[:, :, data.shape[2] // 2], cmap='gray')
plt.title('Axial')
plt.subplot(132)
plt.imshow(data[:, data.shape[1] // 2, :], cmap='gray')
plt.title('Sagittal')
plt.subplot(133)
plt.imshow(data[data.shape[0] // 2, :, :], cmap='gray')
plt.title('Coronal')
plt.show()
elif mode == 5:
'''
逐层显示
'''
import numpy as np
import nibabel as nib
from mayavi import mlab
# 定义 NIfTI 文件路径
nii_file_path = '/home/adminer/qilin/program/voxelmorph-dev/data/muregpro/val/mr_images/case000065.nii.gz'
# 加载 NIfTI 文件
nii_img = nib.load(nii_file_path)
# 获取数据数组
data = nii_img.get_fdata()
# 将数据维度从 (120, 128, 128, 1) 转换为 (128, 128, 120)
data = np.transpose(data.squeeze(), (1, 2, 0))
# 创建 Mayavi 可视化窗口
fig = mlab.figure(size=(800, 800))
# 一次性加载整个3D图
vol = mlab.volume_slice(data, colormap='gray') # 灰度图
# vol = mlab.volume_slice(data, opacity=0.5) # 彩图
# 显示结果
mlab.show()
elif mode == 6:
'''
逐层显示(带标签)
'''
import numpy as np
import nibabel as nib
from mayavi import mlab
# 定义 NIfTI 文件路径
nii_file_path = '/home/adminer/qilin/program/voxelmorph-dev/data/muregpro/train/us_images/case000000.nii.gz'
label_file_path = '/home/adminer/qilin/program/voxelmorph-dev/data/muregpro/train/us_labels/case000000.nii.gz'
# 加载 NIfTI 文件
nii_img = nib.load(nii_file_path)
label_img = nib.load(label_file_path)
# 获取数据数组
data = nii_img.get_fdata()
labels = label_img.get_fdata()
# 将数据维度从 (120, 128, 128, 1) 转换为 (128, 128, 120)
data = np.transpose(data.squeeze(), (1, 2, 0))
# 将标签维度从 (120, 128, 128, 6) 转换为 (128, 128, 120, 6)
labels = np.transpose(labels.squeeze(), (0, 1, 2, 3))
# 创建 Mayavi 可视化窗口
fig = mlab.figure(size=(800, 800))
# 一次性加载整个3D图
vol = mlab.volume_slice(data, colormap='gray') # 灰度图
# vol = mlab.volume_slice(data, opacity=0.5) # 彩图
# 找到标签值为255的位置
x, y, z = np.where(labels[..., 0] == 255) # 选择你想要查看的标签的索引,这里使用了第一个标签
# 标注为红色
mlab.points3d(x, y, z, color=(1, 0, 0), mode='cube', scale_factor=1, opacity=0.01)
# 显示结果
mlab.show()
elif mode == 7:
'''
立体图
'''
import numpy as np
import nibabel as nib
from mayavi import mlab
# 定义 NIfTI 文件路径
# nii_file_path = '/home/adminer/qilin/program/voxelmorph-dev/data/output/moved.nii.gz'
# nii_file_path = '/home/adminer/qilin/program/voxelmorph-dev/data/muregpro/val/us_images/case000069.nii.gz'
# nii_file_path = '/home/adminer/qilin/program/voxelmorph-dev/data/muregpro/val/us_images/case000066.nii.gz'
# nii_file_path = '/home/adminer/qilin/program/voxelmorph-dev/data/muregpro/val/us_images/case000067.nii.gz'
# nii_file_path = '/home/adminer/qilin/program/voxelmorph-dev/data/dataTest/us_output/normal/moved.nii.gz'
moving = 'F:/文件/麒麟/program/python/TransMorph_Transformer_for_Medical_Image_Registration-main/Data/OASIS/Test_nii/img0438.nii.gz'
fixed = 'F:/文件/麒麟/program/python/TransMorph_Transformer_for_Medical_Image_Registration-main/Data/OASIS/Test_nii/img0439.nii.gz'
moved = 'F:/文件/麒麟/program/python/TransMorph_Transformer_for_Medical_Image_Registration-main/Data/OASIS/Test_nii/img0440.nii.gz'
# 加载 NIfTI 文件
nii_img = nib.load(moving)
nii_img_2 = nib.load(fixed)
nii_img_3 = nib.load(moved)
# 获取数据数组
data = nii_img.get_fdata()
data_2 = nii_img_2.get_fdata()
data_3 = nii_img_3.get_fdata()
# 将数据维度从 (120, 128, 128, 1) 转换为 (128, 128, 120)
data = np.transpose(data.squeeze(), (1, 2, 0))
data_2 = np.transpose(data_2.squeeze(), (1, 2, 0))
data_3 = np.transpose(data_3.squeeze(), (1, 2, 0))
# 创建 Mayavi 可视化窗口
# fig = mlab.figure(size=(800, 800))
# vol = mlab.pipeline.iso_surface(mlab.pipeline.scalar_field(data), colormap='gray') # 灰度图
# vol = mlab.pipeline.iso_surface(mlab.pipeline.scalar_field(data), opacity=0.9) # 彩图 (opacity:0——1 不透明度)
# 创建一个大小为800x800的图像窗口
fig = mlab.figure(size=(800, 800))
# 定义数据的形状
shape = data.shape
# 计算数据在x轴方向上的中心位置
center_x = shape[0] // 6 # 左边1/3的中心位置
# 在图像的左边1/3处显示数据
vol = mlab.pipeline.iso_surface(mlab.pipeline.scalar_field(data), colormap='gray') # 灰度图
vol.actor.actor.position = np.array([-center_x, 0, 0])
fig_2 = mlab.figure(size=(800, 800))
# vol_2 = mlab.pipeline.iso_surface(mlab.pipeline.scalar_field(data_2), colormap='gray') # 灰度图
vol_2 = mlab.pipeline.iso_surface(mlab.pipeline.scalar_field(data_2), opacity=0.9) # 彩图 (opacity:0——1 不透明度)
fig_3 = mlab.figure(size=(800, 800))
# vol_3 = mlab.pipeline.iso_surface(mlab.pipeline.scalar_field(data_3), colormap='gray') # 灰度图
vol_3 = mlab.pipeline.iso_surface(mlab.pipeline.scalar_field(data_3), opacity=0.9) # 彩图 (opacity:0——1 不透明度)
# 显示结果
mlab.show()
elif mode == 8:
'''
立体图(带标签)
'''
import numpy as np
import nibabel as nib
from mayavi import mlab
# 定义 NIfTI 文件路径
nii_file_path = 'F:/文件/麒麟/program/python/TransMorph_Transformer_for_Medical_Image_Registration-main/Data/muregpro/train/mr_images/case000001.nii.gz'
label_file_path = 'F:/文件/麒麟/program/python/TransMorph_Transformer_for_Medical_Image_Registration-main/Data/muregpro/train/mr_labels/case000001.nii.gz'
# nii_file_path = '/home/adminer/qilin/program/voxelmorph-dev/data/muregpro/val/us_images/case000069.nii.gz'
# label_file_path = '/home/adminer/qilin/program/voxelmorph-dev/data/muregpro/val/us_labels/case000069.nii.gz'
# 加载 NIfTI 文件
nii_img = nib.load(nii_file_path)
label_img = nib.load(label_file_path)
# 获取数据数组
data = nii_img.get_fdata()
labels = label_img.get_fdata()
# 将数据维度从 (120, 128, 128, 1) 转换为 (128, 128, 120)
data = np.transpose(data.squeeze(), (1, 2, 0))
# 将标签维度从 (120, 128, 128, 6) 转换为 (6, 128, 128, 120)
labels = np.transpose(labels.squeeze(), (0, 1, 2, 3))
# 创建 Mayavi 可视化窗口
fig = mlab.figure(size=(800, 800))
# 创建等值面
vol = mlab.pipeline.iso_surface(mlab.pipeline.scalar_field(data), colormap='gray', opacity=0.9) # 灰度图
# vol = mlab.pipeline.iso_surface(mlab.pipeline.scalar_field(data), opacity=0.9) # 彩图 (opacity:0——1 不透明度)
# 找到标签值为255的位置,标注为红色
x, y, z = np.where(labels[..., 0] == 255) # 选择你想要查看的标签的索引,这里使用了第一个标签
mlab.points3d(x, y, z, color=(1, 0, 0), mode='cube', scale_factor=1, opacity=0.1)
x, y, z = np.where(labels[..., 1] == 255) # 选择你想要查看的标签的索引,这里使用了第一个标签
mlab.points3d(x, y, z, color=(0, 1, 0), mode='cube', scale_factor=1, opacity=0.5)
x, y, z = np.where(labels[..., 2] == 255) # 选择你想要查看的标签的索引,这里使用了第一个标签
mlab.points3d(x, y, z, color=(0, 0, 1), mode='cube', scale_factor=1, opacity=0.5)
x, y, z = np.where(labels[..., 3] == 255) # 选择你想要查看的标签的索引,这里使用了第一个标签
mlab.points3d(x, y, z, color=(1, 1, 0), mode='cube', scale_factor=1, opacity=0.5)
x, y, z = np.where(labels[..., 4] == 255) # 选择你想要查看的标签的索引,这里使用了第一个标签
mlab.points3d(x, y, z, color=(1, 0, 1), mode='cube', scale_factor=1, opacity=0.5)
x, y, z = np.where(labels[..., 5] == 255) # 选择你想要查看的标签的索引,这里使用了第一个标签
mlab.points3d(x, y, z, color=(0, 1, 1), mode='cube', scale_factor=1, opacity=0.5)
# 显示结果
mlab.show()
2.文件.pkl格式读取与显示
'''
Author: <qilin>
Email: <EMAIL>
Date: 2024-02-28
'''
import pickle
import numpy as np
import nibabel as nib
from mayavi import mlab
import matplotlib.pyplot as plt
'''
读取pkl文件内容
'''
def read_pkl(path):
# 加载.pkl文件
with open(path, 'rb') as file:
data = pickle.load(file)
# 将数据转换为Numpy数组
data = np.array(data)
# 获取数据的维度
data_shape = data.shape
print("数据维度:", data_shape)
'''
数据滤波
'''
def filter_y_and_z(y, z):
# 手动统计y数组中每个元素的出现次数
y_count = {}
for num in y:
if num in y_count:
y_count[num] += 1
else:
y_count[num] = 1
# 找出出现次数大于等于3次的元素
y_to_keep = [num for num, count in y_count.items() if count >= 3]
# 将y数组中出现次数大于等于3次的元素所对应的z数组中的数据存储到字典中
y_z_mapping = {}
for idx, num in enumerate(y):
if num in y_to_keep:
if num not in y_z_mapping:
y_z_mapping[num] = []
y_z_mapping[num].append(z[idx])
# 对字典中的每个元素,只保留最大值和最小值
for num, z_values in y_z_mapping.items():
max_z = max(z_values)
min_z = min(z_values)
y_z_mapping[num] = [max_z, min_z]
# 将不在y_to_keep中的y数组元素对应的z数组中数据置为None
filtered_y = []
filtered_z = []
for idx, num in enumerate(y):
if num in y_to_keep:
filtered_y.append(num)
filtered_z.append(y_z_mapping[num][0] if z[idx] == max(y_z_mapping[num]) else y_z_mapping[num][1])
return filtered_y, filtered_z
'''
标签突出
'''
def display_seg_3D(data):
x, y, z = np.where(data == 1)
mlab.points3d(x, y, z, color=(1, 0, 0), mode='cube', scale_factor=1, opacity=0.1) # 红
x, y, z = np.where(data == 2)
mlab.points3d(x, y, z, color=(0, 1, 0), mode='cube', scale_factor=1, opacity=0.5) # 绿
x, y, z = np.where(data == 3)
mlab.points3d(x, y, z, color=(0, 0, 1), mode='cube', scale_factor=1, opacity=0.5) # 蓝
x, y, z = np.where(data == 4)
mlab.points3d(x, y, z, color=(1, 1, 0), mode='cube', scale_factor=1, opacity=0.5) # 黄
x, y, z = np.where(data == 5)
mlab.points3d(x, y, z, color=(1, 0, 1), mode='cube', scale_factor=1, opacity=0.5) # 粉
x, y, z = np.where(data == 6)
mlab.points3d(x, y, z, color=(0, 1, 1), mode='cube', scale_factor=1, opacity=0.5) # 青
def display_seg_Section_1(y, z):
return y,z,y,z
def display_seg_Section_2(y, z):
# 重新排序
sorted_indices = sorted(range(len(y)), key=lambda k: y[k])
z = [z[i] for i in sorted_indices]
y = sorted(y)
num_x1 = []
num_x2 = []
num_y1 = [] # 大rang
num_y2 = [] # 小
j = -1
for i in range(len(y)):
if y[i] in num_x2:
if z[i] > num_y1[j]:
num_y1[j] = z[i]
elif z[i] < num_y2[j]:
num_y2[j] = z[i]
else:
j += 1
num_x1.append(y[i])
num_y1.append(z[i])
num_x2.append(y[i])
num_y2.append(z[i])
# print(x,y,z)
# print(len(x),len(y),len(z))
# print(num_x1,num_y1)
# print(num_x2,num_y2)
# print(len(x),len(num_x1),len(num_y1),len(num_x2),len(num_y2))
return num_x1, num_y1,num_x2, num_y2,
def display_seg_sectionAll(data,x_section):
global mode
global thickness
if mode == 1:
Display = display_seg_Section_1
elif mode == 2:
Display = display_seg_Section_2
y, z = np.where(data[x_section] == 1)
a,b,c,d = Display(y, z)
plt.scatter(a, b, color=(1, 0, 0), s=thickness) # 红
plt.scatter(c, d, color=(1, 0, 0), s=thickness)
y, z = np.where(data[x_section] == 2)
a, b, c, d = Display(y, z)
plt.scatter(a, b, color=(0, 1, 0), s=thickness) # 绿
plt.scatter(c, d, color=(0, 1, 0), s=thickness)
# y, z = np.where(data[x_section] == 3)
# a, b, c, d = Display(y, z)
# plt.scatter(a, b, color=(0, 0, 1), s=thickness) # 蓝
# plt.scatter(c, d, color=(0, 0, 1), s=thickness)
#
# y, z = np.where(data[x_section] == 4)
# a, b, c, d = Display(y, z)
# plt.scatter(a, b, color=(1, 1, 0), s=thickness) # 黄
# plt.scatter(c, d, color=(1, 1, 0), s=thickness)
#
# y, z = np.where(data[x_section] == 5)
# a, b, c, d = Display(y, z)
# plt.scatter(a, b, color=(1, 0, 1), s=thickness) # 粉
# plt.scatter(c, d, color=(1, 0, 1), s=thickness)
#
# y, z = np.where(data[x_section] == 6)
# a, b, c, d = Display(y, z)
# plt.scatter(a, b, color=(0, 1, 1), s=thickness) # 青
# plt.scatter(c, d, color=(0, 1, 1), s=thickness)
'''
立体图(带标签)
'''
def display_3D(data_1,data_3,data_2,data_4):
# 显示mr图像
fig = mlab.figure(size=(800, 800))
vol = mlab.pipeline.iso_surface(mlab.pipeline.scalar_field(data_1), colormap='gray', opacity=0.9) # 灰度图
# vol = mlab.pipeline.iso_surface(mlab.pipeline.scalar_field(data_1), opacity=0.9) # 彩图 (opacity:0——1 不透明度)
display_seg_3D(data_2)
mlab.show()
# 显示us图像
fig_2 = mlab.figure(size=(800, 800))
vol_2 = mlab.pipeline.iso_surface(mlab.pipeline.scalar_field(data_3), colormap='gray', opacity=0.9) # 灰度图
# vol_2 = mlab.pipeline.iso_surface(mlab.pipeline.scalar_field(data_1), opacity=0.9) # 彩图 (opacity:0——1 不透明度)
display_seg_3D(data_4)
mlab.show()
'''
剖面图(带标签)
'''
def diaplay_Section(data_1,data_3,data_2,data_4):
# 可视化立体图像
plt.figure(figsize=(8, 8))
x_section = [data_1.shape[0] // 4 * 1,data_1.shape[0] // 4 * 2,data_1.shape[0] // 4 * 3]
x_section_2 = [data_3.shape[0] // 4 * 1,data_3.shape[0] // 4 * 2,data_3.shape[0] // 4 * 3]
# 显示切片
plt.subplot(231)
plt.imshow(data_1[x_section[0], :, :], cmap='gray')
display_seg_sectionAll(data_2,x_section[0])
plt.title('Coronal')
plt.subplot(232)
plt.imshow(data_1[x_section[1], :, :], cmap='gray')
display_seg_sectionAll(data_2,x_section[1])
plt.title('Coronal')
plt.subplot(233)
plt.imshow(data_1[x_section[2], :, :], cmap='gray')
display_seg_sectionAll(data_2,x_section[2])
plt.title('Coronal')
plt.subplot(234)
plt.imshow(data_3[x_section_2[0], :, :], cmap='gray')
display_seg_sectionAll(data_4,x_section_2[0])
plt.title('Coronal')
plt.subplot(235)
plt.imshow(data_3[x_section_2[1], :, :], cmap='gray')
display_seg_sectionAll(data_4,x_section_2[1])
plt.title('Coronal')
plt.subplot(236)
plt.imshow(data_3[x_section_2[2], :, :], cmap='gray')
display_seg_sectionAll(data_4,x_section_2[2])
plt.title('Coronal')
plt.show()
'''
主函数
'''
# pkl_file_path = 'F:\program\database\Multimodal\OASIS_L2R_2021_task03/Test/p_0438_0439.pkl'
pkl_file_path = '/home/adminer/qilin/data/muregpro_ql/mr/train/case000001.pkl'
pkl_file_path_2 = '/home/adminer/qilin/data/muregpro_ql/mr/val/case000066.pkl'
mode = 1 # 显示模式 1:全显示 2:边界显示
thickness = 0.5
with open(pkl_file_path, 'rb') as f:
data_1,data_2 = pickle.load(f) # data_1,data_2分别是mr图像的img与seg
with open(pkl_file_path_2, 'rb') as f:
data_3,data_4 = pickle.load(f) # data_1,data_2分别是mr图像的img与seg
data_1 = np.transpose(data_1.squeeze(), (1, 2, 0))
data_3 = np.transpose(data_3.squeeze(), (1, 2, 0))
# data_2 = np.transpose(data_2.squeeze(), (1, 2, 0))
# data_4 = np.transpose(data_4.squeeze(), (1, 2, 0))
display_3D(data_1,data_3,data_2,data_4)
# diaplay_Section(data_1,data_3,data_2,data_4)mureg
3.文件.npz格式读取与绘制
'''
Author: <qilin>
Email: <EMAIL>
Date: 2024-01-19
Description:
This script converts npz files to an image file that can be used for visualization.
'''
import numpy as np
import matplotlib.pyplot as plt
mode = 4 # 可选模式: 1.折线图绘制 2.输出npz文件内数组名 3.平面切片绘图 4.立体图像绘制
file_name = 'F:/文件/麒麟/program/python/hypermorph-dev/data/dataTest/train1.npz'
array_name = 'vol'
# 'F:/文件/麒麟/program/python/hypermorph-dev/data/dataTest/train1.npz' ['vol', 'seg', 'train_avg']
# 'F:/文件/麒麟/program/python/hypermorph-dev/data/dataTest/train2.npz' ['vol', 'seg', 'train_avg']
# 'F:/文件/麒麟/program/python/hypermorph-dev/data/atlas.npz' ['vol', 'seg', 'train_avg']
# 'F:/文件/麒麟/program/python/hypermorph-dev/data/generated_uncond_atlas.npz' ['vol']
# 'F:/文件/麒麟/program/python/hypermorph-dev/data/labels.npz' ['labels']
# 'F:/文件/麒麟/program/python/hypermorph-dev/data/prob_atlas.npz' ['vol']
# 'F:/文件/麒麟/program/python/hypermorph-dev/data/prob_atlas_mapping.npz' ['mapping']
# 'F:/文件/麒麟/program/python/hypermorph-dev/data/prob_atlas_T1_stats.npz' ['init_mu', 'init_sigma']
# 'F:/文件/麒麟/program/python/hypermorph-dev/data/test_scan.npz' ['vol', 'seg']
'''
3. 'F:/文件/麒麟/program/python/hypermorph-dev/data/atlas.npz'
键值对:['vol', 'seg', 'train_avg']
含义:这个 .npz 文件包含一个参考图谱(atlas)。'vol' 键对应的值是图谱的体积数据,'seg' 键对应的值是图谱的分割数据,'train_avg' 键对应的值是训练平均数据。
4.'F:/文件/麒麟/program/python/hypermorph-dev/data/generated_uncond_atlas.npz'
键值对:['vol']
含义:这个 .npz 文件包含一个生成的无条件图谱(generated unconditional atlas)。'vol' 键对应的值是图谱的体积数据。
5.'F:/文件/麒麟/program/python/hypermorph-dev/data/labels.npz'
键值对:['labels']
含义:这个 .npz 文件包含标签数据(labels)。'labels' 键对应的值是标签数据。
6.'F:/文件/麒麟/program/python/hypermorph-dev/data/prob_atlas.npz'
键值对:['vol']
含义:这个 .npz 文件包含概率图谱(probabilistic atlas)。'vol' 键对应的值是概率图谱的体积数据。
7.'F:/文件/麒麟/program/python/hypermorph-dev/data/prob_atlas_mapping.npz'
键值对:['mapping']
含义:这个 .npz 文件包含概率图谱的映射数据(mapping)。'mapping' 键对应的值是概率图谱的映射数据。
8.'F:/文件/麒麟/program/python/hypermorph-dev/data/prob_atlas_T1_stats.npz'
键值对:['init_mu', 'init_sigma']
含义:这个 .npz 文件包含概率图谱 T1 统计数据。'init_mu' 键对应的值是概率图谱的均值数据,'init_sigma' 键对应的值是概率图谱的标准差数据。
9.'F:/文件/麒麟/program/python/hypermorph-dev/data/test_scan.npz'
键值对:['vol', 'seg']
含义:这个 .npz 文件包含测试扫描数据。'vol' 键对应的值是扫描的体积数据,'seg' 键对应的值是扫描的分割数据。
'''
'''
折线图绘制
'''
if mode == 1:
# 加载.npz文件 ['vol', 'seg', 'train_avg']
# data = np.load('F:/文件/麒麟/实验室/医学图像/论文/开源库/hypermorph-dev/data/dataTest/train1.npz')
data = np.load(file_name)
# 查看文件中保存的数组名称
array_names = data.files
print("Arrays in the .npz file:", array_names)
# 遍历数组并进行可视化
for name in array_names:
array = data[name]
print("Array:", name)
print("Shape:", array.shape)
print("Data:", array)
# 可视化数组
plt.figure()
if array.ndim == 1:
plt.plot(array)
elif array.ndim == 2:
plt.imshow(array, cmap='gray')
else:
print("Skipping visualization for array", name, "due to high dimensions.")
continue
plt.title(name)
plt.show()
'''
输出npz文件内数组名
'''
elif mode == 2:
# 加载.npz文件
data = np.load(file_name)
# 查看文件中保存的数组名称
array_names = data.files
print("Arrays in the .npz file:", array_names)
'''
平面切片绘图
'''
elif mode == 3:
# 加载.npz文件 ['vol', 'seg', 'train_avg']
data = np.load(file_name)
# 获取图像数组
image = data[array_name]
# 选择切片索引
slice_index = 80
# 提取特定切片
slice_image = image[slice_index, :, :]
# 可视化切片图像
plt.imshow(slice_image, cmap='gray')
plt.axis('off') # 关闭坐标轴
plt.show()
'''
立体图像绘制
'''
elif mode == 4:
import numpy as np
from mayavi import mlab
# 加载.npz文件
data = np.load(file_name)
# 获取图像数组
image = data[array_name]
# 创建Mayavi场景
mlab.figure(bgcolor=(1, 1, 1))
# 创建体数据对象
vol = mlab.pipeline.volume(mlab.pipeline.scalar_field(image))
# 显示体数据
mlab.show()