图像处理:大小调整(tf.image.resize_images)

改变图片尺寸的大小使用的是 tf.image.resize_images

    img_resized = tf.image.resize_images(image_data, [300, 300], method=0)

通过tf.image.resize_images函数调整图像的大小。这个函数第一个参数为原始图像,第二个和第三个分别为调整后图像的大小,method参数给出了调整图像大小的算法
method = 0 双线性插值法
method = 1 最近邻居法
method = 2 双三次插值法
method = 3 面积插值法

"""
图片 处理
by song
stay calm stay peace
"""

# matplotlib.pyplot 是一个python的画图工具。可以可视化tensorflow对图像的处理过程
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np

# 读取图像的原始数据
image_raw_data = tf.gfile.FastGFile('/lena.jpg', 'rb').read()

with tf.Session() as sess:
    # 将图像使用JPEG的格式解码从而得到图像对应的三维矩阵。Tensorflow还提供了 tf.image.decode_png函数对png格式的图像进行编码。
    # 解码之后的结果为一个张量, 在使用他的取值之前需要明确调用运行的过程。
    image_data = tf.image.decode_jpeg(image_raw_data)
    # Decode a JPEG-encoded image to a uint8 tensor 所以这里的 image_data 已经是一个tsnsor

    # 通过tf.image.resize_images函数调整图像的大小。这个函数第一个参数为原始图像,第二个和第三个分别为调整后图像的大小,
    # method参数给出了调整图像大小的算法
    img_resized = tf.image.resize_images(image_data, [300, 300], method=0)

    # 输出调整后图像的大小,此处的结果为(300,300,?)。表示图像的大小是300*300,但是图像深度在没有明确设置之前会是问号。
    print(img_resized.get_shape())

    # # 如何显示改变大小以后的照片
    # img_resized = np.asarray(img_resized.eval(), dtype='uint8')  # 变为uint8才能显示
    # # 通过pyplot可视化过程
    # plt.imshow(img_resized)
    # plt.show()

猜你喜欢

转载自blog.csdn.net/qq_43225437/article/details/88018657