TensorFlow零基础入门教程(三)——图像处理

图像作为一个像素矩阵,TensorFlow提供了多个函数用于图像处理。TensorFlow的图像处理部分和OpenCV不同,主要服务于深度学习。比如图像的旋转不变性等等。在功能上肯定不如OpenCV丰富。

1、图像编码处理

虽然图片就是一个三维矩阵,但是所有的图片存储时都会被压缩,也就是编码和解码的过程。所以我们从一张图像得到三维矩阵,首先需要解码。

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

# 对图片进行解码,并且修改其大小,将其打印出来(矩阵值)
image_raw_data = tf.gfile.FastGFile('datasets/cat.jpg','rb').read()
with tf.Session() as sess1:
    img_data = tf.image.decode_jpeg(image_raw_data)
    print(img_data.eval())
    img_data.set_shape([1797, 2673, 3])
    print(img_data.get_shape())

# 利用matplotlib,打印图片
with tf.Session() as sess2:
    plt.imshow(img_data.eval())
    print(img_data.get_shape())
    plt.show()

image.png

2、图像大小调整

对图像的大小进行调整,TensorFlow提供了多种方法,现总结如下。

2.1 放大 缩小

# 对图片进行放大缩小处理,原图信息不变,但是尺寸发生变化
with tf.Session() as sess3:
    # 首先将图像的像素值从0-255,变成浮点数 0-1
    image_float = tf.image.convert_image_dtype(img_data, tf.float32)
    resized = tf.image.resize_images(image_float, [300,300], method=0)

    plt.imshow(resized.eval())
    plt.show()

image.png

调整大小算法(method):
  • method = 0:双线性插值;
  • method = 1:最近邻插值;
  • method = 2:双三次插值;
  • method = 3:面积插值法;

2.2 裁剪 填充

对其进行裁剪或者填充.但是原图像的大小并没有变.

with tf.Session() as sess:    
    croped = tf.image.resize_image_with_crop_or_pad(img_data, 1000, 1000)
    padded = tf.image.resize_image_with_crop_or_pad(img_data, 3000, 3000)
    plt.imshow(croped.eval())
    plt.show()
    plt.imshow(padded.eval())
    plt.show()

image.png
image.png

2.3 按比例调整大小

截取中间50%的图片

with tf.Session() as sess:   
    central_cropped = tf.image.central_crop(img_data, 0.5)
    plt.imshow(central_cropped.eval())
    plt.show()

image.png

3、图像翻转

对图像进行上下,左右的翻转。

with  tf.Session() as sess: 
    # 上下翻转
    #flipped1 = tf.image.flip_up_down(img_data)
    # 左右翻转
    #flipped2 = tf.image.flip_left_right(img_data)

    #对角线翻转
    transposed = tf.image.transpose_image(img_data)
    plt.imshow(transposed.eval())
    plt.show()

    # 以一定概率上下翻转图片。
    #flipped = tf.image.random_flip_up_down(img_data)
    # 以一定概率左右翻转图片。
    #flipped = tf.image.random_flip_left_right(img_data)

image.png

4、对图像进行色彩调整


with  tf.Session() as sess:
    # 在进行一系列图片调整前,先将图片转换为实数形式,有利于保持计算精度。
    image_float = tf.image.convert_image_dtype(img_data, tf.float32)

    # 将图片的亮度-0.5。
    #adjusted = tf.image.adjust_brightness(image_float, -0.5)

    # 将图片的亮度-0.5
    #adjusted = tf.image.adjust_brightness(image_float, 0.5)

    # 在[-max_delta, max_delta)的范围随机调整图片的亮度。
    adjusted = tf.image.random_brightness(image_float, max_delta=0.5)

    # 将图片的对比度-5
    #adjusted = tf.image.adjust_contrast(image_float, -5)

    # 将图片的对比度+5
    #adjusted = tf.image.adjust_contrast(image_float, 5)

    # 在[lower, upper]的范围随机调整图的对比度。
    #adjusted = tf.image.random_contrast(image_float, lower, upper)

    # 在最终输出前,将实数取值截取到0-1范围内。
    adjusted = tf.clip_by_value(adjusted, 0.0, 1.0)
    plt.imshow(adjusted.eval())
    plt.show()

image.png

5、调整图像色相和饱和度

with tf.Session() as sess:
    # 在进行一系列图片调整前,先将图片转换为实数形式,有利于保持计算精度。
    image_float = tf.image.convert_image_dtype(img_data, tf.float32)

    adjusted = tf.image.adjust_hue(image_float, 0.1)
    #adjusted = tf.image.adjust_hue(image_float, 0.3)
    #adjusted = tf.image.adjust_hue(image_float, 0.6)
    #adjusted = tf.image.adjust_hue(image_float, 0.9)

    # 在[-max_delta, max_delta]的范围随机调整图片的色相。max_delta的取值在[0, 0.5]之间。
    #adjusted = tf.image.random_hue(image_float, max_delta)

    # 将图片的饱和度-5。
    #adjusted = tf.image.adjust_saturation(image_float, -5)
    # 将图片的饱和度+5。
    #adjusted = tf.image.adjust_saturation(image_float, 5)
    # 在[lower, upper]的范围随机调整图的饱和度。
    #adjusted = tf.image.random_saturation(image_float, lower, upper)

    # 将代表一张图片的三维矩阵中的数字均值变为0,方差变为1。
    #adjusted = tf.image.per_image_whitening(image_float)

    # 在最终输出前,将实数取值截取到0-1范围内。
    adjusted = tf.clip_by_value(adjusted, 0.0, 1.0)
    plt.imshow(adjusted.eval())
    plt.show()

image.png

6、对图片增加标注框

# 在图片中添加标注框,和裁剪
with tf.Session() as sess8:
    # 生成标准框
    # 设置标注框的位置
    boxes = tf.constant([[[0.05, 0.05, 0.9, 0.7], [0.35, 0.47, 0.5, 0.56]]])

    # 图片太大,标注框无法显示,所以将图片变小
    image_small = tf.image.resize_images(image_float, [180, 267], method=0)
    # 对数据进行扩充,将其变为4维矩阵
    batchced_img = tf.expand_dims(image_small, 0)
    # draw_bounding_boxes处理多张图片,所以是一个四维矩阵,需要对原图像进行扩充
    image_with_box = tf.image.draw_bounding_boxes(batchced_img, boxes)

    plt.imshow(image_with_box[0].eval())
    plt.show()

    # 对原图进行随机裁剪,但是至少覆盖标注框boxes,40%的内容
    begin, size, bbox_for_draw = tf.image.sample_distorted_bounding_box(
        tf.shape(image_float), bounding_boxes=boxes, min_object_covered=0.4
    )
    print(begin)
    print(size)
    distorted_image = tf.slice(image_float, begin, size)
    plt.imshow(image_with_box[0].eval())
    plt.show()

image.png

猜你喜欢

转载自blog.csdn.net/linxid/article/details/80645162