简体   繁体   English

Tensorflow:如果灰度,将图像转换为 rgb

[英]Tensorflow: Convert image to rgb if grayscale

I have a dataset of rgb and grayscale images.我有一个 rgb 和灰度图像数据集。 While iterating over the dataset, I want to detect if the image is a grayscale image such that I can convert it to rgb.在迭代数据集时,我想检测图像是否是灰度图像,以便我可以将其转换为 rgb。 I wanted to use tf.shape(image) to detect the dimensions of the image.我想使用tf.shape(image)来检测tf.shape(image)的尺寸。 For a rgb image I get something like [1, 100, 100, 3] .对于 rgb 图像,我得到类似[1, 100, 100, 3] For grayscale images the function returns for example [1, 100, 100] .对于灰度图像,该函数返回例如[1, 100, 100] I wanted to use len(tf.shape(image)) to detect if it is of length 4 (=rgb) or length 3 (=grayscale).我想使用len(tf.shape(image))来检测它的长度是 4 (=rgb) 还是长度 3 (=grayscale)。 That did not work.那没有用。

This is my code so far which did not work:这是我到目前为止不起作用的代码:

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

Is there an alternative way to convert grayscale images to rgb?是否有将灰度图像转换为 rgb 的替代方法?

You can use a function like this for that:您可以使用这样的函数:

import tensorflow as tf

def process_image(image):
    image = tf.convert_to_tensor(image, dtype=tf.uint8)
    image_rgb =  tf.cond(tf.rank(image) < 4,
                         lambda: tf.image.grayscale_to_rgb(tf.expand_dims(image, -1)),
                         lambda: tf.identity(image))
    # Add shape information
    s = image.shape
    image_rgb.set_shape(s)
    if s.ndims is not None and s.ndims < 4:
        image_rgb.set_shape(s.concatenate(3))
    return image_rgb

I had a very similar problem, I wanted to load rgb and greyscale images in one go.我有一个非常相似的问题,我想一次性加载 rgb 和灰度图像。 Tensorflow supports setting the channel number when reading in the images. Tensorflow 支持在读取图像时设置通道号。 So if the images have different numbers of channels, this might be what you are looking for:因此,如果图像具有不同数量的通道,这可能就是您要查找的内容:

# to get greyscale:
tf.io.decode_image(raw_img, expand_animations = False, dtype=tf.float32, channels=1)

# to get rgb:
tf.io.decode_image(raw_img, expand_animations = False, dtype=tf.float32, channels=3)

-> You can even do both on the same image and inside tf.data.Dataset mappings! -> 您甚至可以在同一图像上和tf.data.Dataset映射中同时进行!

You now have to set the channels variable to match the shape you need, so all the loaded images will be of that shape.您现在必须设置channels变量以匹配您需要的形状,因此所有加载的图像都将具有该形状。 Than you could reshape without a condition.比你可以在没有条件的情况下重塑。

This also allows you to directly load a grayscale image to RGB in Tensorflow.这也允许您在 Tensorflow 中直接将灰度图像加载到 RGB。 Here an example:这里有一个例子:

    >> a = Image.open(r"Path/to/rgb_img.JPG")
    >> np.array(a).shape
    (666, 1050, 3)
    >> a = a.convert('L')
    >> np.array(a).shape
    (666, 1050)
    >> b = np.array(a)
    >> im = Image.fromarray(b) 
    >> im.save(r"Path/to/now_it_is_greyscale.jpg")
    >> raw_img = tf.io.read_file(r"Path/to/now_it_is_greyscale.jpg")
    >> img = tf.io.decode_image(raw_img, dtype=tf.float32, channels=3)
    >> img.shape
    TensorShape([666, 1050, 3])
    >> img = tf.io.decode_image(raw_img, dtype=tf.float32, channels=1)
    >> img.shape
    TensorShape([666, 1050, 1])

Use expand_animations = False if you get ValueError: 'images' contains no shape.如果出现ValueError: 'images' contains no shape.请使用expand_animations = False ValueError: 'images' contains no shape. ! See: https://stackoverflow.com/a/59944421/9621080参见: https : //stackoverflow.com/a/59944421/9621080

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

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