Tensorflow中将图像转化为张量

一、Tensorflow中读取图像步骤

1、通过函数 read_file 读取本地的图像文件
2、利用函数 decode_jpeg 解码为张量

二、以代码做实例的讲解

1、图像数据为猫的图片

2、代码讲解

# -*- coding: utf-8 -*-
import tensorflow as tf
import matplotlib.pylab as plt
# 读取数据文件
image = tf.read_file("C:/kitty.jpg", 'r')
# 将图像文件解码为Tensor
image_tensor = tf.image.decode_jpeg(image)
# 图像张量的形状
shape = tf.shape(image_tensor)
session = tf.Session()
print("图像的形状为:")
print(session.run(shape))
# 将tensor转换为ndarray
image_ndarray = image_tensor.eval(session=session)
# 显示图片
plt.imshow(image_ndarray)
plt.show()

图像的形状为:
[393 500   3]

可视化的结果为:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/a1786742005/article/details/84997631