简体   繁体   English

有没有办法在“纯”Keras中将图像从灰度转换为RGB

[英]Is there a way to convert an image from grayscale to RGB in “pure” Keras

I'd like to know if there is a way to convert an image from grayscale to RGB in Python using "pure" Keras (ie without importing Tensorflow). 我想知道是否有办法使用“纯”Keras将图像从灰度转换为RGB(即不导入Tensorflow)。

What I do now is: 我现在做的是:

x_rgb = tf.image.grayscale_to_rgb(x_grayscale)

Maybe you would consider this "cheating" (as keras.backend may end up calling Tensorflow behind the scene), but here's a solution: 也许你会认为这是“作弊”(因为keras.backend最终可能会在幕后调用Tensorflow),但这是一个解决方案:

from keras import backend as K

def grayscale_to_rgb(images, channel_axis=-1):
    images= K.expand_dims(images, axis=channel_axis)
    tiling = [1] * 4    # 4 dimensions: B, H, W, C
    tiling[channel_axis] *= 3
    images= K.tile(images, tiling)
    return images

(supposing your grayscale images have a shape B x H x W and not eg B x H x W x 1 ; otherwise just remove the first line of the function) (假设您的灰度图像的形状为B x H x W而不是例如B x H x W x 1 ;否则只需删除函数的第一行)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM