2

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
Authman Apatira
  • 3,994
  • 1
  • 26
  • 33

2 Answers2

3

You can use the BinaryTheshold filter.

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

That should only select pixels with intensity 100.

Dave Chen
  • 1,905
  • 1
  • 12
  • 18
2

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)
g.stevo
  • 745
  • 3
  • 10
  • 1
    I was hoping there was a way to do it within sitk w/o converting to numpy, as a lot of the sitk functions are more optimal and perform faster – Authman Apatira Apr 06 '17 at 18:21