首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >运行Nipype教程时Nibabel中的错误

运行Nipype教程时Nibabel中的错误
EN

Stack Overflow用户
提问于 2018-02-12 11:37:51
回答 1查看 89关注 0票数 2

有问题的切片niimgnibabel.get_data()

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

代码语言:javascript
复制
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()

要运行

代码语言:javascript
复制
plot_slice("output/run001_bet.nii.gz")

结果是这样的;

代码语言:javascript
复制
---------------------------------------------------------------------------
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
EN

回答 1

Stack Overflow用户

发布于 2019-11-06 16:17:58

因为ctr2是由np.dot返回的,所以它实际上是一个浮点数,尽管它看起来像一个int。尝试int(ctr2) + z_idx。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48745598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档