python - Operate on slices of 3D image in SimpleITK and create new 3D image -


i have 3d image read in simpleitk (using python) nifti file, take each axial slice, , re-insert new 2d slice 3d volume (hopefully) appropriate dimensions. example,

output = sitk.image(original.getsize(), sitk.sitkfloat32) output.copyinformation(original) z in numpy.arange(original.getdepth()):     image = original[:,:,z]     << in simpleitk>>     << produce new 2d image = newimage >>     output[:,:,z] = newimage 

the final step throwing error

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 

what correct syntax (or set of commands) complete final step in loop?

use paste function paste slice image volume. slight trick paste function assumes both images 3d. need convert 2d image 3d image (with z size of 1). can joinseries function.

here example python script show how work

#! /usr/bin/env python  import simpleitk sitk  # make black volume vol_img = sitk.image(100,100,100,sitk.sitkuint8)  # make white slice slice_img = sitk.image(100,100,sitk.sitkuint8) slice_img = slice_img + 200  # convert 2d slice 3d volume slice_vol = sitk.joinseries(slice_img)  # z insertion location z = 42  # paste 3d white slice black volume pasted_img = sitk.paste(vol_img, slice_vol, slice_vol.getsize(), destinationindex=[0,0,z])  sitk.show(pasted_img) 

Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -