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. [转]C# 6.0 的新特性

    本文的内容包括引入C#6.0中的新的语言特性有哪些. 还有已经被引入的代码名称为 “Roslyn”新编译器. 编译器是开放源码的,并且可以从 codeplex 网站的这个地址下载到源代码: https ...

  2. 微信小程序初体验

    小程序最近太火,不过相比较刚发布时,已经有点热度散去的感觉,不过这不影响我们对小程序的热情,开发之前建议通读下官网文档,附链接:https://mp.weixin.qq.com/debug/wxado ...

  3. nvm安装最新稳定版node

    安装当前最新的稳定版. nvm install stable

  4. 十二、spark MLlib的scala示例

    简介 spark MLlib官网:http://spark.apache.org/docs/latest/ml-guide.html mllib是spark core之上的算法库,包含了丰富的机器学习 ...

  5. ssh 连接慢问题

    连接先看报错: There were 11 failed login attempts since the last successful login. 先前有上百上千失败login,被攻击了,把短时 ...

  6. poj 3070 Fibonacci 矩阵相乘

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7715   Accepted: 5474 Descrip ...

  7. Linux下ipv6配置系列

    Linux下ipv6配置系列一:如何配置Linux系统ipv6环境 Linux下ipv6配置系列二:如何为Nginx添加ipv6模块 Linux下ipv6配置系列三:如何为Nginx配置IPv6端口监 ...

  8. HTML——基本html标签

    基本html标签 <html> ... </html>  定义HTML文档 <head> ... </head>  文档的信息 <meta /&g ...

  9. DOM3 textInput事件

    DOM3中引入了文本事件,其中之一 textInput . 当用户再可编辑区域输入字符时触发该事件. 与keypress不同的是,该事件只会在用户输入可视字符时触发,而keypres事件则只要按下键即 ...

  10. Java常见异常类

    NullpointException(空指针异常)ClassNotFoundException(类找不到异常)ClassCastException(类型转换异常)IllegalArgumentExce ...