How to convert image color spaces in Keras?

I feed RGB color images to a neural network implemented with Keras. How can I convert Keras images to another color space (for example, YUV, Lab or some shades of gray)?

I tried with a layer Lambda()but got an error:

model.add(Lambda(lambda x: cv2.cvtColor(x, cv2.COLOR_RGB2LAB), input_shape=(160, 320, 3)))

gave me

TypeError: src is not a numpy array, neither a scalar

I believe the problem is what xis a tensor, and I don’t know how to convert it to what OpenCV accepts.

Even better if I can do it in the GPU. For instance. with Tensorflow, I would use features like tf.image.rgb_to_hsv()and tf.image.rgb_to_grayscale().

Thank!

+4
source share
1 answer

shadoworflow, tf.image.rgb_to_hsv() :

def hsv_conversion(x):
    import tensorflow as tf    
    return tf.image.rgb_to_hsv(x)

model.add(Lambda(hsv_conversion, input_shape=(160, 320, 3)))
+1

Source: https://habr.com/ru/post/1673247/


All Articles