TensorFlow学习之 图像预处理
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学习之 图像预处理的更多相关文章
- tensorflow学习笔记——图像数据处理
喜欢摄影的盆友都知道图像的亮度,对比度等属性对图像的影响是非常大的,相同物体在不同亮度,对比度下差别非常大.然而在很多图像识别问题中,这些因素都不应该影响最后的结果.所以本文将学习如何对图像数据进行预 ...
- TensorFlow图像预处理完整样例
参考书 <TensorFlow:实战Google深度学习框架>(第2版) 以下TensorFlow程序完成了从图像片段截取,到图像大小调整再到图像翻转及色彩调整的整个图像预处理过程. #! ...
- 『TensorFlow』第九弹_图像预处理_不爱红妆爱武装
部分代码单独测试: 这里实践了图像大小调整的代码,值得注意的是格式问题: 输入输出图像时一定要使用uint8编码, 但是数据处理过程中TF会自动把编码方式调整为float32,所以输入时没问题,输出时 ...
- TensorFlow学习笔记(五)图像数据处理
目录: 一.TFRecord输入数据格式 1.1 TFrecord格式介绍 1.2 TFRecord样例程序 二.图像数据处理 2.1TensorFlow图像处理函数 2.2图像预处理完整样例 三.多 ...
- 深度学习与计算机视觉(12)_tensorflow实现基于深度学习的图像补全
深度学习与计算机视觉(12)_tensorflow实现基于深度学习的图像补全 原文地址:Image Completion with Deep Learning in TensorFlow by Bra ...
- tensorflow学习笔记——多线程输入数据处理框架
之前我们学习使用TensorFlow对图像数据进行预处理的方法.虽然使用这些图像数据预处理的方法可以减少无关因素对图像识别模型效果的影响,但这些复杂的预处理过程也会减慢整个训练过程.为了避免图像预处理 ...
- TensorFlow学习笔记——LeNet-5(训练自己的数据集)
在之前的TensorFlow学习笔记——图像识别与卷积神经网络(链接:请点击我)中了解了一下经典的卷积神经网络模型LeNet模型.那其实之前学习了别人的代码实现了LeNet网络对MNIST数据集的训练 ...
- tensorflow学习笔记——使用TensorFlow操作MNIST数据(1)
续集请点击我:tensorflow学习笔记——使用TensorFlow操作MNIST数据(2) 本节开始学习使用tensorflow教程,当然从最简单的MNIST开始.这怎么说呢,就好比编程入门有He ...
- Tensorflow学习笔记No.5
tf.data卷积神经网络综合应用实例 使用tf.data建立自己的数据集,并使用CNN卷积神经网络实现对卫星图像的二分类问题. 数据下载链接:https://pan.baidu.com/s/141z ...
随机推荐
- js 千分位符号 正则方法
function toThousands(num) { return (num || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');}
- Spring中三个重要概念 IOC AOP Bean
Spring中三个重要概念 IOC AOP Bean 首先讲解一下Spring框架,以及为什么要使用Spring 框架? spring 是一个很好的容器框架, 是轻量级的IoC和AOP的容器框架,主要 ...
- 优化SQLServer
由于SQLServer,数据文件mdf过大,造成系统异常卡 一. 更改隔离级别 ALTER DATABASE [B2EC] SET SINGLE_USER WITH ROLLBACK IMMEDIAT ...
- GString惰性求值
当对一个GString实例求值时,如果其中包含一个变量,该变量的值会被简单地打印到一个Writer,通常是一个StringWriter.然而,如果GString中包含的是一个闭包,而非变量,该闭包就会 ...
- 《JavaWeb从入门到改行》JSP+EL+JSTL大杂烩汤
title: Servlet之JSP tags: [] notebook: javaWEB --- JSP是什么 ? JSP就是Servlet,全名是"JavaServer Pages&qu ...
- C#与.NET的区别和C#程序结构
C#语言及其特点 (1)语法简洁,不允许直接操作做内存,去掉指针操作 (2)彻底的面向对象设计,C#具有面向对象所应用的一切特性:封装.继承.多态 (3)与Web紧密结合,C#支持绝大多数的Web标准 ...
- mybatis三种传值方式
第一种:按序列传参(dao层的函数方法)[sql] Public User selectUser(String name,String area); 对应的Mapper.xml [sql] <s ...
- Sass、Less、Stylus,我选Sass!
Sass官网 | Sass中文 简介:待添加 Less 简介:待添加 Stylus 简介:待添加
- Shellinabox安装及使用教程
本文转载自: shellinabox:一款使用 AJAX 的基于 Web 的终端模拟器 一.shellinabox简介 通常情况下,我们在访问任何远程服务器时,会使用常见的通信工具如OpenSSH和P ...
- Java Web高性能开发 - 前端高性能
作者:IBM developerWorks链接:https://www.ibm.com/developerworks/cn/java/j-lo-javawebhiperf1/著作权归作者所有.商业转载 ...