我从NIfTI文件中读取SimpleITK(使用python)的3D图像,获取每个轴向切片,对其执行某些操作,然后将新的2D切片重新插入具有(希望)适当尺寸的3D体积中。例如,
output = sitk.Image(original.GetSize(), sitk.sitkFloat32)
output.CopyInformation(original)
for z in numpy.arange(original.GetDepth()):
image = original[:,:,z]
<< Do Something in SimpleITK>>
<< Produce a new 2D image = newimage >>
output[:,:,z] = newimage
最后一步是抛出错误
In [???]: (executing line ??? of "code.py")
Traceback (most recent call last):
File "code.py", line ???, in <module>
output[:,:,z] = newimage
File "/Library/Python/2.7/site-packages/SimpleITK-0.8.1-py2.7-macosx-10.10-intel.egg/SimpleITK/SimpleITK.py", line 3894, in __setitem__
raise IndexError("invalid index")
IndexError: invalid index
在for循环中完成最后一步的正确语法(或命令集)是什么?
答案 0 :(得分:5)
使用“粘贴”功能将切片图像粘贴到卷中。唯一的小技巧是粘贴功能假设两个图像都是3d。因此,您需要将2d图像转换为3d图像(z大小为1)。您可以使用JoinSeries函数执行此操作。
这是一个示例python脚本,用于说明这将如何工作
#! /usr/bin/env python
import SimpleITK as sitk
# make a black volume
vol_img = sitk.Image(100,100,100,sitk.sitkUInt8)
# make a white slice
slice_img = sitk.Image(100,100,sitk.sitkUInt8)
slice_img = slice_img + 200
# convert the 2d slice into a 3d volume
slice_vol = sitk.JoinSeries(slice_img)
# z insertion location
z = 42
# paste the 3d white slice into the black volume
pasted_img = sitk.Paste(vol_img, slice_vol, slice_vol.GetSize(), destinationIndex=[0,0,z])
sitk.Show(pasted_img)
答案 1 :(得分:0)