数据增强

本文详细介绍了使用TensorFlow进行图像增强的方法,包括随机翻转、调整亮度和对比度等操作,适用于深度学习图像预处理阶段,提升模型泛化能力。

1.tf.image.random_flip_left_right(image,seed=None)为随机翻转函数,注意这里的image有三个维度[height, width, channels]。还有一点就是此函数是在width方向上随机翻转的,有可能结果就是从左往右翻转,从左往左翻转即没有翻转。

翻转图像
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%的可能性是左右翻转的否则就输出原图

2.tf.image.random_brightness(image,max_delta,seed=None)为随机调整亮度函数,实际上是在原图的基础上随机加上一个值(如果加上的是正值则增亮否则增暗),此值取自[-max_delta,max_delta),要求max_delta>=0。
3.tf.image.random_contrast(image,lower,upper,seed=None)为随机调整对比度函数,对比度调整值取自[lower,upper],

image = img.imread(‘D:/Documents/Pictures/logo4.jpg’)
flipped_image = tf.image.random_flip_left_right(image)#随机翻转函数

imaged = tf.cast(image,tf.float32) #这句必须加上
brightness_image = tf.image.random_brightness(imaged,max_delta=63)#随机调整亮度函数

imaged = tf.cast(image,tf.float32)
contrast_image = tf.image.random_contrast(imaged,lower=0.2,upper=1.8)#随机调整对比度函数

#!/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()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值