tensorflow中几个常见的图像处理函数

tensorflow中几个常见的图像处理函数:(在jupyter中实现)
(1)图像编码处理
tensorflow中tf.image.decode_jpeg和tf.image.decode_png分别可以对图像进行jpg格式和png格式的编码

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np

#读取图像的原始数据
image_raw_data=tf.gfile.FastGFile('1.jpg','rb').read()

with tf.Session() as sess:
    #对图像进行jpeg的格式解码从而得到图像对应的三位矩阵
    img_data=tf.image.decode_jpeg(image_raw_data)
    #对图像进行png的格式解码从而得到图像对应的三位矩阵
    #img_data=tf.image.decode_png(image_raw_data)

#解码之后的结果就是一个张量
print(image_data.eval())
#可视化
plt.imshow(image_data.eval())
plt.show()

(2)将图像缩放到指定尺寸
<1>利用双线性插值法对图像进行缩放

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np

#读取图像的原始数据
image_raw_data=tf.gfile.FastGFile('1.jpg','rb').read()
#双线性插值法ResizeMethod.BILINEAR(默认设置),对应method=0
with tf.Session() as sess:
    #对图像进行jpeg的格式解码从而得到图像对应的三位矩阵
    img_data=tf.image.decode_jpeg(image_raw_data)
    #用双线性插值法将图像缩放为指定尺寸
    resized1=tf.image.resize_images(img_data,[256,256],method=0)
    #tensorflow的函数处理图片后存储的数据是float32格式的,需要转换成uint8才能正确打印图片
    resized1=np.asarray(resized1.eval(),dtype='uint8')
    plt.imshow(resized1)
    plt.show()

<2>利用最近邻插值法对图像进行缩放

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np

#读取图像的原始数据
image_raw_data=tf.gfile.FastGFile('1.jpg','rb').read()
##最近邻插值法NEARST_NEIGHBOR,对应method=1
with tf.Session() as sess:
    #对图像进行jpeg的格式解码从而得到图像对应的三位矩阵
    img_data=tf.image.decode_jpeg(image_raw_data)    
    #用最近邻插值法将图像缩放为指定尺寸
    resized2=tf.image.resize_images(img_data,[256,256],method=1)
    resized2=np.asarray(resized2.eval(),dtype='uint8')
    plt.imshow(resized2)
    plt.show()

<3>利用双立方插值法对图像进行缩放

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np

#读取图像的原始数据
image_raw_data=tf.gfile.FastGFile('1.jpg','rb').read()
#双立方插值法BICUBIC,对应method=2
with tf.Session() as sess:
    #对图像进行jpeg的格式解码从而得到图像对应的三位矩阵
    img_data=tf.image.decode_jpeg(image_raw_data)    
    #用双立方插值法将图像缩放为指定尺寸
    resized3=tf.image.resize_images(img_data,[256,256],method=2)
    resized3=np.asarray(resized3.eval(),dtype='uint8')
    plt.imshow(resized3)
    plt.show()

<4>使用像素区域插值法对图像进行缩放

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np

#读取图像的原始数据
image_raw_data=tf.gfile.FastGFile('1.jpg','rb').read()
#像素区域插值法AREA,对应method=3
with tf.Session() as sess:
    #对图像进行jpeg的格式解码从而得到图像对应的三位矩阵
    img_data=tf.image.decode_jpeg(image_raw_data)  
    #用像素区域插值法将图像缩放为指定尺寸
    resized4=tf.image.resize_images(img_data,[256,256],method=3)
    resized4=np.asarray(resized4.eval(),dtype='uint8')
    plt.imshow(resized4)
    plt.show()

(3)图像裁剪或填充
<1>中心裁剪
tf.image.resize_image_with_crop_or_pad(image,target_height,target_width)
如果目标图像尺寸小于原始图像尺寸,则在中心位置剪裁,反之则用黑色像素进行填充

#图像裁剪或填充后缩放
#tf.image.resize_image_with_crop_or_pad(image,target_height,target_width)
#如果目标图像尺寸小于原始图像尺寸,则在中心位置剪裁,反之则用黑色像素进行填充
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np

#读取图像的原始数据
image_raw_data=tf.gfile.FastGFile('1.jpg','rb').read()
with tf.Session() as sess:
    img_data=tf.image.decode_jpeg(image_raw_data)
    croped=tf.image.resize_image_with_crop_or_pad(img_data,400,400)
    plt.imshow(croped.eval())
    plt.show()
    
    padded=tf.image.resize_image_with_crop_or_pad(img_data,2000,2000)
    plt.imshow(padded.eval())
    plt.show()

<2>随机裁剪

#随机裁剪
#tf.image.random_crop(image,size,seed=None,name=None)
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np

#读取图像的原始数据
image_raw_data=tf.gfile.FastGFile('1.jpg','rb').read()
with tf.Session() as sess:
    img_data=tf.image.decode_jpeg(image_raw_data)
    random_corped1=tf.random_crop(img_data,[600,600,3])
    plt.imshow(random_corped1.eval())
    plt.show()

#再次随机裁剪,验证随机性
with tf.Session() as sess:
    img_data=tf.image.decode_jpeg(image_raw_data)
    random_corped2=tf.random_crop(img_data,[600,600,3])
    plt.imshow(random_corped2.eval())
    plt.show()

每次裁剪得到的结果不同
在这里插入图片描述
在这里插入图片描述
(4)图像翻转
<1>水平翻转tf.image.flip_left_right(img_data)

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np

#读取图像的原始数据
image_raw_data=tf.gfile.FastGFile('1.jpg','rb').read()
with tf.Session() as sess:
    img_data=tf.image.decode_jpeg(image_raw_data)
    plt.imshow(img_data.eval())
    plt.axis('off')
    plt.show()
    flip_left_right=tf.image.flip_left_right(img_data)
    plt.imshow(flip_left_right.eval())
    plt.axis('off')
    plt.show()

在这里插入图片描述
在这里插入图片描述
<2>上下翻转tf.image.flip_up_down(img_data)

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np

#读取图像的原始数据
image_raw_data=tf.gfile.FastGFile('1.jpg','rb').read()
with tf.Session() as sess:
    img_data=tf.image.decode_jpeg(image_raw_data)
    plt.imshow(img_data.eval())
    plt.axis('off')
    plt.show()
    flip_up_down=tf.image.flip_up_down(img_data)
    plt.imshow(flip_up_down.eval())
    plt.axis('off')
    plt.show()

在这里插入图片描述
在这里插入图片描述
(5)改变图像的对比度

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np

#读取图像的原始数据
image_raw_data=tf.gfile.FastGFile('1.jpg','rb').read()
with tf.Session() as sess:
    img_data=tf.image.decode_jpeg(image_raw_data)
    plt.imshow(img_data.eval())
    plt.show()
    #将图像的对比度降低至原来的二分之一
    contrast=tf.image.adjust_contrast(img_data,0.5)
    #将图像的对比度提高至原来的5倍
    #contrast=tf.image.adjust_contrast(img_data,5)
    #在[lower,upper]范围内随机调整图像对比度
    #contrast=tf.image.random_contrast(img_data,lower=0.2,upper=3)
    plt.imshow(contrast.eval())
    plt.show()

原始图像
在这里插入图片描述
将图像的对比度降为原来的二分之一
在这里插入图片描述
将图像对比度提高为原来的5倍
在这里插入图片描述
随机调整对比度
在这里插入图片描述
在这里插入图片描述
(6)白化处理
将图像的像素值转化成零均值和单位方差

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np

#读取图像的原始数据
image_raw_data=tf.gfile.FastGFile('1.jpg','rb').read()
#将图像的像素值转化成零均值和单位方差
#tf.image.per_image_standardization(img_data)将图片进行标准化
with tf.Session() as sess:
    img_data=tf.image.decode_jpeg(image_raw_data)
    plt.imshow(img_data.eval())
    plt.show()
    standardization=tf.image.per_image_standardization(img_data)
    plt.imshow(np.asarray(standardization.eval(),dtype='uint8'))
    plt.show()

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值