如何将一个简单 ITK 图像的所有像素乘以一个标量?

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

我正在为 Python 使用 SimpleITK 包。我正在尝试将使用 ImageFileReader class 构造的 Image 乘以一个标量值。我试过使用 * 运算符和 Multiply 函数无济于事。两者都return最大值为0.0

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

这个returns 0

我也尝试过 MultiplyImageFilter。

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

这也是returns 0

有没有简单的方法来缩放我丢失的图像强度?

您需要在 ImageFileReader 上调用 Update,结果才有效。您编写的代码应该可以工作,至少是第二个变体。

我意识到了这个挑战。生成图像时,像素表示是一个无符号整数。我乘以一个浮点值,这导致所有像素由于四舍五入而归零。要解决此问题,请将图像转换为浮点类型,然后应用基本乘法。

例如...

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)))

这会 return 13