如何在Keras中转换图像色彩空间? [英] How to convert images color space in Keras?

查看:183
本文介绍了如何在Keras中转换图像色彩空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将RGB彩色图像提供给使用Keras实现的神经网络.如何让Keras将图像转换为其他色彩空间(例如YUV,Lab或某些灰度)?

I am feeding RGB color images to a Neural Network implemented with Keras. How can I have Keras convert the images to a different color space (e.g. YUV, Lab, or some grayscale)?

我尝试了Lambda()层,但是出现了错误:

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

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

给我

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

我相信问题在于x是张量,并且我不知道如何将其转换为OpenCV接受的东西.

I believe the issue is that x is a Tensor, and I don't know how to convert it to something OpenCV accepts.

更好的是,如果我可以在GPU中完成它.例如.使用Tensorflow时,我将使用tf.image.rgb_to_hsv()tf.image.rgb_to_grayscale()之类的功能.

Even better, if I can have it done in the GPU instead. E.g. with Tensorflow I would use functions such as tf.image.rgb_to_hsv() and tf.image.rgb_to_grayscale().

谢谢!

推荐答案

如果导入tensorflow,则可以在lambda中使用tf.image.rgb_to_hsv()函数:

If you import tensorflow, you can use the tf.image.rgb_to_hsv() function in the lambda:

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

这篇关于如何在Keras中转换图像色彩空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆