zoukankan      html  css  js  c++  java
  • TensorFlow学习笔记之图像大小调整(最全可运行的代码)

    一、图像编码处理

    在之前的章节中提到一张RGBRGB色彩模式的图像可以看成一个三维矩阵,矩阵中的每一个数表示了图像上不同位置,不同颜色的亮度。然而图像在存储时并不是直接记录这些矩阵中的数字,而是记录经过压缩编码之后的结果。所以要将一张图像还原成一个三维矩阵,需要解码的过程。TensorFIowTensorFIow提供了对jpegjpegpngpng格式图像的编码/解码函数。以下代码示范了如何使用TensorFlowTensorFlow中对jpegjpeg格式图像的编码/解码函数。

    import tensorflow as tf
    import matplotlib.pyplot as plt
    
    # 读取图像的原始数据
    image_raw_data = tf.gfile.FastGFile("./picture/2.jpeg", 'rb').read()
    
    with tf.Session() as sess:
        # 将图像使用jpeg的格式解码从而得到图相对应的三维矩阵。
        # TensorFlow还提供了tf.image.decode_png函数对png格式的图像进行解码。
        # 解码之后的结果为一个张量,在使用它的取值之前需要明确调用运行的过程。
        img_data = tf.image.decode_jpeg(image_raw_data)
    
        # 使用pyplot工具可视化得到的图像。
        plt.imshow(img_data.eval())
        plt.show()
    
        # 将数据的类型转换成实数。
        img_data = tf.image.convert_image_dtype(img_data, dtype=tf.float32)
    
        # 将表示一张图像的三维矩阵重新按照jpeg格式编码并存入文件中。
        # 打开这张图像,可以得到和原始图像一样的图像。
        encode_image = tf.image.encode_jpeg(img_data)
        with tf.gfile.GFile("/path/to/output", "wb") as f:
            f.write(encode_image.eval())
    

    在这里插入图片描述在这里插入图片描述

    二、图像大小调整

    一般来说,网络上获取的图像大小是不固定的,但神经网络输入节点的个数是固定的。所以在将图像的像素作为输入提供给神经网络之前,需要先将图像的大小统一。这就是图像大小调整需要完成的任务。图像大小调整有两种方式,第一种是通过算法使得新的图像尽量保存原始图像上的所有信息。TensorFlowTensorFlow提供了匹种不同的方法,并且将它们封装到
    tf.image.resize_images函数。以下代码示范了如何使用这个函数。

    import tensorflow as tf
    import matplotlib.pyplot as plt
    
    # 读取图像的原始数据
    image_raw_data = tf.gfile.FastGFile("./picture/2.jpeg", 'rb').read()
    
    with tf.Session() as sess:
        # 将图像使用jpeg的格式解码从而得到图相对应的三维矩阵。
        # TensorFlow还提供了tf.image.decode_png函数对png格式的图像进行解码。
        # 解码之后的结果为一个张量,在使用它的取值之前需要明确调用运行的过程。
        img_data = tf.image.decode_jpeg(image_raw_data)
    
        # 将数据的类型转换成实数。
        img_data = tf.image.convert_image_dtype(img_data, dtype=tf.float32)
    
        # 通过tf.image.resize_images函数调整图像的大小。这个函数第一个参数为原始图像,
        # 第二个为调整后图像的大小,method参数给出了调整图像大小的算法。
        resized = tf.image.resize_images(img_data, [400, 400], method=0)
    
        # 输出调整后的图像的大小,此处的结果为(?,?,?)。表示图像的的大小是400x400,
        # 但图像的深度在没有明确设置之前会是问号。
        print(img_data.get_shape())
    
        # 使用pyplot工具可视化得到的图像。
        plt.imshow(resized.eval())
        plt.show()
    
    > (?, ?, ?)
    

    这里tf.image.resize_images函数中的method参数取值与相对应的图像大小调整算法。

    method取值 图像大小调整算法
    0 双线性插值法(Bilinearinterpolation)
    1 最近邻居法(Nearestneighborinterpolation)
    2 双三次插值法(Bicubicinterpolation)
    3 面积插值法(Areainterpolation)

    双线性插值法:

    在这里插入图片描述

    最近邻居法:

    在这里插入图片描述

    双三次插值法:

    运行该方法时会报错,需要调整代码。

    报的错误:

    ValueError: Floating point image RGB values must be in the 0..1 range.
    

    更改后的代码:

    with tf.Session() as sess:
    。。。
    。。。
    	# 输出调整后的图像的大小,此处的结果为(?,?,?)。表示图像的的大小是300x300,
        # 但图像的深度在没有明确设置之前会是问号。
        print(img_data.get_shape())
        
        after_img = tf.clip_by_value(resized, 0.0, 1.0)
    
        # 使用pyplot工具可视化得到的图像。
        plt.imshow(after_img.eval())
        # plt.imshow(resized.eval())
        plt.show()
    

    在这里插入图片描述

    面积差值法:

    在这里插入图片描述
    从上面的图中可以看出,不同算法去调整出来的结果会有细微差别,但不会相差太远。除了将整张图像信息完整保存,TensorFlowTensorFlow还提供了APIAPI对图像进行裁剪或者填充。以下代码展示了通过tf.image.resize-mage-with-crop-or_pad函数来调整图像大小的功能。

    import tensorflow as tf
    import matplotlib.pyplot as plt
    
    # 读取图像的原始数据
    image_raw_data = tf.gfile.FastGFile("./picture/2.jpeg", 'rb').read()
    
    with tf.Session() as sess:
        # 将图像使用jpeg的格式解码从而得到图相对应的三维矩阵。
        # TensorFlow还提供了tf.image.decode_png函数对png格式的图像进行解码。
        # 解码之后的结果为一个张量,在使用它的取值之前需要明确调用运行的过程。
        img_data = tf.image.decode_jpeg(image_raw_data)
    
        # 将数据的类型转换成实数。
        img_data = tf.image.convert_image_dtype(img_data, dtype=tf.float32)
    
        # 使用pyplot工具可视化得到的图像。
        plt.imshow(img_data.eval())
        plt.show()
    
        # 通过tf.image.resize_image_with_crop_or_pad函数调整图像的大小。
        # 这个函数的第一个参数为原始图像,后面两个参数是调整后的目标图像大小。
        # 如果原始图像的尺寸大于目标图像,那么这个函数会自动截取原始图像中居中的部分。
        # 如果目标图像大于原始图像,这个函数会自动在原始图像的四周填充全0背景。
        croped = tf.image.resize_image_with_crop_or_pad(img_data, 500, 500)
        padded = tf.image.resize_image_with_crop_or_pad(img_data, 2000, 2000)
    
        # 使用pyplot工具可视化得到的图像。
        plt.imshow(croped.eval())
        plt.show()
        plt.imshow(padded.eval())
        plt.show()
    

    原始图像:

    在这里插入图片描述

    croped——500x500图像:

    在这里插入图片描述

    padded——2000x2000图像:

    在这里插入图片描述

    如果想要更多的资源,欢迎关注 @我是管小亮,文字强迫症MAX~

    回复【福利】即可获取我为你准备的大礼,包括C++,编程四大件,NLP,深度学习等等的资料。

    想看更多文(段)章(子),欢迎关注微信公众号「程序员管小亮」~

    在这里插入图片描述

  • 相关阅读:
    Java的代理proxy机制
    Java 编译器 javac 笔记:javac API、注解处理 API 与 Lombok 原理
    Java 运行时获取方法参数名
    python 之 list 和 dict 置空
    Python 模块(Module)调用
    Python 模块(Module)及包
    Mybatis中进行批量更新(updateBatch)
    可持久化线段树+主席树+动态主席树
    无向图的割点与割边
    [NOIP2016] 愤怒的小鸟 题解
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13302808.html
Copyright © 2011-2022 走看看