Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 684
Authman Apatira Avatar asked Jan 19 '26 02:01

Authman Apatira


2 Answers

You can use the BinaryTheshold filter.

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

That should only select pixels with intensity 100.

like image 167
Dave Chen Avatar answered Jan 20 '26 17:01

Dave Chen


You are working using the SimpleITK image object to use it in a numpy style you need to use the methods GetArrayFromImage and GetImageFromArray to then get pixel access by converting the imagedata into a numpy array.

import SimpleITK as sitk

difference = 500 - 100
img_arr = sitk.GetArrayFromImage(image)
offset =  img_arr[img_arr == 100] * difference
output = sitk.GetImageFromArray(image += offset)
like image 29
g.stevo Avatar answered Jan 20 '26 17:01

g.stevo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!