TensorFlow笔记1——图像处理函数

本文详细介绍使用TensorFlow进行图像处理的多种技术,包括图像编码、大小调整、翻转、色彩和对比度调整,以及处理标注框的方法。通过具体代码实例,展示了如何实现图像的解码、调整尺寸、随机翻转、色彩调整以及绘制标注框等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

1、图像编码处理:

#!/usr/bin/env python 
# -*- coding:utf-8 -*-
import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt
import tensorflow as tf

image_raw_data = tf.gfile.FastGFile("F:/test.jpg", 'rb').read()

with tf.Session() as  sess:
    img_data = tf.image.decode_jpeg(image_raw_data)

    #print (img_data.eval()) 输出三维矩阵

    #plt.imshow(img_data.eval())
    #plt.show()  可视化图像

    #将表示一张图像的三维矩阵重新保存,与原始图像一样
    encoded_image = tf.image.encode_jpeg(img_data)
    with tf.gfile.GFile("F:/python/test.jpg","wb") as  f:
        f.write(encoded_image.eval())

2、图像大小调整

#!/usr/bin/env python 
# -*- coding:utf-8 -*-
import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt
import tensorflow as tf

image_raw_data = tf.gfile.FastGFile("F:/test.jpg", 'rb').read()

with tf.Session() as  sess:
    img_data = tf.image.decode_jpeg(image_raw_data)#图像解码
    img_data = tf.image.convert_image_dtype(img_data, dtype=tf.float32)#将整数转换成实数
    resized = tf.image.resize_images(img_data, [300,300], method=0)
    #croped = tf.image.resize_image_with_crop_or_pad(img_data, 200, 200)#剪裁图像
    #padded = tf.image.resize_image_with_crop_or_pad(img_data, 700, 700)#填充图像
    #central_cropped = tf.image.central_crop(img_data, 0.5)#按比例额剪裁图像

    #print (img_data.eval()) #输出三维矩阵

    plt.imshow(resized.eval())
    plt.show()  #可视化图像

 3、图像翻转

    #flipped = tf.image.flip_up_down(img_data)#上下翻转
    #flipped = tf.image.flip_left_right(img_data)#左右翻转
    #transposed = tf.image.transpose_image(img_data)#沿对角线翻转
    flipped = tf.image.random_flip_left_right(img_data)#随机翻转

 4、图像色彩调整

    #adjusted = tf.image.adjust_brightness(img_data, -0.5)图像亮度减0.5
    #adjusted = tf.clip_by_value(adjusted, 0.0, 1.0)#将图像亮点截断在0.0-1.0之间
    adjusted = tf.image.random_brightness(img_data, 1.0)#随机调整亮度

5、图像对比度调整

    #adjusted = tf.image.adjust_contrast(img_data, 0.5)#调整对比度
    #adjusted = tf.image.random_brightness(img_data, 0.5, 5)

 6、处理标注框

    img_data = tf.image.decode_jpeg(image_raw_data)#图像解码
    img_data = tf.image.convert_image_dtype(img_data, dtype=tf.float32)#将整数转换成实数
    resized = tf.image.resize_images(img_data, [180, 267], method=0)
    batched = tf.expand_dims(tf.image.convert_image_dtype(resized, tf.float32), 0)
    boxes = tf.constant([[[0.1, 0.1, 0.8, 0.9]]])
    result = tf.image.draw_bounding_boxes(batched, boxes)
    plt.imshow(result[0].eval())
    plt.show()  # 可视化图像

重点注意: plt.imshow(result[0].eval())

    img_data = tf.image.decode_jpeg(image_raw_data)#图像解码
    img_data = tf.image.convert_image_dtype(img_data, dtype=tf.float32)#将整数转换成实数
    resized = tf.image.resize_images(img_data, [180, 267], method=0)
    batched = tf.expand_dims(tf.image.convert_image_dtype(resized, tf.float32), 0)
    boxes = tf.constant([[[0.05, 0.05, 0.9, 0.7],[0.35, 0.47, 0.5, 0.56]]])
    begin, size, bbox_for_draw = tf.image.sample_distorted_bounding_box(
        tf.shape(img_data), bounding_boxes=boxes, min_object_covered=0.4
    )
    result = tf.image.draw_bounding_boxes(batched, bbox_for_draw)

    plt.imshow(result[0].eval())
    plt.show()  # 可视化图像

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值