我有一个 DICOM 图像,我正在尝试将其从灰度转换为 RGB,以便与采用 RGB 图像的模型一起使用(输入形状必须为 (256, 256, 3))。
这是我使用tensorflow的grayscale_to_rgb函数加载图像的代码:
def load(image,label):
# Read and decode an image file to a uint16 tensor
image = tf.io.read_file(image)
image = tf.squeeze(tfio.image.decode_dicom_image(image, dtype=tf.uint16), axis=0)
# Convert image to float32 tensor
image = tf.cast(image, tf.float32)
image = tf.image.grayscale_to_rgb(image, name=None)
# Resize to uniform size (244,244)
image = tf.image.resize(image, [256,256], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
return image, label
当我尝试显示图像时,我得到以下信息:
image, label = load(path, label)
plt.figure()
plt.grid(False)
plt.imshow(image)
警告:matplotlib.image:使用 RGB 数据将输入数据剪切到 imshow 的有效范围(浮点数为 [0..1],整数为 [0..255])。
我做错了什么吗? 图像的形状为(256, 256, 3)。
如果我不将其转换为灰度,形状为 (256, 256, 1) 并且图像显示得非常好
提前谢谢您:)
灰度图像的位深度是
dtype=tf.uint16
,像素强度范围为 [0, 65535]。如果您想在 matplotlib.pyploy.imshow() 中显示 (M, N, 3) 图像,您需要将其缩小到范围 [0, 255] 的 8 位,或者将其转换为范围内的浮点数[0, 1].
对于 (M, N) 数据
imshow()
并不那么挑剔,并且会绘制任何标量数据。