深度学习框架Tensorflow学习与应用 图像数据处理之一


图像处理python有不少个库,由于最近在学习Tensorflow,因此就用Tensorlow作了一些小处理。python


一:图像编码处理

        图像在存储时不是直接记录图像中的每一个像素值,而是记录通过压缩编码后的结果。因此要将图像还原成矩阵,须要解码的过程。Tensorflow提供了对jpeg/png格式图像的编码或解码函数。api

import matplotlib.pyplot as plt
import tensorflow as tf

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

with tf.Session() as sess:
    # 将图片使用jppeg的格式解码从而获得图像对应的三维矩阵。tensorflow还提供了
    # tf.image.decode_png函数对png格式的图像进行解码,解码以后的结果为一个张量
    # ,在使用它的取值以前须要明确调用运行的过程
    image_data = tf.image.decode_jpeg(image_raw_data)

    # 输出解码的三维矩阵
    #eval() 其实就是tf.Tensor的Session.run() 的另一种写法
    print(image_data.eval())

    # 使用pyplot工具可视化获得的图像
    plt.imshow(image_data.eval())
    plt.show()

    #将数据转化成实数方便下面的样例程序对图像进行处理
    image_data = tf.image.convert_image_dtype(image_data,dtype=tf.uint8)

    #将表示一张图像的三维矩阵从新按照jpeg格式编码并存入文件中,打开这张像
    #能够获得和原始图像同样的图像
    encode_image = tf.image.encode_jpeg(image_data)
    with tf.gfile.GFile("out./01.jpg",'wb') as f:
        f.write(encode_image.eval())

这就是把图像另存了一下,就不贴图片了。
网络



二:图像大小调整

(一)

        咱们从网上获取到的数据集每每不符合咱们的需求,但神经网络的输入节点个数确是固定的,因此须要去先把图像的尺寸改成本身所须要的。Tensorflow给咱们提供了四种方法,它们被封装在tf.image.resize_images函数里。dom

resized = tf.image.resizes(image_data,[img_H,img_W],method=)函数

method取值  
0 双线性插值法
1 最近邻居法
2 双三次插值法
3 面积插值法

这四种方法的具体区别我也不是很清楚,若是有知道的,请赐教。工具

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np

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

with tf.Session() as sess:
    image_data = tf.image.decode_jpeg(image_raw_data)
    #method =0 双线性插值法
    #method = 1 ,最近邻居法
    #method = 2 ,双三次插值法
    #meithod = 3 面积插值法
    resized = tf.image.resize_images(image_data,[300,300],method=0)
    # 输出调整后图像的大小,此处的结果为(300,300,?)。表示图像的大小是300*300但图像的深度在没有明确设置以前会是问号。
    print(resized.get_shape())

    # TensorFlow的函数处理图片后存储的数据是float32格式的,
    # 须要转换成uint8才能正确打印图片。
    resized = np.asarray(resized.eval(), dtype='uint8')
    plt.imshow(resized)
    plt.show()

    #将表示一张图像的三维矩阵从新按照jpeg格式编码并存入文件中,打开这张像能够获得和原始图像同样的图像
    encode_image = tf.image.encode_jpeg(resized)

    with tf.gfile.GFile("out./02.jpg",'wb') as f:

        f.write(encode_image.eval())

(二)对处理的图像进行填充或裁剪与按照比例调整图像大小

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np

image_raw_data = tf.gfile.FastGFile('timg.jpg','rb').read()

with tf.Session() as sess:
     img_data = tf.image.decode_jpeg(image_raw_data)

     # tensorflow提供了api对图像进行裁剪或者填充
     # 若是目标图像小于原始图像,则自动在截取原始图像居中部分
     croped = tf.image.resize_image_with_crop_or_pad(img_data, 150, 150)
     plt.imshow(croped.eval())
     plt.show()

     # 若是目标图像大于原始图像,则自动在原始图像四周填充0
     padded = tf.image.resize_image_with_crop_or_pad(img_data, 600, 800)
     plt.imshow(padded.eval())
     plt.show()

    #tensorflow还支持比例调整
     #第二个参数为(0,1]之间的实数
     central_cropped = tf.image.central_crop(img_data, 0.5)
     plt.imshow(central_cropped.eval())
     plt.show()

     croped1 = tf.image.encode_jpeg(croped)
     with tf.gfile.GFile("out./02_crop.jpg", 'wb') as f:
         f.write(croped1.eval())

     padded1 = tf.image.encode_jpeg(padded)
     with tf.gfile.GFile("out./02_padded.jpg", 'wb') as f:
         f.write(padded1.eval())

     central_cropped1 = tf.image.encode_jpeg(central_cropped)
     with tf.gfile.GFile("out./02_central_cropped.jpg", 'wb') as f:
         f.write(central_cropped1.eval())

三:图像翻转

对图像进行翻转能够有效的扩充数据集.咱们能够将图像上下翻转,左右翻转以及沿对角线翻转。学习

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np

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

with tf.Session() as sess:
    image_data = tf.image.decode_jpeg(image_raw_data)

   #将图像上下翻转
    flipped = tf.image.flip_up_down(image_data)
    resized = np.asarray(flipped.eval(), dtype='uint8')
    plt.imshow(resized)
    plt.show()
    encode_image = tf.image.encode_jpeg(resized)
    with tf.gfile.GFile("out./03_up_down.jpg", 'wb') as f:
        f.write(encode_image.eval())

    #将图片左右翻转,
    flipped = tf.image.flip_left_right(image_data)
    resized = np.asarray(flipped.eval(), dtype='uint8')
    plt.imshow(resized)
    plt.show()
    encode_image = tf.image.encode_jpeg(resized)
    with tf.gfile.GFile("out./03_left_right.jpg", 'wb') as f:
        f.write(encode_image.eval())

    #将图片沿对角线翻转
    flipped = tf.image.transpose_image(image_data)
    resized = np.asarray(flipped.eval(), dtype='uint8')
    plt.imshow(resized)
    plt.show()
    encode_image = tf.image.encode_jpeg(resized)
    with tf.gfile.GFile("out./03_transpose_image.jpg", 'wb') as f:
        f.write(encode_image.eval())

    #将图片沿以必定几率上下翻转
    flipped = tf.image.random_flip_up_down(image_data)
    resized = np.asarray(flipped.eval(), dtype='uint8')
    plt.imshow(resized)
    plt.show()
    encode_image = tf.image.encode_jpeg(resized)
    with tf.gfile.GFile("out./03_random_flip_up_down.jpg", 'wb') as f:
        f.write(encode_image.eval())

    #将图片以必定几率
    flipped = tf.image.random_flip_left_right(image_data)
    resized = np.asarray(flipped.eval(), dtype='uint8')
    plt.imshow(resized)
    plt.show()
    encode_image = tf.image.encode_jpeg(resized)
    with tf.gfile.GFile("out./03_random_flip_left_right.jpg", 'wb') as f:
        f.write(encode_image.eval())