Tensorflow细节-P174-真正的图像预处理

注意这里的读取image_raw_data = tf.gfile.FastGFile("./datasets/cat.jpg", "rb").read(),写入f = tf.gfile.GFile("output.png", "wb")所用的函数,注意这里及以下都是调用的tf.image.

import matplotlib.pyplot as plt
import tensorflow as tf
image_raw_data = tf.gfile.FastGFile("./datasets/cat.jpg", "rb").read()
f = tf.gfile.GFile("output.png", "wb")
with tf.Session() as sess:
    img_data = tf.image.decode_jpeg(image_raw_data)
    fig = plt.figure()
    plt.imshow(sess.run(img_data))
    encoded_image = tf.image.encode_jpeg(img_data)

    f.write(encoded_image.eval())

重新调整图片大小API
image_float = tf.image.convert_image_dtype(img_data, tf.float32)
resized = tf.image.resize_images(image_float, [300, 300], method=0)
裁剪或填充图片
croped = tf.image.resize_image_with_crop_or_pad(img_data, 1000, 1000)
padded = tf.image.resize_image_with_crop_or_pad(img_data, 3000, 3000)
截取中间50%的图片
central_cropped = tf.image.central_crop(img_data, 0.5)
翻转图片
transposed = tf.image.transpose_image(img_data)
处理标注框

图像的裁剪区域必须包含所提供的任意一个边界框的至少 min_object_covered 的内容。

import matplotlib.pyplot as plt
import tensorflow as tf
image_raw_data = tf.gfile.FastGFile("./datasets/cat.jpg", "rb").read()

with tf.Session() as sess:
    boxes = tf.constant([[[0.05, 0.05, 0.9, 0.7], [0.35, 0.47, 0.5, 0.56]]])
    img_data = tf.image.decode_jpeg(image_raw_data)
    # sample_distorted_bounding_box要求输入图片必须是实数类型。
    image_float = tf.image.convert_image_dtype(img_data, tf.float32)

    begin, size, bbox_for_draw = tf.image.sample_distorted_bounding_box(
        tf.shape(image_float), bounding_boxes=boxes, min_object_covered=0.4)  # 截取部分至少包含某个标注框40%的内容

    # 截取后的图片
    distorted_image = tf.slice(image_float, begin, size)  # 这是截取的操作
    plt.imshow(distorted_image.eval())
    plt.show()

    # 在原图上用标注框画出截取的范围。由于原图的分辨率较大(2673x1797),生成的标注框
    # 在Jupyter Notebook上通常因边框过细而无法分辨,这里为了演示方便先缩小分辨率。
    image_small = tf.image.resize_images(image_float, [180, 267], method=0)
    batchced_img = tf.expand_dims(image_small, 0)
    image_with_box = tf.image.draw_bounding_boxes(batchced_img, bbox_for_draw)
    print(bbox_for_draw.eval())
    plt.imshow(image_with_box[0].eval())
    plt.show()
posted @ 2019-10-07 17:32  博博的Blog  阅读(204)  评论(0编辑  收藏  举报