pythonitksimpleitk

How do you multiply all of the pixels of a Simple ITK image by a scalar?


I'm using the SimpleITK package for Python. I'm trying to multiply an Image constructed using the ImageFileReader class by a scalar value. I've tried using the * operator and the Multiply function to no avail. Both return a maximum value of 0.0

scalar = 5.0
image = image * scalar
print(np.max(sitk.GetArrayFromImage(image)))

This returns 0

I've also tried the MultiplyImageFilter.

multiply = sitk.MultiplyImageFilter()
image = multiply.Execute(image, scalar)
print(np.max(sitk.GetArrayFromImage(image)))

This also returns 0

Is there any easy way to scale the image intensities that I am missing?


Solution

  • I realized the challenge. When generating the image, the pixel representation was an unsigned integer. I was multiplying by a float value, which was causing all of the pixels to zero out due to rounding. To solve this issue, cast the image to float type and then apply basic multiplication.

    For example...

    rdr = sitk.ImageFileReader()
    rdr.SetFileName(file)
    image = rdr.Execute()
    
    #max value of the image is 10
    
    image = sitk.Cast(image, sitk.sitkFloat32)
    image = image * 1.3
    print(np.max(sitk.GetArrayFromImage(image)))
    

    This would return 13