tensorflow.image常见API
图像预处理之图像编码,解码,resize图像大小,crop 和pad
目录
-
image_raw_data = tf.gfile.FastGFile(image_path,‘rb’).read() #获得原始数据
-
img_data = tf.image.decode_jpeg(image_raw_data)#解码
-
** tf.image.encode_jpeg(img_data)** #编码
-
with tf.gfile.GFile(“3.jpg”,‘wb’) as f:
f.write(encoded_image.eval()) #编码之后保存 -
** tf.image.resize_images(img_data,[224,224],method=0)**
#对解码 后数据resize,resized后的imshow显示不出来,需转换np.arrary格式显示 -
tf.image.convert_image_dtype(resized,dtype=tf.uint8)
#tensorfow处理之后是float类型,但是tf转换int之后依然显示不出图片,但是转换numpy之后可以显示 -
resized_np = np.asarray(resized.eval(),dtype=‘uint8’)
-
tf.image.resize_image_with_crop_or_pad(img_data,224,224) #crop图片
-
tf.image.resize_image_with_crop_or_pad(img_data,600,600) #padding图片
-
tf.image.central_crop(img_data,0.6) 中间截取图片
-
tf.image.flip_up_down(img_data) 上下翻转图片
-
tf.image.random_flip_up_down(img_data) #以一定概率上下翻转图片
-
tf.image.flip_left_right(img_data) #左右翻转图片
-
tf.image.random_flip_left_right(img_data) #以一定概率左右翻转图片
-
tf.image.transpose_image(img_data) #对角线翻转图片
-
tf.image.adjust_brightness(img_data, -0.4) #将图片的亮度-0.4
-
tf.image.adjust_brightness(img_data, 0.4) #将图片的亮度+0.4
-
tf.image.random_brightness(img_data, max_delta=0.8) #在[-0.8,0.8]之间随机调整图片的亮度
-
tf.image.adjust_contrast(img_data, -8) #将图片的对比度-8
-
tf.image.adjust_contrast(img_data, 8) #将图片的对比度+8
-
tf.image.random_contrast(img_data, 0.3, 0.8) #将对比度在[0.3,0.8]之间随机调整
-
tf.image.adjust_hue(img_data, 0.3) #调整图片的色相
-
tf.image.random_hue(img_data, 0.5) #在[ -a,a]的范围随机调整图片的色相。a的取值在[0, 0.5]之间,
注意:a<=0.5 -
tf.image.adjust_saturation(img_data, -7) #将图片的饱和度-7
-
tf.image.random_saturation(img_data, 0,8) #在[0,8]之间随机调整饱和度
-
tf.image.per_image_standardization(img_data) #图片像素数字均值变为0,方差变为1,此处在显示图片之前需要把数值clip[0.,1.]之间。
使用 after_img = tf.clip_by_value(per_image_standardization, 0.0, 1.0)
图片的编码和解码
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
image_path = "0bc58747-9a2e-43e1-af1f-cf0a41f9f2ba.png"
image_raw_data = tf.gfile.FastGFile(image_path,'rb').read()
# plt.imshow(image_raw_data)
with tf.Session() as sess:
#图片的解码
img_data = tf.image.decode_jpeg(image_raw_data)
print(img_data.eval())
print(img_data.eval().shape)
plt.imshow(img_data.eval())
plt.show()
# #把图像的像素值转换到[0,1]之间
# img_data = tf.image.convert_image_dtype(img_data,dtype=tf.float32)
# print(img_data.eval())
#编码过程,并保存编码的结果
encoded_image = tf.image.encode_jpeg(img_data)
with tf.gfile.GFile("3.jpg",'wb') as f:
f.write(encoded_image.eval())
图片解码tf.image.decode_jpeg后输出的结果:
//图片解码后 img_data.eval()的输出结果
[[[228 220 217]
[223 215 212]
[224 216 213]
...
[246 230 215]
[247 232 216]
[250 235 218]]
[[223 215 212]
[217 209 206]
[217 209 206]
...
[243 225 208]
[243 226 209]
[246 229 209]]
[[223 215 212]
[217 209 206]
[217 210 207]
...
[240 223 207]
[243 226 209]
[245 228 209]]
..