nii格式图像 灰度值 归一化
时间: 2024-08-16 13:09:13 浏览: 255
NII(Neuroimaging Informatics Technology Initiative)是一种标准格式,主要用于存储和管理医学影像数据,如MRI、CT等脑部扫描图像。这种格式包含元数据以及与图像相关的详细信息,例如灰度值。
灰度值是指图像中每个像素点的亮度等级,通常范围是从0到255或-1到1之间,其中0代表黑色,白色表示最高亮度。在神经影像中,灰度值反映了组织的不同密度,比如脑白质、脑灰质和背景的对比。
归一化(Normalization)是对NII图像灰度值的一种处理步骤,目的是将所有图像的数据标准化到相同的尺度上。常见的归一化方法有全局最小最大规范化(min-max normalization),将像素值缩放到[0,1]区间;z-score标准化,使得数据均值为0,标准差为1;或者通过其他自定义方法调整,确保图像在整个分析过程中具有一致的对比度和可比性。
相关问题
nii 灰度值 归一化
"NII"(National Institute of Informatics)不是一个直接与灰度值归一化相关的技术术语,它通常指的是日本国立情报学研究所。然而,在图像处理领域,"灰度值归一化"是一种常见的预处理步骤。灰度值归一化是指将图像中每个像素点的灰度强度值缩放到一个特定范围,比如0到1之间,或者将其转换成标准差为1的分布。这有助于减少数据间的差异,使得后续分析或机器学习模型训练更为稳定。常见的归一化方法有最小-max标准化、z-score标准化等。
LiTS2017数据集的路径为D:\BaiduNetdiskDownload\LiTS2017,将数据集的3D图像转换成2D的三通道png格式图像,3D标签转换成(背景灰度值0、肝脏灰度值1、肝脏肿瘤灰度值2的)2D的png格式标签。,LiTS2017数据集里面包含Training Batch 1和Training Batch 2两个文件夹,Training Batch 1文件夹里面有图像,图像的拓展名为.nii。Training Batch 2文件夹里面有标签,标签拓展名为.nii,保存路径为C:\Users\Administrator\Desktop\2D-LiTS2017,请帮我创建好对应文件夹,并且将转换好的保存到对应的文件夹
好的,以下是对应的代码实现:
```python
import os
import numpy as np
import nibabel as nib
from PIL import Image
# 定义数据集路径
data_path = 'D:/BaiduNetdiskDownload/LiTS2017'
# 定义保存路径
save_path = 'C:/Users/Administrator/Desktop/2D-LiTS2017'
# 定义函数将3D图像转换成2D的三通道png格式图像
def convert_image(img_path, save_path):
# 加载nii格式的图像
img = nib.load(img_path).get_fdata()
# 将图像归一化到0-255之间
img = (img - np.min(img)) / (np.max(img) - np.min(img)) * 255
# 将图像转换成三通道的png格式图像
img = np.stack([img, img, img], axis=-1).astype(np.uint8)
# 获取图像文件名
img_name = os.path.splitext(os.path.basename(img_path))[0]
# 保存图像
Image.fromarray(img).save(os.path.join(save_path, img_name + '.png'))
# 定义函数将3D标签转换成2D的png格式标签
def convert_label(label_path, save_path):
# 加载nii格式的标签
label = nib.load(label_path).get_fdata()
# 将标签转换成二维的png格式标签
label = np.where(label == 1, 255, label) # 将肝脏部分设置为白色
label = np.where(label == 2, 127, label) # 将肝脏肿瘤部分设置为灰色
label = label.astype(np.uint8)
# 获取标签文件名
label_name = os.path.splitext(os.path.basename(label_path))[0]
# 保存标签
Image.fromarray(label).save(os.path.join(save_path, label_name + '.png'))
# 创建保存图像和标签的文件夹
if not os.path.exists(save_path):
os.mkdir(save_path)
os.mkdir(os.path.join(save_path, 'image'))
os.mkdir(os.path.join(save_path, 'label'))
# 遍历Training Batch 1文件夹里的图像文件
for img_file in os.listdir(os.path.join(data_path, 'Training Batch 1')):
if img_file.endswith('.nii'):
# 转换图像并保存到对应文件夹
img_path = os.path.join(data_path, 'Training Batch 1', img_file)
convert_image(img_path, os.path.join(save_path, 'image'))
# 遍历Training Batch 2文件夹里的标签文件
for label_file in os.listdir(os.path.join(data_path, 'Training Batch 2')):
if label_file.endswith('.nii'):
# 转换标签并保存到对应文件夹
label_path = os.path.join(data_path, 'Training Batch 2', label_file)
convert_label(label_path, os.path.join(save_path, 'label'))
```
运行代码后,你会发现在C:/Users/Administrator/Desktop/2D-LiTS2017目录下,会生成image和label两个文件夹,分别保存转换好的图像和标签。
阅读全文
相关推荐















