【深度学习】TensorFlow图像处理

图像处理:

#coding=utf-8
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 sess:
    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 ())

# 打印图片
with tf.Session() as sess:
    plt.imshow(img_data.eval())
    plt.show()

# 重新调整图片大小
with tf.Session () as sess:
    resized = tf.image.resize_images (img_data, [300, 300], method=0)

    # TensorFlow的函数处理图片后存储的数据是float32格式的,需要转换成uint8才能正确打印图片。
    print("Digital type: ", resized.dtype)

    cat = np.asarray (resized.eval (), dtype='uint8')
    # tf.image.convert_image_dtype(rgb_image, tf.float32)
    plt.imshow (cat)
    plt.show ()

# 裁剪和填充图片
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()


# 截取中间50%的图片
with tf.Session() as sess:
    central_cropped = tf.image.central_crop(img_data, 0.5)
    plt.imshow(central_cropped.eval())
    plt.show()

# 翻转图片
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)


# 图片色彩调整
with tf.Session () as sess:
    # 将图片的亮度-0.5。
    # adjusted = tf.image.adjust_brightness(img_data, -0.5)

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

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

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

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

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

    plt.imshow (adjusted.eval ())
    plt.show ()


#  添加色相和饱和度
with tf.Session () as sess:
    adjusted = tf.image.adjust_hue (img_data, 0.1)
    # adjusted = tf.image.adjust_hue(img_data, 0.3)
    # adjusted = tf.image.adjust_hue(img_data, 0.6)
    # adjusted = tf.image.adjust_hue(img_data, 0.9)

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

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

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

    plt.imshow (adjusted.eval ())
    plt.show ()


# 添加标注框并裁减。
with tf.Session () as sess:
    boxes = tf.constant ([[[0.05, 0.05, 0.9, 0.7], [0.35, 0.47, 0.5, 0.56]]])

    begin, size, bbox_for_draw = tf.image.sample_distorted_bounding_box (
        tf.shape (img_data), bounding_boxes=boxes)

    batched = tf.expand_dims (tf.image.convert_image_dtype (img_data, tf.float32), 0)
    image_with_box = tf.image.draw_bounding_boxes (batched, bbox_for_draw)

    distorted_image = tf.slice (img_data, begin, size)
    plt.imshow (distorted_image.eval ())
    plt.show ()



一张图片产生多种随机训练数据:

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


# 随机调整图片的色彩,定义两种顺序。
def distort_color(image, color_ordering=0):
    if color_ordering == 0:
        image = tf.image.random_brightness(image, max_delta=32./255.)
        image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
        image = tf.image.random_hue(image, max_delta=0.2)
        image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
    else:
        image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
        image = tf.image.random_brightness(image, max_delta=32./255.)
        image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
        image = tf.image.random_hue(image, max_delta=0.2)

    return tf.clip_by_value(image, 0.0, 1.0)


#  对图片进行预处理,将图片转化成神经网络的输入层数据。
def preprocess_for_train(image, height, width, bbox):
    # 查看是否存在标注框。
    if bbox is None:
        bbox = tf.constant ([0.0, 0.0, 1.0, 1.0], dtype=tf.float32, shape=[1, 1, 4])
    if image.dtype != tf.float32:
        image = tf.image.convert_image_dtype (image, dtype=tf.float32)

    # 随机的截取图片中一个块。
    bbox_begin, bbox_size, _ = tf.image.sample_distorted_bounding_box (
        tf.shape (image), bounding_boxes=bbox,min_object_covered=0.1)
    bbox_begin, bbox_size, _ = tf.image.sample_distorted_bounding_box (
        tf.shape (image), bounding_boxes=bbox,min_object_covered=0.1)
    distorted_image = tf.slice (image, bbox_begin, bbox_size)

    # 将随机截取的图片调整为神经网络输入层的大小。
    distorted_image = tf.image.resize_images (distorted_image, [height, width], method=np.random.randint (4))
    distorted_image = tf.image.random_flip_left_right (distorted_image)
    distorted_image = distort_color (distorted_image, np.random.randint (2))
    return distorted_image


# 读取图片
image_raw_data = tf.gfile.FastGFile("../datasets/cat.jpg", "rb").read()
with tf.Session() as sess:
    img_data = tf.image.decode_jpeg(image_raw_data)
    boxes = tf.constant([[[0.05, 0.05, 0.9, 0.7], [0.35, 0.47, 0.5, 0.56]]])
    for i in range(9):
        result = preprocess_for_train(img_data, 299, 299, boxes)
        plt.imshow(result.eval())
        plt.show()


  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值