当前位置: 代码迷 >> python >> 运行 Nipype 教程时 Nibabel 出错
  详细解决方案

运行 Nipype 教程时 Nibabel 出错

热度:106   发布时间:2023-07-16 11:01:51.0

nibabel.get_data()切片niimgnibabel.get_data()问题。

使用Nipype教程的这个功能时,

def plot_slice(fname, z_idx=5):
    # Load the image and collect the data
    # and orientation information
    img = nib.load(fname)
    data = img.get_data()
    aff = img.get_affine()

    # Find the center of the brain matrix
    ctr = np.dot(np.linalg.inv(aff), [0, 0, 0, 1])[:3]

    # Plot the data
    vmin, vmax = (0, 1) if data.dtype == np.int16 else (30, 150)
    plt.imshow(np.rot90(data[:, :, ctr[2] + z_idx]), cmap="gray", vmin=vmin, vmax=vmax)
    plt.gca().set_axis_off()

plot_slice("output/run001_bet.nii.gz")

然后结果是这样的;

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-10-5b2c71c131be> in <module>()
----> 1 plot_slice("output/run001_bet.nii.gz")

<ipython-input-8-4c2563317c99> in plot_slice(fname, z_idx)
     12     # Plot the data
     13     vmin, vmax = (0, 1) if data.dtype == np.int16 else (30, 150)
---> 14     plt.imshow(np.rot90(data[:, :, ctr[2] + z_idx]), 
     15                cmap="gray", vmin=vmin, vmax=vmax)
     16     plt.gca().set_axis_off()

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

因为 ctr[2] 是由 np.dot 返回的,它实际上是一个浮点数,即使它看起来像一个整数。 尝试 int(ctr[2]) + z_idx。