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
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: tensorflow cifar-10-batches-py是一个经典的深度学习数据集,被广泛用于图像分类任务的训练和评估。 该数据集是CIFAR-10数据集的Python版本,提供了10个类别的60000个32x32彩色图像。其中,50000张图像作为训练集,10000张图像作为测试集。 这个数据集是用Python编写的,并且使用了pickle库来加载和处理数据。它可以通过执行"import cifar10"来导入,并使用"cifar10.load_data()"来加载其数据。 加载数据后,可以使用TensorFlow来构建一个图像分类模型。TensorFlow是一个开源的深度学习框架,可以用于构建、训练和评估机器学习模型。 使用tensorflow cifar-10-batches-py数据集,可以进行图像分类任务的实验和研究。可以结合卷积神经网络等深度学习模型,对图像进行特征提取和分类。 在训练模型时,可以使用训练集进行权重更新和优化,然后使用测试集来评估模型的性能。 总结来说,tensorflow cifar-10-batches-py是一个常用的深度学习数据集,可以用于图像分类任务的研究和实验。它结合了TensorFlow框架,提供了加载、处理和评估数据的功能。通过使用它,可以建立一个自定义的图像分类模型,并对其进行训练和评估。 ### 回答2: tensorflow cifar-10-batches-py是一个用于在tensorflow框架中处理CIFAR-10数据集的Python脚本。CIFAR-10数据集是一个广泛应用于图像分类的数据集,包含10个不同类别的影像数据,每个类别有6000个32x32大小的彩色图像。 这个Python脚本通过提供一些函数和类来加载CIFAR-10数据集,并且将图像和标签进行预处理,以便于在训练和测试模型时使用。脚本中的函数可以帮助我们将原始的二进制数据转换成可用于训练的张量形式。 该脚本提供的函数可以将CIFAR-10数据集分为训练集和测试集,并提供了一个函数用于获取下一个训练批或测试批的图像和标签。此外,该脚本还提供了一个函数用于显示CIFAR-10数据集中的图像。 使用tensorflow cifar-10-batches-py脚本,我们可以很方便地加载和预处理CIFAR-10数据集,并用于训练和测试图像分类模型。这个脚本是使用Python编写的,可以在tensorflow环境中直接使用。 ### 回答3: TensorFlow的cifar-10-batches-py是一个用于训练和验证图像分类模型的数据集。它是基于CIFAR-10数据集的一个版本,其中包含50000张用于训练的图像和10000张用于验证的图像。 CIFAR-10数据集是一个常用的图像分类数据集,包含10个不同的类别,每个类别有大约6000张图像。这些类别包括:飞机、汽车、鸟类、猫、鹿、狗、青蛙、马、船和卡车。每个图像大小为32x32像素,是彩色图像。 cifar-10-batches-py数据集通过Python脚本cifar10.py提供,它将数据集分为5个训练批次和1个验证批次。在训练过程中,可以使用这些批次中的图像进行训练,并根据验证数据集的结果来评估模型的性能。 这个数据集提供了一个方便的方式来测试和评估不同的图像分类算法和模型。使用TensorFlow的cifar10.py脚本可以加载这个数据集,并提供一些函数,用于解析和处理图像数据。 在使用cifar-10-batches-py数据集进行训练时,通常会将图像数据进行预处理,例如将像素值进行归一化处理,以便于模型的训练。同时,还可以使用数据增强的技术,如随机翻转、旋转或裁剪图像,以增加数据的多样性。 总的来说,TensorFlow的cifar-10-batches-py数据集是为了方便机器学习研究人员进行图像分类模型训练和验证而提供的一个常用数据集。它可以用于测试和评估不同的图像分类算法和模型的性能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值