我有一组rgb和灰度图像的数据集。在迭代整个数据集时,我想要检测图像是否是灰度图像,以便将其转换为rgb格式。我想使用
以下是我的代码,但并未实现想要的效果:
有没有另一种方法将灰度图像转换为RGB?
tf.shape(image)
来检测图像的维度。对于rgb图像,我得到类似于[1, 100, 100, 3]
的结果。对于灰度图像,该函数返回例如[1, 100, 100]
。我想使用len(tf.shape(image))
来检测它的长度,如果长度为4,则表示rgb图像,长度为3则表示灰度图像。但这并不起作用。以下是我的代码,但并未实现想要的效果:
def process_image(image):
# Convert numpy array to tensor
image = tf.convert_to_tensor(image, dtype=tf.uint8)
# Take care of grayscale images
dims = len(tf.shape(image))
if dims == 3:
image = np.expand_dims(image, axis=3)
image = tf.image.grayscale_to_rgb(image)
return image
有没有另一种方法将灰度图像转换为RGB?
tf.io.decode_image(raw_img, expand_animations=False, dtype=tf.float32, channels=3)
直接将灰度图像加载到 RGB 图像中? - Gilfoyle