import tensorflow as tf
import matplotlib.pyplot as plt image_raw_data = tf.gfile.GFile("./picture.jpg", "rb").read()
with tf.Session() as sess:
"""
图像编码解码处理
"""
# 解码过程
img_data = tf.image.decode_jpeg(image_raw_data)
print(img_data.eval()) # 编码过程
encoded_image = tf.image.encode_jpeg(img_data)
with tf.gfile.GFile('./output.jpg', 'wb') as f:
f.write(encoded_image.eval()) """
图像大小调整
"""
# 将图片数据转化为实数类型,将0-255的像素值转化为0.0-1.0之间的实数
img_data = tf.image.convert_image_dtype(img_data, dtype=tf.float32) # 第一个参数:原始图像;第二个参数:调整后的图像大小;第三个参数:method参数给出调整图像大小的算法
resized = tf.image.resize_images(img_data, [300, 300], method=0) # 目标尺寸小于原始尺寸,居中截取;否者填充
croped = tf.image.resize_image_with_crop_or_pad(img_data, 500, 300) plt.figure()
plt.subplot(2, 2, 1)
plt.imshow(img_data.eval())
plt.subplot(2, 2, 2)
plt.imshow(resized.eval())
plt.subplot(2, 1, 2)
plt.imshow(croped.eval()) """
图像翻转
"""
# 上下翻转
flipped_up_down = tf.image.flip_up_down(img_data)
# 50%的概率上下翻转
flipped1 = tf.image.random_flip_up_down(img_data) # 左右翻转
flipped_left_right = tf.image.flip_left_right(img_data)
# 50%的概率左右翻转
flipped2 = tf.image.random_flip_left_right(img_data) # 沿对角线高翻转
transpose_image = tf.image.transpose_image(img_data) plt.figure()
plt.subplot(2, 2, 1)
plt.imshow(flipped_up_down.eval())
plt.subplot(2, 2, 2)
plt.imshow(flipped_left_right.eval())
plt.subplot(2, 2, 3)
plt.imshow(transpose_image.eval())
plt.subplot(2, 2, 4)
plt.imshow(flipped2.eval()) """
图像色彩调整
"""
# 亮度 -0.5
adjusted = tf.image.adjust_brightness(img_data, -0.5)
# 将其值截取在0.0-1.0之间,防止像素实数值超出0.0-1.0的范围
adjusted_down = tf.clip_by_value(adjusted, 0.0, 1.0)
# 亮度 +0.5
adjusted_up = tf.image.adjust_brightness(img_data, 0.5)
# 在[-max_delta, max_delta]的范围之间随机调整图像亮度
adjusted_random = tf.image.random_brightness(img_data, 0.2) # 对比度 x0.5
adjusted1 = tf.image.adjust_contrast(img_data, 0.5)
# 对比度 增加5倍
adjusted2 = tf.image.adjust_contrast(img_data, 5)
# 在[lower, upper]的范围内随机调整图像的对比度
adjusted3 = tf.image.random_contrast(img_data, 2, 4) # 色相 +0.6
adjusted_hue1 = tf.image.adjust_hue(img_data, 0.6)
# 色相 -0.6
adjusted_hue2 = tf.image.adjust_hue(img_data, -0.6)
# 在[-max_delta, max_delta]的范围内随机调整图像的色相,max_delta取值范围[0, 0.5]
adjusted_hue3 = tf.image.random_hue(img_data, 0.3) # 饱和度 -5
adjust_saturation1 = tf.image.adjust_saturation(img_data, -5)
# 饱和度 +5
adjust_saturation2 = tf.image.adjust_saturation(img_data, 5)
# 在[lower, upper]的范围之间随机调整图像的饱和度,lower必须是非负值
adjust_saturation3 = tf.image.random_saturation(img_data, 0, 4)
plt.figure()
plt.subplot(4, 2, 1)
plt.imshow(adjusted_down.eval())
plt.subplot(4, 2, 2)
plt.imshow(adjusted_up.eval())
plt.subplot(4, 2, 3)
plt.imshow(adjusted1.eval())
plt.subplot(4, 2, 4)
plt.imshow(adjusted2.eval())
plt.subplot(4, 2, 5)
plt.imshow(adjusted_hue1.eval())
plt.subplot(4, 2, 6)
plt.imshow(adjusted_hue2.eval())
plt.subplot(4, 2, 7)
plt.imshow(adjust_saturation1.eval())
plt.subplot(4, 2, 8)
plt.imshow(adjust_saturation2.eval()) # 图像数值标准化,均值为0,方差为1
adjust_standardization = tf.image.per_image_standardization(img_data)
plt.figure()
plt.imshow(adjust_standardization.eval()) plt.show()

TensorFlow学习之 图像预处理的更多相关文章

  1. tensorflow学习笔记——图像数据处理

    喜欢摄影的盆友都知道图像的亮度,对比度等属性对图像的影响是非常大的,相同物体在不同亮度,对比度下差别非常大.然而在很多图像识别问题中,这些因素都不应该影响最后的结果.所以本文将学习如何对图像数据进行预 ...

  2. TensorFlow图像预处理完整样例

    参考书 <TensorFlow:实战Google深度学习框架>(第2版) 以下TensorFlow程序完成了从图像片段截取,到图像大小调整再到图像翻转及色彩调整的整个图像预处理过程. #! ...

  3. 『TensorFlow』第九弹_图像预处理_不爱红妆爱武装

    部分代码单独测试: 这里实践了图像大小调整的代码,值得注意的是格式问题: 输入输出图像时一定要使用uint8编码, 但是数据处理过程中TF会自动把编码方式调整为float32,所以输入时没问题,输出时 ...

  4. TensorFlow学习笔记(五)图像数据处理

    目录: 一.TFRecord输入数据格式 1.1 TFrecord格式介绍 1.2 TFRecord样例程序 二.图像数据处理 2.1TensorFlow图像处理函数 2.2图像预处理完整样例 三.多 ...

  5. 深度学习与计算机视觉(12)_tensorflow实现基于深度学习的图像补全

    深度学习与计算机视觉(12)_tensorflow实现基于深度学习的图像补全 原文地址:Image Completion with Deep Learning in TensorFlow by Bra ...

  6. tensorflow学习笔记——多线程输入数据处理框架

    之前我们学习使用TensorFlow对图像数据进行预处理的方法.虽然使用这些图像数据预处理的方法可以减少无关因素对图像识别模型效果的影响,但这些复杂的预处理过程也会减慢整个训练过程.为了避免图像预处理 ...

  7. TensorFlow学习笔记——LeNet-5(训练自己的数据集)

    在之前的TensorFlow学习笔记——图像识别与卷积神经网络(链接:请点击我)中了解了一下经典的卷积神经网络模型LeNet模型.那其实之前学习了别人的代码实现了LeNet网络对MNIST数据集的训练 ...

  8. tensorflow学习笔记——使用TensorFlow操作MNIST数据(1)

    续集请点击我:tensorflow学习笔记——使用TensorFlow操作MNIST数据(2) 本节开始学习使用tensorflow教程,当然从最简单的MNIST开始.这怎么说呢,就好比编程入门有He ...

  9. Tensorflow学习笔记No.5

    tf.data卷积神经网络综合应用实例 使用tf.data建立自己的数据集,并使用CNN卷积神经网络实现对卫星图像的二分类问题. 数据下载链接:https://pan.baidu.com/s/141z ...

随机推荐

  1. 十七、ThreadPoolExecutor线程池

    一.简介 executor接口 executor接口在JDK的java.util.concurrent包下,它只有一个抽象方法: void execute(Runnable command); 这意味 ...

  2. CXF开发WebService

    CXF开发Web Service 参考链接 使用 spring 框架来集成 Web Services 开发 浏览器调用接口 大概这样, 没成功 加@WebMethod(action="get ...

  3. 高并发系列之——MQ消息中间件Kafka

    1.前言 1.1 包路径和源码 下载链接 基于发布订阅的分布式消息系统,使用scala语言编写. 特点:采用分区机制,每个分区可以放到不同的服务器上,提高了吞吐率,同时基于磁盘存储,以及副本机制可以确 ...

  4. [LeetCode]Maximum Length of Repeated Subarray

    Maximum Length of Repeated Subarray: Given two integer arrays A and B, return the maximum length of ...

  5. uestc Another LCIS

    Another LCIS Time Limit: 1000 ms Memory Limit: 65536 kB Solved: 193 Tried: 2428 Description For a se ...

  6. Java 并发:Executor

    异常捕获 以前使用executor的时候,为了记录任务线程的异常退出会使用ThreadFactory来设置线程的UncaughtExceptionHandler,但是按照书上的验证发现,采用execu ...

  7. slider轮播插件的多种写法

    slider轮播插件相信大家经常会用到,写法也是各种各样,大部分都是用的第三方提供的组件使用,如果想基于自己的业务特点封装个组件供自己使用的话就要自己封装组件了,网上看了大部分写法都是通过js控制do ...

  8. Maven学习总结(八):Myecplise中配置maven

    第一步:下载maven安装包,配置环境变量M2_HOME;变量值为maven的解压目录. 第二步:在eclipse4.0之前的版本需要安装maven插件,方法即:将maven插件包复制到eclipse ...

  9. <pre> <textarea> <code>标签区别

    这篇文章里面放的大都是我自己写程序的时候遇到的一些小问题,其实都是自己没有掌握的点,别人看起来应该很简单啦,但写下来能提醒自己,也能鼓励一下自己,这条路也不好走哇. <pre> <t ...

  10. Visual Studio 2015 + Update 1

    Visual Studio 2015是一个基本完整的开发工具集,它包括了整个软件生命周期中所需要的大部分工具,如UML工具.代码管控工具.集成开发环境(IDE)等等.所写的目标代码适用于微软支持的所有 ...