What is the best way to do assign values or slices to indexed arrays in SimpleITK?
Example (1): Assigning a 2D slice to an indexed 2D slice in a 3D volume
In NumPy, we can do the following assignment to the index arrays:
import numpy as np
nda = np.ones((64, 256,256))
nda[0,:,:] = 2*nda[0,:,:]
The same operation in SimpleITK,
import SimpleITK as sitk
image = sitk.GetImageFromArray(nda)
image[:,:,0] = 2*image[:,:,0]
gives the following error:
IndexError Traceback (most recent call last)
<ipython-input-18-4649e90a4ea5> in <module>
----> 1 image[:,:,0] = 2*image[:,:,0]
~/anaconda3/lib/python3.7/site-packages/SimpleITK/SimpleITK.py in __setitem__(self, idx, value)
4690
4691 # the index parameter was an invalid set of objects
-> 4692 raise IndexError("invalid index")
4693
4694
IndexError: invalid index
Example (2): Assigning a value an indexed 2D slice in a 3D volume The following operation in NumPy,
nda[0,:,:] = 2
n SimpleITK,
image[:,:,0] = 2
gives the same Index error as in Example (1).
Unfortunately you can't do that type of operation in SimpleITK. The left hand side of the the assignment operator has to be a single pixel. It can't be a slice.
To do those types of operations, you'd have to extract out a slice from the volume, operate on the slice and then use the Paste function to paste the slice back into the volume.
You can see an example of the Paste function in an earlier answer I posted: https://stackoverflow.com/a/32612215/3712577