Python 运行Nipype教程时Nibabel中出错
从Python 运行Nipype教程时Nibabel中出错,python,nipype,Python,Nipype,从nibabel.get_data()对niimg进行切片时出现问题 当使用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
nibabel.get_data()
对niimg进行切片时出现问题
当使用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
---------------------------------------------------------------------------
索引器回溯(最后一次最近调用)
在()
---->1个绘图片(“输出/run001\u bet.nii.gz”)
在plot_slice(fname,z_idx)中
12#绘制数据
如果data.dtype==np.int16 else(30150),则为13 vmin,vmax=(0,1)
--->14 plt.imshow(np.rot90(数据[:,:,ctr[2]+z_idx]),
15 cmap=“灰色”,vmin=vmin,vmax=vmax)
16 plt.gca().设置轴关闭()
索引器错误:只有整数、片(`:`)、省略号(`…`)、numpy.newaxis(`None`)和整数或布尔数组是有效的索引
因为ctr[2]是由np.dot返回的,所以它实际上是一个浮点,即使它看起来像一个int。试试int(ctr[2])+z_idx。你确定ctr[2]+z_idx
是一个int
。这是根据教程笔记本。