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()