tf图像相关处理

tf图像相关处理

"""
图像的预处理过程
"""
import os
import matplotlib.pyplot as plt
import tensorflow as tf


# 读取数据
def get_decode_image(file_name, dtype=tf.uint8, has_eval=False):
    # 读取原始数据
    image_raw_data = tf.gfile.FastGFile(file_name, "rb").read()

    # 解码图片数据得到三维矩阵
    # image_data = tf.image.decode_jpeg(image_raw_data)
    # image_data = tf.image.decode_png(image_raw_data)
    image_data = tf.image.decode_image(image_raw_data)

    # 转换数据类型
    image_data = tf.image.convert_image_dtype(image_data, dtype=dtype)

    if has_eval:
        with tf.Session():
            image_data = image_data.eval()

    return image_data


# 由于图片存在压缩,将一张图像还原成三维矩阵,需要进行解码。
def decode_image_and_save(file_name="data_process.jpg"):
    name, ext = os.path.splitext(file_name)
    # 读取数据
    image_data = get_decode_image(file_name)

    # 编码图片
    encoded_image = tf.image.encode_jpeg(image_data) if ext.lower() == "png" else tf.image.encode_png(image_data)
    ext = "jpg" if ext.lower() == "png" else "png"

    with tf.Session():
        # 保存图片
        with tf.gfile.GFile(name="{}.{}".format(name, ext), mode="wb") as f:
            f.write(encoded_image.eval())

        plt.imshow(image_data.eval())
        plt.show()
    pass


# 图片大小调整:通过算法使新的图像尽量保存原始图像的所有信息。
def resize_image(file_name="data_process.jpg"):
    # 读取数据
    image_data = get_decode_image(file_name, dtype=tf.float32, has_eval=True)

    # 1.调整大小:插值
    resized_images = tf.image.resize_images(image_data, [300, 300], method=tf.image.ResizeMethod.BILINEAR)
    # 2.调整大小:crop or pad
    # resized_images = tf.image.resize_image_with_crop_or_pad(image_data, target_height=500, target_width=500)
    # 3.调整大小:比例
    # resized_images = tf.image.central_crop(image_data, 0.5)
    # 4.调整大小:裁剪
    # resized_images = tf.image.crop_to_bounding_box(image_data, 100, 100, target_height=400, target_width=500)
    # 5.调整大小:填充
    # resized_images = tf.image.pad_to_bounding_box(image_data, 400, 100, target_height=1000, target_width=1200)
    # 6.调整大小:裁剪并resize
    # image_data = tf.expand_dims(image_data, axis=0)
    # resized_images = tf.image.crop_and_resize(image_data, [[0.8, 0.2, 0.5, 0.5]], box_ind=[0], crop_size=[200, 200])
    # resized_images = tf.squeeze(resized_images, axis=0)

    print(resized_images.get_shape())

    # 显示图片
    with tf.Session():
        plt.imshow(resized_images.eval())
        plt.show()
    pass


# 图像翻转
def flip_image(file_name="data_process.jpg"):
    image_data = get_decode_image(file_name, dtype=tf.float32)

    # 翻转
    # flip_image_data = tf.image.flip_up_down(image_data)
    # flip_image_data = tf.image.flip_left_right(image_data)
    # 随机翻转
    # flip_image_data = tf.image.random_flip_left_right(image_data)
    # flip_image_data = tf.image.random_flip_up_down(image_data)
    # 转置
    flip_image_data = tf.image.transpose_image(image_data)

    print(flip_image_data.get_shape())

    with tf.Session():
        plt.imshow(flip_image_data.eval())
        plt.show()
    pass


# 图像色彩调整:亮度、对比度、色相、饱和度
def adjust_color(file_name="data_process.jpg"):
    image_data = get_decode_image(file_name, dtype=tf.uint8, has_eval=True)

    # 亮度
    adjust_image_data = tf.image.adjust_brightness(image_data, 0.5)
    # adjust_image_data = tf.image.random_brightness(image_data, 0.5)

    # 对比度:(x - mean) * contrast_factor + mean
    # adjust_image_data = tf.image.adjust_contrast(image_data, 5)
    # adjust_image_data = tf.image.adjust_contrast(image_data, -5)
    # adjust_image_data = tf.image.random_contrast(image_data, 2, 5)

    # 色相
    # adjust_image_data = tf.image.adjust_hue(image_data, 0.5)
    # adjust_image_data = tf.image.adjust_hue(image_data, -0.5)
    # adjust_image_data = tf.image.random_hue(image_data, 0.4)

    # 饱和度
    # adjust_image_data = tf.image.adjust_saturation(image_data, 5)
    # adjust_image_data = tf.image.adjust_saturation(image_data, 0.4)
    # adjust_image_data = tf.image.random_saturation(image_data, 0.4, 10)

    # 标准化:(x - mean) / adjusted_stddev
    # adjust_image_data = tf.image.per_image_standardization(image_data)

    with tf.Session():
        plt.imshow(adjust_image_data.eval())
        plt.show()
    pass


# 标准化
def standard_image(file_name="data_process.jpg"):
    image_data = get_decode_image(file_name, dtype=tf.uint8, has_eval=True)

    # 标准化:(x - mean) / adjusted_stddev
    standard_image_data = tf.image.per_image_standardization(image_data)

    with tf.Session():
        print(standard_image_data.eval())
    pass


# 处理标注框
def draw_bounding_boxes(file_name="data_process.jpg"):
    image_data = get_decode_image(file_name, dtype=tf.float32, has_eval=True)
    image_data = tf.image.resize_images(image_data, [500, 500], method=tf.image.ResizeMethod.BILINEAR)
    image_data = tf.expand_dims(image_data, axis=0)
    result = tf.image.draw_bounding_boxes(image_data, boxes=[[[0.1, 0.1, 0.2, 0.2], [0.3, 0.3, 0.5, 0.5]]])
    result = tf.squeeze(result, axis=0)
    with tf.Session():
        plt.imshow(result.eval())
        plt.show()
    pass

if __name__ == '__main__':
    draw_bounding_boxes()

二、详细说明

tf.image.crop_and_resize()
官方链接:https://tensorflow.google.cn/api_docs/python/tf/image/crop_and_resize

用法

tf.image.crop_and_resize(
    image,
    boxes,
    box_ind,
    crop_size,
    method='bilinear',
    extrapolation_value=0,
    name=None
)

参数

image:张量。必须是下列类型之一:uint8,uint16,int8,int16,int32,int64,half,float32,float64;
boxes:float32类型张量,指需要划分的区域,输入格式为 [[ymin,xmin,ymax,xmax]] ;
box_ind:int32类型张量;
crop_size:裁剪尺寸;
method:方法,"bilinear", "nearest"。

例子

import tensorflow as tf
import matplotlib.pyplot as plt 
img = plt.imread('F:aa.jpg')
shape = img.shape
img = img.reshape([1,shape[0], shape[1], shape[2]])
a = tf.image.crop_and_resize(img,[[0.5,0.5,0.6,0.2],[0.5,0.5,1.3,0.9]],
                             box_ind=[0,0],crop_size=(100,100))
sess = tf.Session()
b = a.eval(session = sess)
plt.imshow(b[0]/255)
plt.imshow(b[0].astype('uint8'))
img = plt.imread('F:aa.jpg')#代表路径

结果

参考博客:

tf.image.crop_and_resize()使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值