在 SimpleITK 中对 3D 图像切片进行操作并创建新的 3D 图像 [英] Operate on slices of 3D image in SimpleITK and create new 3D image

查看:36
本文介绍了在 SimpleITK 中对 3D 图像切片进行操作并创建新的 3D 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 3D 图像从 NIfTI 文件读入 SimpleITK(使用 python),取每个轴向切片,用它做一些事情,然后将新的 2D 切片重新插入到具有(希望)适当尺寸的 3D 体积中.例如,

I have a 3D image read in to SimpleITK (using python) from a NIfTI file, take each axial slice, do something with it and re-insert the new 2D slice into a 3D volume with the (hopefully) appropriate dimensions. For example,

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 循环中最后一步的正确语法(或命令集)是什么?

What is the correct syntax (or set of commands) to complete the final step in my for loop?

推荐答案

使用粘贴功能将切片图像粘贴到卷中.唯一的小技巧是粘贴功能假定两个图像都是 3d.因此,您需要将 2d 图像转换为 3d 图像(z 大小为 1).您可以使用 JoinSeries 函数执行此操作.

Use the Paste function to paste your slice image into the volume. The only slight trick is that the Paste function assumes both images are 3d. So you need to convert your 2d image into a 3d image (with z size of 1). You can do this with the JoinSeries function.

这是一个 Python 脚本示例,用于展示其工作原理

Here is an example python script to show how this would work

#! /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)

这篇关于在 SimpleITK 中对 3D 图像切片进行操作并创建新的 3D 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆