Tensorflow深度学习之十二:基础图像处理之二
Tensorflow深度学习之十二:基础图像处理之二
首先放出原始图像:
1、图像的翻转
import tensorflow as tf
import cv2
# 这里定义一个tensorflow读取的图片格式转换为opencv读取的图片格式的函数
# 请注意:
# 在tensorflow中,一个像素点的颜色顺序是R,G,B。
# 在opencv中,一个像素点的颜色顺序是B,G,R。
# 因此,我们循环遍历每一个像素点,将第0位的颜色和第2位的颜色数值换一下即可。
# 第一个参数name:将要显示的窗口名称。
# 第二个参数image:储存图片信息的一个tensor。
def cv2Show(name="", image=None):
# 获取矩阵信息
np = image.eval()
# 获取行数列数
row, col = len(np),len(np[1])
# 两重循环遍历
for i in range(row):
for j in range(col):
# 交换数值
tmp = np[i][j][0]
np[i][j][0] = np[i][j][2]
np[i][j][2] = tmp
# 显示图片
cv2.imshow(name,np)
pass
# tensorflow会话
with tf.Session() as sess:
# 以二进制的方式读取图片。
image_raw_data = tf.gfile.FastGFile("bus.jpg", "rb").read()
# 按照jpeg的格式解码图片。
image_data = tf.image.decode_jpeg(image_raw_data)
# 显示原图片。
cv2Show("Read by Tensorflow+Dispalyed by Opencv",image_data)
# 上下翻转图像
up_and_down = tf.image.flip_up_down(image_data)
cv2Show("up and down",up_and_down)
# 左右翻转图像
left_and_right = tf.image.flip_left_right(image_data)
cv2Show("left and right", left_and_right)
# 沿对角线翻转图像
transposed = tf.image.transpose_image(image_data)
cv2Show("transposed image", transposed)
# 以一定概率上下翻转图像
random_up_and_down = tf.image.random_flip_up_down(image_data)
cv2Show("random up and down", random_up_and_down)
# 以一定概率左右翻转图像
random_left_and_right = tf.image.random_flip_left_right(image_data)
cv2Show("random left and right", random_left_and_right)
cv2.waitKey()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
程序运行结果如下:
注:由于个人的显示器限制,这里只截取了部分的对角线翻转的图像。
注:可以看到这里按照一定的概率翻转,只有上下进行了翻转,而左右并没有翻转。
2、图像的亮度调整
import tensorflow as tf
import cv2
# 这里定义一个tensorflow读取的图片格式转换为opencv读取的图片格式的函数
# 请注意:
# 在tensorflow中,一个像素点的颜色顺序是R,G,B。
# 在opencv中,一个像素点的颜色顺序是B,G,R。
# 因此,我们循环遍历每一个像素点,将第0位的颜色和第2位的颜色数值换一下即可。
# 第一个参数name:将要显示的窗口名称。
# 第二个参数image:储存图片信息的一个tensor。
def cv2Show(name="", image=None):
# 获取矩阵信息
np = image.eval()
# 获取行数列数
row, col = len(np),len(np[1])
# 两重循环遍历
for i in range(row):
for j in range(col):
# 交换数值
tmp = np[i][j][0]
np[i][j][0] = np[i][j][2]
np[i][j][2] = tmp
# 显示图片
cv2.imshow(name,np)
pass
# tensorflow会话
with tf.Session() as sess:
# 以二进制的方式读取图片。
image_raw_data = tf.gfile.FastGFile("bus.jpg", "rb").read()
# 按照jpeg的格式解码图片。
image_data = tf.image.decode_jpeg(image_raw_data)
# 显示原图片。
cv2Show("Read by Tensorflow+Dispalyed by Opencv",image_data)
# 将图片的亮度-0.5
adjusted1 = tf.image.adjust_brightness(image_data, -0.5)
cv2Show("brightness -0.5", adjusted1)
# 将图片的亮度+0.5
adjusted2 = tf.image.adjust_brightness(image_data, 0.5)
cv2Show("brightness +0.5",adjusted2)
# 随机调整图像的亮度:
# random_brightness(image, max_delta, seed=None)
# image:待调整的图像
# max_delta:在[-max_delte,max_delte)的范围随机调整图像的亮度
# seed:随机数种子
adjusted3 = tf.image.random_brightness(image_data, 0.3)
cv2Show("random brightness", adjusted3)
cv2.waitKey()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
程序运行如下:
3、调整图像的对比度
import tensorflow as tf
import cv2
# 这里定义一个tensorflow读取的图片格式转换为opencv读取的图片格式的函数
# 请注意:
# 在tensorflow中,一个像素点的颜色顺序是R,G,B。
# 在opencv中,一个像素点的颜色顺序是B,G,R。
# 因此,我们循环遍历每一个像素点,将第0位的颜色和第2位的颜色数值换一下即可。
# 第一个参数name:将要显示的窗口名称。
# 第二个参数image:储存图片信息的一个tensor。
def cv2Show(name="", image=None):
# 获取矩阵信息
np = image.eval()
# 获取行数列数
row, col = len(np),len(np[1])
# 两重循环遍历
for i in range(row):
for j in range(col):
# 交换数值
tmp = np[i][j][0]
np[i][j][0] = np[i][j][2]
np[i][j][2] = tmp
# 显示图片
cv2.imshow(name,np)
pass
# tensorflow会话
with tf.Session() as sess:
# 以二进制的方式读取图片。
image_raw_data = tf.gfile.FastGFile("bus.jpg", "rb").read()
# 按照jpeg的格式解码图片。
image_data = tf.image.decode_jpeg(image_raw_data)
# 显示原图片。
cv2Show("Read by Tensorflow+Dispalyed by Opencv",image_data)
# 将图片的对比度-5
adjusted1 = tf.image.adjust_contrast(image_data, -5)
cv2Show("contrast -5", adjusted1)
# 将图片的对比度+5
adjusted2 = tf.image.adjust_contrast(image_data, 5)
cv2Show("contrast +5",adjusted2)
# 随机调整图像的对比度:
# random_contrast(image, lower, upper, seed=None)
# image:待调整的图像
# lower,upper:在[lower,upper]的范围随机调整图像的对比度。lower非负。
# seed:随机数种子
adjusted3 = tf.image.random_contrast(image_data, 1, 9)
cv2Show("random contrast", adjusted3)
cv2.waitKey()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
程序运行如下:
4、调整图像的色相
import tensorflow as tf
import cv2
# 这里定义一个tensorflow读取的图片格式转换为opencv读取的图片格式的函数
# 请注意:
# 在tensorflow中,一个像素点的颜色顺序是R,G,B。
# 在opencv中,一个像素点的颜色顺序是B,G,R。
# 因此,我们循环遍历每一个像素点,将第0位的颜色和第2位的颜色数值换一下即可。
# 第一个参数name:将要显示的窗口名称。
# 第二个参数image:储存图片信息的一个tensor。
def cv2Show(name="", image=None):
# 获取矩阵信息
np = image.eval()
# 获取行数列数
row, col = len(np),len(np[1])
# 两重循环遍历
for i in range(row):
for j in range(col):
# 交换数值
tmp = np[i][j][0]
np[i][j][0] = np[i][j][2]
np[i][j][2] = tmp
# 显示图片
cv2.imshow(name,np)
pass
# tensorflow会话
with tf.Session() as sess:
# 以二进制的方式读取图片。
image_raw_data = tf.gfile.FastGFile("bus.jpg", "rb").read()
# 按照jpeg的格式解码图片。
image_data = tf.image.decode_jpeg(image_raw_data)
# 显示原图片。
cv2Show("Read by Tensorflow+Dispalyed by Opencv",image_data)
# adjust_hue(image, delta, name=None)
# delte的范围:[-1,1]
# 将图片的色相+0.1
adjusted1 = tf.image.adjust_hue(image_data, 0.1)
cv2Show("hue +0.1", adjusted1)
# 将图片的色相+0.3
adjusted2 = tf.image.adjust_hue(image_data, 0.3)
cv2Show("hue +0.3", adjusted2)
# 将图片的色相+0.6
adjusted3 = tf.image.adjust_hue(image_data, 0.6)
cv2Show("hue +0.6", adjusted3)
# 将图片的色相+0.9
adjusted4 = tf.image.adjust_hue(image_data, 0.9)
cv2Show("hue +0.9", adjusted4)
# 随机调整图像的色相:
# random_hue(image, max_delta, seed=None)
# image:待调整的图像
# max_delta:在[-max_delta,max_delta]的范围随机调整图像的色相。max_delta的范围[0,0.5]。
# seed:随机数种子
adjusted5 = tf.image.random_hue(image_data,0.4)
cv2Show("random hue", adjusted5)
cv2.waitKey()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
程序运行结果如下:
5、调整图像的饱和度
import tensorflow as tf
import cv2
# 这里定义一个tensorflow读取的图片格式转换为opencv读取的图片格式的函数
# 请注意:
# 在tensorflow中,一个像素点的颜色顺序是R,G,B。
# 在opencv中,一个像素点的颜色顺序是B,G,R。
# 因此,我们循环遍历每一个像素点,将第0位的颜色和第2位的颜色数值换一下即可。
# 第一个参数name:将要显示的窗口名称。
# 第二个参数image:储存图片信息的一个tensor。
def cv2Show(name="", image=None):
# 获取矩阵信息
np = image.eval()
# 获取行数列数
row, col = len(np),len(np[1])
# 两重循环遍历
for i in range(row):
for j in range(col):
# 交换数值
tmp = np[i][j][0]
np[i][j][0] = np[i][j][2]
np[i][j][2] = tmp
# 显示图片
cv2.imshow(name,np)
pass
# tensorflow会话
with tf.Session() as sess:
# 以二进制的方式读取图片。
image_raw_data = tf.gfile.FastGFile("bus.jpg", "rb").read()
# 按照jpeg的格式解码图片。
image_data = tf.image.decode_jpeg(image_raw_data)
# 显示原图片。
cv2Show("Read by Tensorflow+Dispalyed by Opencv",image_data)
# 将图片的饱和度-5
adjusted1 = tf.image.adjust_saturation(image_data, -5)
cv2Show("saturation -5", adjusted1)
# 将图片的饱和度+5
adjusted2 = tf.image.adjust_saturation(image_data, 5)
cv2Show("saturation +5", adjusted2)
# 随机调整图像的饱和度:
# random_saturation(image, lower, upper, seed=None)
# image:待调整的图像
# lower,upper:在[lower,upper]的范围随机调整图像的饱和度。lower非负。
# seed:随机数种子
adjusted3 = tf.image.random_saturation(image_data, 1, 9)
cv2Show("random saturation", adjusted3)
cv2.waitKey()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
程序运行结果如下:
6、图像的标准化
import tensorflow as tf
import cv2
# 这里定义一个tensorflow读取的图片格式转换为opencv读取的图片格式的函数
# 请注意:
# 在tensorflow中,一个像素点的颜色顺序是R,G,B。
# 在opencv中,一个像素点的颜色顺序是B,G,R。
# 因此,我们循环遍历每一个像素点,将第0位的颜色和第2位的颜色数值换一下即可。
# 第一个参数name:将要显示的窗口名称。
# 第二个参数image:储存图片信息的一个tensor。
def cv2Show(name="", image=None):
# 获取矩阵信息
np = image.eval()
# 获取行数列数
row, col = len(np),len(np[1])
# 两重循环遍历
for i in range(row):
for j in range(col):
# 交换数值
tmp = np[i][j][0]
np[i][j][0] = np[i][j][2]
np[i][j][2] = tmp
# 显示图片
cv2.imshow(name,np)
pass
# tensorflow会话
with tf.Session() as sess:
# 以二进制的方式读取图片。
image_raw_data = tf.gfile.FastGFile("bus.jpg", "rb").read()
# 按照jpeg的格式解码图片。
image_data = tf.image.decode_jpeg(image_raw_data)
# 显示原图片。
cv2Show("Read by Tensorflow+Dispalyed by Opencv",image_data)
# 将代表一张图像的三维矩阵中的数字均值变成0,方差变为1。
adjusted = tf.image.per_image_standardization(image_data)
cv2Show("image_standardization", adjusted)
cv2.waitKey()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
程序运行结果如下:
Tensorflow深度学习之十二:基础图像处理之二的更多相关文章
- TensorFlow深度学习基础与应用实战高清视频教程
TensorFlow深度学习基础与应用实战高清视频教程,适合Python C++ C#视觉应用开发者,基于TensorFlow深度学习框架,讲解TensorFlow基础.图像分类.目标检测训练与测试以 ...
- 没有博士学位,照样玩转TensorFlow深度学习
教程 | 没有博士学位,照样玩转TensorFlow深度学习 机器之心2017-01-24 12:32:22 程序设计 谷歌 操作系统 阅读(362)评论(0) 选自Codelabs 机器之心编译 参 ...
- TensorFlow 深度学习中文第二版·翻译完成
原文:Deep Learning with TensorFlow Second Edition 协议:CC BY-NC-SA 4.0 不要担心自己的形象,只关心如何实现目标.--<原则>, ...
- (转) TensorFlow深度学习,一篇文章就够了
TensorFlow深度学习,一篇文章就够了 2016/09/22 · IT技术 · TensorFlow, 深度学习 分享到:6 原文出处: 我爱计算机 (@tobe迪豪 ) 作者: 陈迪 ...
- (转)深度学习word2vec笔记之基础篇
深度学习word2vec笔记之基础篇 声明: 1)该博文是多位博主以及多位文档资料的主人所无私奉献的论文资料整理的.具体引用的资料请看参考文献.具体的版本声明也参考原文献 2)本文仅供学术交流,非商用 ...
- 深度学习word2vec笔记之基础篇
作者为falao_beiliu. 作者:杨超链接:http://www.zhihu.com/question/21661274/answer/19331979来源:知乎著作权归作者所有.商业转载请联系 ...
- TensorFlow深度学习,一篇文章就够了
http://blog.jobbole.com/105602/ 作者: 陈迪豪,就职小米科技,深度学习工程师,TensorFlow代码提交者. TensorFlow深度学习框架 Google不仅是大数 ...
- 问题集录--TensorFlow深度学习
TensorFlow深度学习框架 Google不仅是大数据和云计算的领导者,在机器学习和深度学习上也有很好的实践和积累,在2015年年底开源了内部使用的深度学习框架TensorFlow. 与Caffe ...
- 学习参考《TensorFlow深度学习》高清中文版PDF+英文版PDF+源代码
我们知道,TensorFlow是比较流行的深度学习框架,除了看手册文档外,推荐大家看看<Tensorflow深度学习>,共分5方面内容:基础知识.关键模块.算法模型.内核揭秘.生态发展.前 ...
随机推荐
- tomcat知识点
(1)使用线程池 Servlet引擎为每一个请求创建一个隔离的线程,分配这个线程给service()方法,在它执行完后移除这个线程.默认情况下,servlet引擎 为每一个请求创建新的线程.因为创 ...
- filter和spring 的interceptor都是单例的,都不是线程安全的
Filter 是在 Servlet 容器启动时就初始化的,因此可以认为是以单例对象存在的,如果一个请求线程对其中的成员变量修改的话,会影响到其他的请求线程,因此认为是多线程不安全的.
- SpringMVC整合fastdfs-client-java实现web文件上传下载
原文:http://blog.csdn.net/wlwlwlwl015/article/details/52682153 本篇blog主要记录一下SpringMVC整合FastDFS的Java客户端实 ...
- DevExpress打印功能介绍 z
一.打印功能说明: 打印功能,我们有多种实现方式,可以根据需要自行选择,我简单的总结下两种方法. (1).使用微软.net框架自带的PrintDocument,这种方式是直接借助Graphics,自行 ...
- iOS -- SKTransition类
SKTransition类 继承自 NSObject 符合 NSObject(NSObject) 框架 /System/Library/Frameworks/SpriteKit.framewor ...
- 全方位绕过软WAF攻略
0×00 前言 现在软waf较为多,就在今年夏天苦逼挖洞的日子里经常遇到360主机卫士,安全狗,云锁之类的软waf进行拦截,经常碰到如下拦截提示: 看到以上三个拦截提示就让人头疼不已,欲罢不能. so ...
- 构造方法后面带:this()
可以这么理解,有参数的构造函数需要执行无参构造函数中的代码,为了省去重复代码的编写,所以就继承了,先执行没参数的那个构造函数. 在this上“转到定义”(F12)就到第一个构造函数上去了.
- 工作总结 mvc 调页面传参数 参数值会一直保存 在这个页面上的
意思是 两个页面均可以 获取到id 和 goodsType 都可以获取 id goodsType post 的 还多带点属性值 form data 页面上带过去的 (新增 编辑)
- Ubuntu 登陆异常-输入正确的密码后还会返回到登陆界面的问题
问题表现: 启动到了登陆界面,输入对应的密码,发现一闪黑屏有返回到登陆界面,如此往复. 解决方法: 开机后在登陆界面按下shift + ctrl + F1进入tty命令行终端登陆,可以查看用户主目录下 ...
- Ecilpse绑定jdk的源码
因为近期才入职,所以电脑环境才配好,今天在写代码的时候,想查看源码,发现不能查看,所以在网上百度了一下: 下面是解决方法: 1.在Ecilpse的窗体下,点击Preferences 2.然后点击Jav ...