• TensorFlow 图像预处理(一) 图像编解码,图像尺寸调整


    TensorFlow提供了几类图像处理函数,下面介绍图像的编码与解码,图像尺寸调整。

    编码与解码

    图像解码与编码:一张RGB三通道的彩色图像可以看成一个三维矩阵,矩阵中的不位置上的数字代表图像的像素值。然后图像在存储时并不是直接记录这些矩阵中的数字,而是经过了压缩编码。所以将一张图像还原成一个三维矩阵的过程就是解码的过程,反之就是编码了。其实如果大家熟悉opencv的话,imread和imwrite就是一个解码和编码的过程。
    TensorFlow提供了常用图片格式的解码和编码操作,下面用一个jpg的图像演示:

    import matplotlib.pyplot as plt
    import tensorflow as tf
    
    
    image_raw_data = tf.gfile.FastGFile('.//image//1.jpg','rb').read()
    
    with tf.Session() as sess:
         img_data = tf.image.decode_jpeg(image_raw_data)
         print(img_data.eval())
    
         plt.imshow(img_data.eval())
         plt.show()
    
         #img_data = tf.image.convert_image_dtype(img_data,dtype = tf.float32)
    
         encoded_image = tf.image.encode_jpeg(img_data)
         with tf.gfile.GFile(".//image//3.jpg","wb") as f:
              f.write(encoded_image.eval())
    

    其中:
    decode_jpeg函数为jpeg(jpg)图片解码的过程,对应的encode_jpeg函数为编码过程,编码后将图片重命名写入到指定的路径下。

    图像尺寸调整
    图像尺寸调整属于基础的图像几何变换,TensorFlow提供了几种尺寸调整的函数:
    tf.image.resize_images:将原始图像缩放成指定的图像大小,其中的参数method(默认值为ResizeMethod.BILINEAR)提供了四种插值算法,具体解释可以参考图像几何变换(缩放、旋转)中的常用的插值算法
    tf.image.resize_image_with_crop_or_pad:剪裁或填充处理,会根据原图像的尺寸和指定的目标图像的尺寸选择剪裁还是填充,如果原图像尺寸大于目标图像尺寸,则在中心位置剪裁,反之则用黑色像素填充。
    tf.image.central_crop:比例调整,central_fraction决定了要指定的比例,取值范围为(0,1],该函数会以中心点作为基准,选择整幅图中的指定比例的图像作为新的图像。

    import matplotlib.pyplot as plt
    import tensorflow as tf
    import numpy as np
    
    image_raw_data = tf.gfile.FastGFile('.//image//1.jpg','rb').read()
    
    with tf.Session() as sess:
         img_data = tf.image.decode_jpeg(image_raw_data)
         plt.imshow(img_data.eval())
         plt.show()
    
    
         resized = tf.image.resize_images(img_data, [100, 100], method=0)
         # TensorFlow的函数处理图片后存储的数据是float32格式的,需要转换成uint8才能正确打印图片。
         print("Digital type: ", resized.dtype)
         resized = np.asarray(resized.eval(), dtype='uint8')
         # tf.image.convert_image_dtype(rgb_image, tf.float32)
         plt.imshow(resized)
         plt.show()
    
         croped = tf.image.resize_image_with_crop_or_pad(img_data, 100, 100)
         padded = tf.image.resize_image_with_crop_or_pad(img_data, 500, 500)
         plt.imshow(croped.eval())
         plt.show()
         plt.imshow(padded.eval())
         plt.show()
    
         central_cropped = tf.image.central_crop(img_data, 0.5)
         plt.imshow(central_cropped.eval())
         plt.show()

    原图:
    这里写图片描述

    resize_images(img_data, [100, 100], method=0):
    这里写图片描述

    resize_image_with_crop_or_pad(img_data, 100, 100):
    这里写图片描述

    resize_image_with_crop_or_pad(img_data, 500, 500):
    这里写图片描述

    central_crop(img_data, 0.5):
    这里写图片描述

    另外可以看 http://www.360doc.com/content/17/0513/14/10408243_653519828.shtml

    tensorflow里面提供了实现图像进行裁剪和填充的函数,就是tf.image.resize_image_with_crop_or_pad(img,height,width )。img表示需要改变的图像,height是改变后图像的高度,width是宽度。

    例如:

    [python] view plain copy
    1. import matplotlib.pyplot as plt;  
    2. import tensorflow as tf;  
    3.   
    4. image_raw_data_jpg = tf.gfile.FastGFile('11.jpg', 'r').read()  
    5.   
    6. with tf.Session() as sess:  
    7.     img_data_jpg = tf.image.decode_jpeg(image_raw_data_jpg)  
    8.     img_data_jpg = tf.image.convert_image_dtype(img_data_jpg, dtype=tf.float32)  
    9.     crop = tf.image.resize_image_with_crop_or_pad(img_data_jpg, 500, 500)  
    10.     pad = tf.image.resize_image_with_crop_or_pad(img_data_jpg, 2000, 2000)  
    11.   
    12.     plt.figure(1)  
    13.     plt.imshow(crop.eval())  
    14.     plt.figure(2)  
    15.     plt.imshow(pad.eval())  
    16.     plt.show()  

    结果:

  • 相关阅读:
    mybatis3.0-[topic10-14] -全局配置文件_plugins插件简介/ typeHandlers_类型处理器简介 /enviroments_运行环境 /多数据库支持/mappers_sql映射注册
    _MyBatis3-topic06.07.08.09_ 全局配置文件_引入dtd约束(xml提示)/ 引入properties引用/ 配置驼峰命名自动匹配 /typeAliases起别名.批量起别名
    MyBatis3-topic04,05 -接口式编程
    JDBC终章- 使用 DBUtils实现增删查改- C3P0Utils数据源/QueryRunner runner连接数据源并执行sql
    [课本10.1.4]JDBC数据库连接池- C3P0数据源--通过构造方法创建数据源对象--通过配置文件创建数据源对象[推荐]
    JDBC课程5--利用反射及JDBC元数据(ResultSetMetaData)编写通用的查询方法
    JDBC课程4--使用PreparedStatement进行增删查改--封装进JDBCTools的功能中;模拟SQL注入 ; sql的date()传入参数值格式!
    本周进度总结
    本周进度总结
    大道至简读后感
  • 原文地址:https://www.cnblogs.com/bonelee/p/8932124.html
Copyright © 2020-2023  润新知