简体   繁体   中英

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

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:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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