import matplotlib.pyplot as plt
import tensorflow as tf
import tensorflow.compat.v1 as tf1
import sys
import importlib
importlib.reload(sys)
tf1.disable_eager_execution()
default_encodeing = 'gbk'
image = tf1.gfile.FastGFile("D:/公用程序部分/tensor/1.jpg","rb").read()
#函数功能:实现对图片的读取,r代表解码方式为uft-8,rb为非utf-8
with tf1.Session() as sess:
img_after_decode = tf.image.decode_jpeg(image)
print(img_after_decode )
print(img_after_decode.eval())
plt.imshow(img_after_decode.eval())
plt.show()
flipped = tf.image.random_flip_left_right (img_after_decode )
#以一定概率翻转图片
tf.image.random_flip_up_down(image,seed)
#以一定概率上下翻转图片。
tf.image.flip_up_down(image)
#将图像进行上下翻转
tf.image.flip_left_right(image)
#将图像进行左右翻转
tf.image.transpose_image(image)
#将图像进行对角线翻转
1、亮度
adjusted_brightness = tf.image.random_brightness(img_after_decode,max_delta=1)
#函数原型random_brightness(image,max_delta,seed)
#max_delta的值不能为负,函数会在[-max_delta,max_delta]值之间随机调整图像的亮度
adjust_brightness(image,delta)
#说明:delta参数为正值则图像的亮度会增加,为负值则图像的亮度会降低
#将dalta设为大于1或小于-1的值是没有意义的,这时图像会变成一致的颜色
2、对比度
tf.image.random_contrast(img_after_decode, 0.2,18, )
#函数原型random_contrast(image,lower,upper,seed)
#函数会在[lower upper]之间随机调整图像的对比度
#但要注意参数lower和upper都不能为负
adjust_contrast(images,contrast_factor)
#参数contrast_factor可为正或为负,正值会增加对比度,负值会降低对比度
#在数值为整数时效果明显
标准化:亮度均值变为0,方差变为1.
tf.image.per_image_standardization()
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
image = tf.gfile.FastGFile("/home/jiangziyang/images/duck.png", 'r').read()
with tf.Session() as sess:
img_after_decode = tf.image.decode_png(image)
#函数原型resize_images(images,size,method,align_corners)
resized = tf.image.resize_images(img_after_decode, [300, 300], method=3)
print(resized.dtype)
#打印的信息
# 从print的结果看出经由resize_images()函数处理图片后返回的数据是float32格式的,
# 所以需要转换成uint8才能正确打印图片,这里使用np.asarray()存储了转换的结果
resized = np.asarray(resized.eval(), dtype="uint8")
plt.imshow(resized)
plt.show()
其中method = 0,1,2,3
0代表双线性插值法
1代表最近邻居法
2代表双三次插值法
3代表面积插值法
import matplotlib.pyplot as plt
import tensorflow as tf
image = tf.gfile.FastGFile("/home/jiangziyang/images/duck.png", 'r').read()
with tf.Session() as sess:
img_after_decode = tf.image.decode_png(image)
#函数原型resize_image_with_crop_or_pad(image,target_height,target_width)
#裁剪图像
croped = tf.image.resize_image_with_crop_or_pad(img_after_decode, 300, 300)
#填充图像
padded = tf.image.resize_image_with_crop_or_pad(img_after_decode, 1000, 1000)
#用pyplot显示结果
plt.imshow(croped.eval())
plt.show()
plt.imshow(padded.eval())
plt.show()
import matplotlib.pyplot as plt
import tensorflow as tf
image = tf.gfile.FastGFile("/home/jiangziyang/images/duck.png", 'r').read()
with tf.Session() as sess:
img_after_decode = tf.image.decode_png(image)
# 函数原型central_crop(image,central_fraction)
central_cropped = tf.image.central_crop(img_after_decode, 0.4)
plt.imshow(central_cropped.eval())
plt.show()