【问题标题】:XYZ fromat of nifti MRI imagesnifti MRI 图像的 XYZ 格式
【发布时间】:2023-02-24 04:50:36
【问题描述】:

我正在处理 X、Y、Z 格式的 3d MRI 图像,其中 Z 是大脑中的切片数。我有 JPEGS 和 niftis,我想将这两个文件保存为 xyz 格式的 numpy 数组。在 nifti 文件格式中,文件以 xyz 形式读入,但是当我打印第一个切片时,我将其作为 x 轴长度 z 和 Y 的长度。我怎样才能将它读作 xyz 并且仍然能够将三维打印为正方形。

img = nib.load(os.path.join(data_path, str(list_dir[i]) + ".nii.gz"))

# Get the data from the NIFTI image
data = img.get_fdata()

# Get the shape of the data
slices = []
print(data.shape)
data = np.transpose(data, (0, 1, 2))
shape = data.shape
print(shape)

# Loop through each slice in the data
for i in range(shape[2]):
    # Get the current slice
    slice = data[:,:,i]
    slice = np.rot90(slice, axes = (1,0))

    # Save the slice as a 3D Numpy array
    slices.append(np.array(slice))
# Convert the list of slices to a Numpy array
print(len(slices))
slices = np.array(slices)
print(slices.shape)

【问题讨论】:

  • 目前还不清楚。 “因为 z 的 x 轴长度和 Y 的长度是”无法理解。
  • 首次加载图像时,data.shape 中的值是多少?他们是你所期望的吗?你说 X、Y 和 Z 是数字,x、y 和 z 是方向,对吗?您是否希望 x 从左到右,y 从前到后,z 从下到上?

标签: python image numpy nifti mri


【解决方案1】:

我知道从一些(比如 3 个)JPEG 文件制作 NIftI 文件的最快方法是:

先下载一个文件试试:
wget https://upload.wikimedia.org/wikipedia/commons/3/3b/MRI_brain.jpg

然后,使用您可以获得的所有库

import matplotlib.pyplot as plt;   # for reading JPG files
import nibabel as nib;             # for writing nifti files
import numpy as np;                # for concatenating 2D -> 3D

sli = plt.imread ( '/tmp/MRI_brain.jpg' );     # load the JPG 'slice;
img = np.dstack ( ( sli, sli, sli ) )          # stack copies as 3D
nii = nib.Nifti1Image ( img, np.eye ( 4 ) );   # make a NIfTI image
nii.to_filename ( 'test.nii' );                # save as a NIfTI file

那应该给你一个 3 个切片的 3D 图像。

XYZ 故事的(我认为)你的意思是,在 MRI 图像查看器中,“最快”方向 X 是从左到右,Y 是从前到后,Z 是从下到上。但是这些尺寸可能与图像的存储方式不对应。这些值可能是从右到左而不是从左到右存储的,并且维度可能是 ZXY 而不是 XYZ。

如果您在查看器中打开文件 test.nii,您将看到每个切片都显示一个矢状图像(这将是 XZ 维度),但它们显示为轴向切片,并且(如果查看器解释它们)R-L , A-P 和 I-S 是相反的。这是因为co-ordinate matrix不对应文件中的存储顺序。

在这种情况下,最快的解决方案是使用与存储顺序对应的坐标矩阵:

nii = nib.Nifti1Image ( img, np.asarray ( [ [ 0,  0, 1, 0],
                                            [ 0, -1, 0, 0],
                                            [-1,  0, 0, 0],
                                            [ 0,  0, 0, 1] ] ) );
nii.to_filename ( 'test.nii' );

那么 A-P 和 I-S 的字母就会正确显示。
(切片相同,所以 L-R 和 R-L 之间没有唯一的解决方案)

【讨论】:

    猜你喜欢
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多