Tensorflow图像预处理之图像编码,解码,resize,crop,pad

tensorflow.image常见API

图像预处理之图像编码,解码,resize图像大小,crop 和pad

目录

  1. image_raw_data = tf.gfile.FastGFile(image_path,‘rb’).read() #获得原始数据

  2. img_data = tf.image.decode_jpeg(image_raw_data)#解码

  3. ** tf.image.encode_jpeg(img_data)** #编码

  4. with tf.gfile.GFile(“3.jpg”,‘wb’) as f:
    f.write(encoded_image.eval()) #编码之后保存

  5. ** tf.image.resize_images(img_data,[224,224],method=0)**
    #对解码 后数据resize,resized后的imshow显示不出来,需转换np.arrary格式显示

  6. tf.image.convert_image_dtype(resized,dtype=tf.uint8)
    #tensorfow处理之后是float类型,但是tf转换int之后依然显示不出图片,但是转换numpy之后可以显示

  7. resized_np = np.asarray(resized.eval(),dtype=‘uint8’)

  8. tf.image.resize_image_with_crop_or_pad(img_data,224,224) #crop图片

  9. tf.image.resize_image_with_crop_or_pad(img_data,600,600) #padding图片

  10. tf.image.central_crop(img_data,0.6) 中间截取图片

  11. tf.image.flip_up_down(img_data) 上下翻转图片

  12. tf.image.random_flip_up_down(img_data) #以一定概率上下翻转图片

  13. tf.image.flip_left_right(img_data) #左右翻转图片

  14. tf.image.random_flip_left_right(img_data) #以一定概率左右翻转图片

  15. tf.image.transpose_image(img_data) #对角线翻转图片

  16. tf.image.adjust_brightness(img_data, -0.4) #将图片的亮度-0.4

  17. tf.image.adjust_brightness(img_data, 0.4) #将图片的亮度+0.4

  18. tf.image.random_brightness(img_data, max_delta=0.8) #在[-0.8,0.8]之间随机调整图片的亮度

  19. tf.image.adjust_contrast(img_data, -8) #将图片的对比度-8

  20. tf.image.adjust_contrast(img_data, 8) #将图片的对比度+8

  21. tf.image.random_contrast(img_data, 0.3, 0.8) #将对比度在[0.3,0.8]之间随机调整

  22. tf.image.adjust_hue(img_data, 0.3) #调整图片的色相

  23. tf.image.random_hue(img_data, 0.5) #在[ -a,a]的范围随机调整图片的色相。a的取值在[0, 0.5]之间,
    注意:a<=0.5

  24. tf.image.adjust_saturation(img_data, -7) #将图片的饱和度-7

  25. tf.image.random_saturation(img_data, 0,8) #在[0,8]之间随机调整饱和度

  26. 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]]

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值