python 单通道转3通道,tensorflow灰度图转RGB图

import numpy as np

with tf.Session():
	image, label = sess.run(next_batch)	# batch_size=1
	print(image.shape)	# [224, 224, 1]
	image = np.concatenate((image, image, image), axis=-1)
	print(image.shape)	# [224, 224, 3]
	# image有三个通道,每个通道都是原始的单通道的复制

tf.image.grayscale_to_rgb函数

tf.image.grayscale_to_rgb(
    images,
    name=None
)

定义

将一个或多个图像从灰度转换为RGB

输出与images相同DType和秩的张量.输出的最后一个维度的大小为3,包含像素的RGB值

参数

images:要转换的灰度张量,最后一个维度大小必须为1
name:操作的名称(可选)

返回值

转换后的灰度图像
# 举例
from PIL import Image,ImageChops,ImageEnhance
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np

if __name__ == "__main__":

    img_data = Image.open('grey.jpg', 'r')
    print(img_data.size)	# (1000,625)
    
    plt.subplot(1,2,1)
    plt.title("origin")
    plt.imshow(img_data)

    img = np.array(img_data)
    img = img.reshape(625,1000,1)
    img_tensor = tf.convert_to_tensor(img)
    img_tensor = tf.image.grayscale_to_rgb(img_tensor)

    sess = tf.Session()
    img = sess.run(img_tensor)
    print(img_tensor.shape)	# (625,1000,3)

    plt.subplot(1, 2, 2)
    plt.title("rgb")
    plt.imshow(img)
    plt.show()


在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章