TensorFlow学习--tensorflow图像处理--图像翻转及大小色彩调整

翻转图像

tf.image.flip_up_down()

将图像上下翻转

tf.image.flip_left_right()

将图像左右翻转

tf.image.transpose_image()

通过交换第一维和第二维来转置图像

随机翻转

tf.image.random_flip_up_down()
将图像上下翻转的概率为0.5,即输出的图像有50%的可能性是上下翻转的否则就输出原图.
tf.image.random_flip_left_right()
将图像左右翻转的概率为0.5,即输出的图像有50%的可能性是左右翻转的否则就输出原图

在训练图像时,利用随机翻转对图像进行预处理来增加训练数据.

#!/usr/bin/python
# coding:utf-8

import matplotlib.pyplot as plt
import tensorflow as tf
# 读取图像数据
img = tf.gfile.FastGFile('daibola.jpg').read()

with tf.Session() as sess:
    img_data = tf.image.decode_jpeg(img)
    # 将图像上下翻转
    flipped0 = tf.image.flip_up_down(img_data)
    # 将图像左右翻转
    flipped1 = tf.image.flip_left_right(img_data)
    # 通过交换第一维和第二维来转置图像
    flipped2 = tf.image.transpose_image(img_data)

    plt.subplot(221), plt.imshow(img_data.eval()), plt.title('original')
    plt.subplot(222), plt.imshow(flipped0.eval()), plt.title('flip_up_down')
    plt.subplot(223), plt.imshow(flipped1.eval()), plt.title('flip_left_right')
    plt.subplot(224), plt.imshow(flipped2.eval()), plt.title('transpose_image')

    plt.show()

输出:

这里写图片描述

调整图像大小

tf.image.resize_images()

tensorflow中函数tf.image.resize_images可以实现对图像大小的调整,对应的API为:

def resize_images(images,
                  size,
                  method=ResizeMethod.BILINEAR,
                  align_corners=False):

其中,method的取值与对应的插值算法为:

Methold取值图像大小调整算法
0双线性插值法(Bilinear interprolation)
1最临近插值法 (Nearest neighbor interprolation)
2双三次插值法 (Bicubic interprolation)
3面积插值法 (Area interprolation)
tf.image.crop_to_bounding_box()

API:

def crop_to_bounding_box(image, offset_height, offset_width, target_height,target_width)

将图像裁剪到指定的边界框
image:shape为[height, width, channels]的3维张量;shape为[batch, height, width, channels]的4维张量
offset_height:结果图像左上角点的垂直坐标
offset_width:结果图像左上角点的水平坐标
target_height:结果图像的高度
target_width:结果图像的宽度

tf.image.resize_image_with_crop_or_pad()

API:

def resize_image_with_crop_or_pad(image, target_height, target_width)

将图像裁剪或填充到目标宽度和高度。

tf.image.central_crop()

裁剪图像的中心区域,删除图像的外部部分,但保留图像的中心区域.

#!/usr/bin/python
# coding:utf-8

import matplotlib.pyplot as plt
import tensorflow as tf
# 读取图像数据
img = tf.gfile.FastGFile('daibola.jpg').read()

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

    resized0 = tf.image.resize_images(img_data, (700, 1000), method=1)
    resized1 = tf.image.crop_to_bounding_box(img_data, 30, 220, 300, 250)
    resized2 = tf.image.resize_image_with_crop_or_pad(img_data, 300, 300)
    resized3 = tf.image.resize_image_with_crop_or_pad(img_data, 1300, 1300)
    # 函数tf.image.central_crop可以通过比例调整图像的大小
    resized4 = tf.image.central_crop(img_data, 0.6)

    plt.subplot(231), plt.imshow(img_data.eval()), plt.title('original')
    plt.subplot(232), plt.imshow(resized0.eval())
    plt.subplot(233), plt.imshow(resized1.eval())
    plt.subplot(234), plt.imshow(resized2.eval())
    plt.subplot(235), plt.imshow(resized3.eval())
    plt.subplot(236), plt.imshow(resized4.eval())
    plt.show()

输出:

这里写图片描述

调整图像色彩

tf.image.adjust_brightness()

调整图像的亮度

tf.image.adjust_contrast()

调整图像的对比度

tf.image.adjust_hue()

调整图像的色相

tf.image.adjust_saturation()

调整图像的饱和度

tf.image.per_image_standardization()

将图像线性缩放为零均值和单位范数

随机调整

在指定范围内随机调整图像的亮度/对比度/色相/饱和度
tf.image.random_brightness(img_data,max_delta)
tf.image.random_contrast(img_data, lower, upper)
tf.image.random_hue(img_data, max_delta)
tf.image.random_saturation(img_data, lower, upper)
随机调整这些属性,使训练得到的模型尽可能小的受到无关因素的影响.

#!/usr/bin/python
# coding:utf-8

import matplotlib.pyplot as plt
import tensorflow as tf
# 读取图像数据
img = tf.gfile.FastGFile('daibola.jpg').read()

with tf.Session() as sess:
    img_data = tf.image.decode_jpeg(img)
    # 将图像的亮度-0.2
    adjusted0 = tf.image.adjust_brightness(img_data, -0.2)
    # 将图像的对比度+3
    adjusted1 = tf.image.adjust_contrast(img_data, +3)
    # 将图像的色相+0.2
    adjusted2 = tf.image.adjust_hue(img_data, 0.2)
    # 将图像的饱和度+3
    adjusted3 = tf.image.adjust_saturation(img_data, 3)
    # 将图像线性缩放为零均值和单位范数
    adjusted4 = tf.image.per_image_standardization(img_data)

    plt.subplot(231), plt.imshow(img_data.eval()), plt.title('original')
    plt.subplot(232), plt.imshow(adjusted0.eval()), plt.title('adjust_brightness')
    plt.subplot(233), plt.imshow(adjusted1.eval()), plt.title('adjust_contrast')
    plt.subplot(234), plt.imshow(adjusted2.eval()), plt.title('adjust_hue')
    plt.subplot(235), plt.imshow(adjusted3.eval()), plt.title('adjust_saturation')
    plt.subplot(236), plt.imshow(adjusted4.eval()), plt.title('per_image_standardization')

    plt.show()

输出:

这里写图片描述

  • 5
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值