深度学习框架Tensorflow学习与应用 图像数据处理之一
2018-03-30 13:54
701 查看
图像处理python有很多个库,因为最近在学习Tensorflow,所以就用Tensorlow做了一些小处理。
import tensorflow as tf
# 读取图像的原始数据
image_raw_data = tf.gfile.FastGFile("timg.jpg", 'rb').read()
with tf.Session() as sess:
# 将图片使用jppeg的格式解码从而得到图像对应的三维矩阵。tensorflow还提供了
# tf.image.decode_png函数对png格式的图像进行解码,解码之后的结果为一个张量
# ,在使用它的取值之前需要明确调用运行的过程
image_data = tf.image.decode_jpeg(image_raw_data)
# 输出解码的三维矩阵
#eval() 其实就是tf.Tensor的Session.run() 的另外一种写法
print(image_data.eval())
# 使用pyplot工具可视化得到的图像
plt.imshow(image_data.eval())
plt.show()
#将数据转化成实数方便下面的样例程序对图像进行处理
image_data = tf.image.convert_image_dtype(image_data,dtype=tf.uint8)
#将表示一张图像的三维矩阵重新按照jpeg格式编码并存入文件中,打开这张像
#可以得到和原始图像一样的图像
encode_image = tf.image.encode_jpeg(image_data)
with tf.gfile.GFile("out./01.jpg",'wb') as f:
f.write(encode_image.eval())这就是把图像另存了一下,就不贴图片了。
resized = tf.image.resizes(image_data,[img_H,img_W],method=)
这四种方法的具体区别我也不是很清楚,如果有知道的,请赐教。import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
# 读取图像的原始数据
image_raw_data = tf.gfile.FastGFile("timg.jpg", 'rb').read()
with tf.Session() as sess:
image_data = tf.image.decode_jpeg(image_raw_data)
#method =0 双线性插值法
#method = 1 ,最近邻居法
#method = 2 ,双三次插值法
#meithod = 3 面积插值法
resized = tf.image.resize_images(image_data,[300,300],method=0)
# 输出调整后图像的大小,此处的结果为(300,300,?)。表示图像的大小是300*300但图像的深度在没有明确设置之前会是问号。
print(resized.get_shape())
# TensorFlow的函数处理图片后存储的数据是float32格式的,
# 需要转换成uint8才能正确打印图片。
resized = np.asarray(resized.eval(), dtype='uint8')
plt.imshow(resized)
plt.show()
#将表示一张图像的三维矩阵重新按照jpeg格式编码并存入文件中,打开这张像可以得到和原始图像一样的图像
encode_image = tf.image.encode_jpeg(resized)
with tf.gfile.GFile("out./02.jpg",'wb') as f:
f.write(encode_image.eval())
![](https://oscdn.geek-share.com/Uploads/Images/Content/201803/a9db7a62aaa758046ad70156afd0a74e)
三:图像翻转
对图像进行翻转可以有效的扩充数据集.我们可以将图像上下翻转,左右翻转以及沿对角线翻转。import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
# 读取图像的原始数据
image_raw_data = tf.gfile.FastGFile("timg.jpg", 'rb').read()
with tf.Session() as sess:
image_data = tf.image.decode_jpeg(image_raw_data)
#将图像上下翻转
flipped = tf.image.flip_up_down(image_data)
resized = np.asarray(flipped.eval(), dtype='uint8')
plt.imshow(resized)
plt.show()
encode_image = tf.image.encode_jpeg(resized)
with tf.gfile.GFile("out./03_up_down.jpg", 'wb') as f:
f.write(encode_image.eval())
#将图片左右翻转,
flipped = tf.image.flip_left_right(image_data)
resized = np.asarray(flipped.eval(), dtype='uint8')
plt.imshow(resized)
plt.show()
encode_image = tf.image.encode_jpeg(resized)
with tf.gfile.GFile("out./03_left_right.jpg", 'wb') as f:
f.write(encode_image.eval())
#将图片沿对角线翻转
flipped = tf.image.transpose_image(image_data)
resized = np.asarray(flipped.eval(), dtype='uint8')
plt.imshow(resized)
plt.show()
encode_image = tf.image.encode_jpeg(resized)
with tf.gfile.GFile("out./03_transpose_image.jpg", 'wb') as f:
f.write(encode_image.eval())
#将图片沿以一定概率上下翻转
flipped = tf.image.random_flip_up_down(image_data)
resized = np.asarray(flipped.eval(), dtype='uint8')
plt.imshow(resized)
plt.show()
encode_image = tf.image.encode_jpeg(resized)
with tf.gfile.GFile("out./03_random_flip_up_down.jpg", 'wb') as f:
f.write(encode_image.eval())
#将图片以一定概率
flipped = tf.image.random_flip_left_right(image_data)
resized = np.asarray(flipped.eval(), dtype='uint8')
plt.imshow(resized)
plt.show()
encode_image = tf.image.encode_jpeg(resized)
with tf.gfile.GFile("out./03_random_flip_left_right.jpg", 'wb') as f:
f.write(encode_image.eval())
![](https://oscdn.geek-share.com/Uploads/Images/Content/201803/3f39045c425c3fb82c27a9bdac98b079)
一:图像编码处理
图像在存储时不是直接记录图像中的每个像素值,而是记录经过压缩编码后的结果。所以要将图像还原成矩阵,需要解码的过程。Tensorflow提供了对jpeg/png格式图像的编码或解码函数。import matplotlib.pyplot as pltimport tensorflow as tf
# 读取图像的原始数据
image_raw_data = tf.gfile.FastGFile("timg.jpg", 'rb').read()
with tf.Session() as sess:
# 将图片使用jppeg的格式解码从而得到图像对应的三维矩阵。tensorflow还提供了
# tf.image.decode_png函数对png格式的图像进行解码,解码之后的结果为一个张量
# ,在使用它的取值之前需要明确调用运行的过程
image_data = tf.image.decode_jpeg(image_raw_data)
# 输出解码的三维矩阵
#eval() 其实就是tf.Tensor的Session.run() 的另外一种写法
print(image_data.eval())
# 使用pyplot工具可视化得到的图像
plt.imshow(image_data.eval())
plt.show()
#将数据转化成实数方便下面的样例程序对图像进行处理
image_data = tf.image.convert_image_dtype(image_data,dtype=tf.uint8)
#将表示一张图像的三维矩阵重新按照jpeg格式编码并存入文件中,打开这张像
#可以得到和原始图像一样的图像
encode_image = tf.image.encode_jpeg(image_data)
with tf.gfile.GFile("out./01.jpg",'wb') as f:
f.write(encode_image.eval())这就是把图像另存了一下,就不贴图片了。
二:图像大小调整
(一)
我们从网上获取到的数据集往往不符合我们的需求,但神经网络的输入节点个数确是固定的,所以需要去先把图像的尺寸改为自己所需要的。Tensorflow给我们提供了四种方法,它们被封装在tf.image.resize_images函数里。resized = tf.image.resizes(image_data,[img_H,img_W],method=)
method取值 | |
0 | 双线性插值法 |
1 | 最近邻居法 |
2 | 双三次插值法 |
3 | 面积插值法 |
import tensorflow as tf
import numpy as np
# 读取图像的原始数据
image_raw_data = tf.gfile.FastGFile("timg.jpg", 'rb').read()
with tf.Session() as sess:
image_data = tf.image.decode_jpeg(image_raw_data)
#method =0 双线性插值法
#method = 1 ,最近邻居法
#method = 2 ,双三次插值法
#meithod = 3 面积插值法
resized = tf.image.resize_images(image_data,[300,300],method=0)
# 输出调整后图像的大小,此处的结果为(300,300,?)。表示图像的大小是300*300但图像的深度在没有明确设置之前会是问号。
print(resized.get_shape())
# TensorFlow的函数处理图片后存储的数据是float32格式的,
# 需要转换成uint8才能正确打印图片。
resized = np.asarray(resized.eval(), dtype='uint8')
plt.imshow(resized)
plt.show()
#将表示一张图像的三维矩阵重新按照jpeg格式编码并存入文件中,打开这张像可以得到和原始图像一样的图像
encode_image = tf.image.encode_jpeg(resized)
with tf.gfile.GFile("out./02.jpg",'wb') as f:
f.write(encode_image.eval())
(二)对处理的图像进行填充或裁剪与按照比例调整图像大小
import matplotlib.pyplot as plt import tensorflow as tf import numpy as np image_raw_data = tf.gfile.FastGFile('timg.jpg','rb').read() with tf.Session() as sess: img_data = tf.image.decode_jpeg(image_raw_data) # tensorflow提供了api对图像进行裁剪或者填充 # 如果目标图像小于原始图像,则自动在截取原始图像居中部分 croped = tf.image.resize_image_with_crop_or_pad(img_data, 150, 150) plt.imshow(croped.eval()) plt.show() # 如果目标图像大于原始图像,则自动在原始图像四周填充0 padded = tf.image.resize_image_with_crop_or_pad(img_data, 600, 800) plt.imshow(padded.eval()) plt.show() #tensorflow还支持比例调整 #第二个参数为(0,1]之间的实数 central_cropped = tf.image.central_crop(img_data, 0.5) plt.imshow(central_cropped.eval()) plt.show() croped1 = tf.image.encode_jpeg(croped) with tf.gfile.GFile("out./02_crop.jpg", 'wb') as f: f.write(croped1.eval()) padded1 = tf.image.encode_jpeg(padded) with tf.gfile.GFile("out./02_padded.jpg", 'wb') as f: f.write(padded1.eval()) central_cropped1 = tf.image.encode_jpeg(central_cropped) with tf.gfile.GFile("out./02_central_cropped.jpg", 'wb') as f: f.write(central_cropped1.eval())
三:图像翻转
对图像进行翻转可以有效的扩充数据集.我们可以将图像上下翻转,左右翻转以及沿对角线翻转。import matplotlib.pyplot as pltimport tensorflow as tf
import numpy as np
# 读取图像的原始数据
image_raw_data = tf.gfile.FastGFile("timg.jpg", 'rb').read()
with tf.Session() as sess:
image_data = tf.image.decode_jpeg(image_raw_data)
#将图像上下翻转
flipped = tf.image.flip_up_down(image_data)
resized = np.asarray(flipped.eval(), dtype='uint8')
plt.imshow(resized)
plt.show()
encode_image = tf.image.encode_jpeg(resized)
with tf.gfile.GFile("out./03_up_down.jpg", 'wb') as f:
f.write(encode_image.eval())
#将图片左右翻转,
flipped = tf.image.flip_left_right(image_data)
resized = np.asarray(flipped.eval(), dtype='uint8')
plt.imshow(resized)
plt.show()
encode_image = tf.image.encode_jpeg(resized)
with tf.gfile.GFile("out./03_left_right.jpg", 'wb') as f:
f.write(encode_image.eval())
#将图片沿对角线翻转
flipped = tf.image.transpose_image(image_data)
resized = np.asarray(flipped.eval(), dtype='uint8')
plt.imshow(resized)
plt.show()
encode_image = tf.image.encode_jpeg(resized)
with tf.gfile.GFile("out./03_transpose_image.jpg", 'wb') as f:
f.write(encode_image.eval())
#将图片沿以一定概率上下翻转
flipped = tf.image.random_flip_up_down(image_data)
resized = np.asarray(flipped.eval(), dtype='uint8')
plt.imshow(resized)
plt.show()
encode_image = tf.image.encode_jpeg(resized)
with tf.gfile.GFile("out./03_random_flip_up_down.jpg", 'wb') as f:
f.write(encode_image.eval())
#将图片以一定概率
flipped = tf.image.random_flip_left_right(image_data)
resized = np.asarray(flipped.eval(), dtype='uint8')
plt.imshow(resized)
plt.show()
encode_image = tf.image.encode_jpeg(resized)
with tf.gfile.GFile("out./03_random_flip_left_right.jpg", 'wb') as f:
f.write(encode_image.eval())
相关文章推荐
- 深度学习框架Tensorflow学习与应用 图像数据处理之二
- TensorFlow:实战Google深度学习框架(六)图像数据处理
- 深度学习框架TensorFlow学习与应用(七)——循环神经网络(RNN)应用于MNIST数据集分类
- 深度学习框架TensorFlow学习与应用(一)——基本概念与简单示例
- 深度学习框架Tensorflow学习与应用 第1课
- 深度学习框架TensorFlow学习与应用(二)——非线性回归、MINST数据集分类
- 大数据下基于Tensorflow框架的深度学习示例教程
- Tensorflow学习笔记-输入数据处理框架
- 深度学习框架TensorFlow学习与应用(四)——拟合问题、优化器
- 炼数成金《深度学习框架Tensorflow学习与应用》
- 寻找下一款Prisma APP:深度学习在图像处理中的应用探讨(阅读小结)
- 深度学习框架Tensorflow学习与应用 第2课
- python-框架-网页爬虫-文本处理-科学计算-可视化-机器学习-数据挖掘-深度学习
- TensorFlow:实战Google深度学习框架,数据增强
- 深度学习——卷积神经网络在tensorflow框架下的应用案例
- 深度学习-CAFFE利用CIFAR10网络模型训练自己的图像数据获得模型-4应用生成模型进行预测
- 深度学习框架TensorFlow学习与应用(五)——TensorBoard结构与可视化
- 大数据下基于Tensorflow框架的深度学习示例教程
- TensorFlow:实战Google深度学习框架(五)图像识别与卷积神经网络
- 深度学习框架TensorFlow学习与应用(三)——使用交叉熵作为代价函数