Search code examples
pythonnumpyfiltermaskingsimpleitk

SimpleITK Selectively Alter Pixels / Slicing


I've loaded a CT scan in SimpleITK. I'd like to do a few things that are pretty simple in NumPy, but haven't figured out how to do them in SimpleITK. I'd like to do them in SimpleITK for speed.

# NumPy: Changes all values of 100 to now become 500
nparr = nparr[nparr == 100] = 500

# SimpleITK:
???

SimpleITK image==100 will produce a binary image of the same dimension, where all intensities==100 are 1/True. This is desired. But I don't believe SimpleITK supports boolean indexing unfortunately. What's the most efficient way to accomplish this?

I've come up with this funky looking thing; but I was hoping to find the intended method / best practice means for doing this:

# Cast because data type returned is uint8 otherwise
difference = 500 - 100
offset = SimpleITK.Cast( image == 100), sitk.sitkInt32 ) * difference
image += offset

Solution

  • You can use the BinaryTheshold filter.

    result = sitk.BinaryThreshold( image, 100, 101, 500, 0 )

    That should only select pixels with intensity 100.