tensorflow图像处理函数

tensorflow图像处理函数

必备知识点: 一张RGB色彩模式的图像可以看成一个三维矩阵,矩阵中的每一个数表示了图像上不同的位置,不同颜色的亮度。然而在图像储存的时候,并不是直接记录这些矩阵中的数字,而是记录经过压缩之后的结果,故在打开一张照片的时候则需要进行解码的过程
流程图:
在这里插入图片描述
tensorflow提供了对jpeg和png格式图像的编码/解码函数
使用的图像:
在这里插入图片描述

读取图像

import matplotlib.pyplot as plt  #python画图工具
import tensorflow as tf
#调用tf.gfile.GFile(路径,读取方式)函数,如果用r去read读取文件,会有编码不配的错误,所以这里我采用rb来read
img_raw_data = tf.gfile.GFile("C:\\Users\\huanshangfeng\\Desktop\\tf\\picture\\1.jpeg",'rb').read()

解码图像,并显示图像

sess = tf.InteractiveSession() #创建会话,等价于with tf.Session() as sess: 
img_data = tf.image.decode_jpeg(img_raw_data) #对jpeg格式的图像进行解码,若是png格式,则使用tf.image.decode_png(未解码的数据),结果为张量
plt.imshow(resized.eval())
plt.show()
ecoded_image = tf.image.encode_png(img_data) #压缩编码
with tf.gfile.GFile("保存地址","wb") as f:
    f.write(ecoded_image.eval())  #写入文件

改变图片的大小(通过算法)

#使用tf.image.resize_images(图片张量,改变后大小,调整方法)
resized1 = tf.image.resize_images(img_data,size=[1000,1000],method=0)#双线性插值法
resized2 = tf.image.resize_images(img_data,size=[1000,1000],method=1)#最近邻居法
resized3 = tf.image.resize_images(img_data,size=[1000,1000],method=2)#双三次插值法
resized4 = tf.image.resize_images(img_data,size=[1000,1000],method=3)#面积插值法
print(resized1.get_shape())  #可以获取图片打下的信息

改变图片大小(通过剪裁)

#当所修改的尺寸大于原始图像尺寸时,使全零填充,当所修改的尺寸小于原始图像尺寸时,自动截取原始图像居中部分
croped = tf.image.resize_image_with_crop_or_pad(resized,500,500)
plt.imshow(croped.eval())
plt.show()
padded = tf.image.resize_image_with_crop_or_pad(resized,2000,2000)
plt.imshow(padded.eval())
plt.show()

尺寸小于:
在这里插入图片描述
尺寸大于:
在这里插入图片描述

图像翻转

img_data = tf.image.decode_jpeg(img_raw_data)  #解码
flipped = tf.image.flip_up_down(img_data)#上下翻转
flipped = tf.image.flip_left_right(img_data)#左右翻转
transped = tf.image.transpose_image(img_data)#对角线翻转
flipped = tf.image.random_flip_up_down(img_data)#左右随机翻转
flipped = tf.image.random_flip_left_right(img_data)#上下随机翻转

上下翻转
在这里插入图片描述
左右翻转
在这里插入图片描述
对角线:

在这里插入图片描述

图像色彩调整(亮度)

#调用tf.image.adjust_breghtness(图像矩阵,调整系数(大于负1小于1)
#小于-1后,就是全黑的图像,大于1则是全白图像
adjusted = tf.image.adjust_brightness(img_data,-0.3)
adjusted = tf.image.adjust_brightness(img_data,0.6)
#随机在[-max_delta,max_delta)范围随机调整图像的亮度,记住此处是random
adjusted = tf.image.random_brightness(img_data,max_delta)

-0.3图像
在这里插入图片描述
0.6图像
在这里插入图片描述

图像色彩调整(对比度)

#调用tf.image.adjust_contrast(图像矩阵,对比度)
adjust = tf.image.adjust_contrast(img_data,-5)
adjust = tf.image.adjust_contrast(img_data,5)
#在一个给定的范围内进行随机变换,[lower,upper],值得注意的是lower>=0;
adjust = tf.image.random_contrast(img_data,0,5)

-5的图像
在这里插入图片描述
5的图像
在这里插入图片描述

图像色彩调整(色相)

#函数tf.image.adjust_hue(图像矩阵,系数)
adjust = tf.image.adjust_hue(img_data,0.3)
#在[0,max_delat]范围内变化
adjust = tf.image.random_hue(img_data,max_delat)

图像色彩调整(饱和度)

adjusted = tf.image.adjust_saturation(img_data,-5)
adjusted = tf.image.adjust_saturation(img_data,5)
#同样,最小值大于等于零
adjusted = tf.image.random_saturation(img_data,0,5)

标准化图像

adjusted = tf.image.per_image_standardization(img_data) #均值为0,方差为1
adjusted = tf.clip_by_value(adjusted, 0.0, 1.0) #为了显示出图像,矩阵不应该有负数,故此处调整为01之间

在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值