【TensorFlow】数据处理(对图像的处理)

这篇博客详细介绍了在TensorFlow中如何进行图像处理,包括解码、大小调整、翻转、色彩调整和处理标注框。提供了图像预处理的实例代码,帮助降低不相关因素对训练模型的影响。
摘要由CSDN通过智能技术生成

项目已上传至 GitHub —— img-pre

1. 目录结构

images/ 文件夹下存放将被用于处理的图像,img_all.py 示范了 TensorFlow 中图像处理函数的使用方法,img_pre.py 给出了一个对图像进行预处理的程序示例:

img-pre/
    images/
        1.jpg
    img_all.py
    img_pre.py

2. 图像处理函数

2.1 编码处理

在使用图像之前需要先对图像进行解码,将图像转为像素矩阵,tensorflow.image 提供了图像解码函数,返回的矩阵数据类型是 uint8:

  • decode_jpeg:解码 JPEG 格式图像
  • decode_and_crop_jpeg:解码并裁剪 JPEG 格式图像
  • decode_png:解码 PNG 格式图像
  • decode_gif:解码 GIF 格式图像
  • decode_bmp:解码 BMP 格式图像
  • decode_image:自动检测图像格式进行解码,注意 GIF 格式图像返回一个四维矩阵

以下代码示范对 JPEG 格式图像的解码处理:

import tensorflow as tf
import matplotlib.pyplot as plt

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


# 使用pyplot显示图像
def show(img_data):
    plt.imshow(img_data.eval())
    plt.show()


with tf.Session() as sess:
    # 将原始数据解码成多维矩阵
    img_data = tf.image.decode_jpeg(image_raw_data)
    print(img_data.eval())
    show(img_data)

    # 将图像的矩阵编码成图像并存入文件
    encoded_image = tf.image.encode_jpeg(img_data)
    with tf.gfile.GFile('images/output.jpg', 'wb') as f:
        f.write(encoded_image.eval())

    # 将图像数据的类型转为实数类型,便于对图像进行处理
    img_data = tf.image.convert_image_dtype(img_data, dtype=tf.float32)

2.2 大小调整

tf.image.resize_images 函数中有四种调整图像的方法,结果会有细微差别:

method 图像大小调整算法
0 双线性插值法(Bilinear interpolation)
1 最近邻居法(Nearest neighbor interpolation)
2 双三次插值法(Bicubic interpolation)
3 面积插值法(Area interpolation)

以下代码都是在图像编码处理代码的基础下运行,省去了加载原始图像,定义会话等过程:

    # 用resize_images调整图像大小
    # 第一个参数为原始图像
    # 第二个参数为调整后的图像大小[new_height,new_width],跟旧版本分为两个参数不一样
    # method参数给出了调整图像大小的算法
    resized = tf.image.resize_images(img_data, [300, 300], method=0)
    print(resized.get_shape())  # 图像深度没有显式指定则为问号
    show(resized)

还可以通过 resize_image_with_crop_or_pad 函数对图像进行裁剪或填充:

    # 用resize_image_with_crop_or_pad调整图像大小
    # 第一个参数为原始图像
    # 第二个和第三个参数是调整后的图像大小,大于原图则填充,小于则裁剪居中部分
    croped = tf.image.resize_image_with_crop_or_pad(img_data, 200, 200)
    show(croped)
    padded =<
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值