什么是.nii文件,数据如何存储在其中?我有其中一些,我想知道如何从它们创建扫描3D图像。MRI
我可以使用 nibabel 在我的 python 脚本中加载文件。接下来从这里去哪里?
什么是.nii文件,数据如何存储在其中?我有其中一些,我想知道如何从它们创建扫描3D图像。MRI
我可以使用 nibabel 在我的 python 脚本中加载文件。接下来从这里去哪里?
在 python 中通过 numpy 数组处理图像的最常见方式。由于您已经通过 nibabel 加载了图像,因此您需要从图像对象中获取数据,然后将其转换为 numpy 数组。
import nibabel as nib
import numpy as np
# Get nibabel image object
img = nib.load("path/to/image.nii")
# Get data from nibabel image object (returns numpy memmap object)
img_data = img.get_data()
# Convert to numpy ndarray (dtype: uint16)
img_data_arr = np.asarray(img_data)
默认情况下,这应该是一个形状为(高度、宽度、图像)的 3D numpy 数组。如果您想访问一张图片,您可以随时使用 PIL。例如保存 .nii 文件的第一个图像:
from PIL import Image
img = Image.fromarray(img_data_arr_norm[:,:,0], 'L')
img.save("image.jpeg")