TensorFlow学习--tensorflow图像处理--添加标注框

添加标注框

tf.image.draw_bounding_boxes

tensorflow通过tf.image.draw_bounding_boxes函数加入标注框.

#!/usr/bin/python
# coding:utf-8

import matplotlib.pyplot as plt
import tensorflow as tf
# 读取图像数据
img = tf.gfile.FastGFile('daibola.jpg').read()

with tf.Session() as sess:
    img_data = tf.image.decode_jpeg(img)
    # tf.image.draw_bounding_boxes要求图像矩阵中的数字为实数
    # 利用tf.image.convert_image_dtype将图像矩阵转化为实数
    batched = tf.expand_dims(tf.image.convert_image_dtype(img_data, tf.float32), 0)
    # 边界框坐标是相对于宽度和宽度在[0.0,1.0]内的浮点数,即这里给出的都是图像的相对位置[0.1, 0.2, 0.8, 0.8]即(0.1*wide, 0.2*high)到(0.8*wide, 0.8*high)
    boxes = tf.constant([[[0.1, 0.2, 0.8, 0.8]]])
    # 在图像上绘制边界框
    result = tf.image.draw_bounding_boxes(batched, boxes)

    plt.subplot(121), plt.imshow(img_data.eval()), plt.title('original')
    plt.subplot(122), plt.imshow(result[0].eval()), plt.title('result')

    plt.show()

输出:

这里写图片描述

图像比较大时,部分边界框可能会出现显示问题.可以先将图像缩小一些,让标注清楚.

 resize = tf.image.resize_images(img_data, (200, 350), method=1)
tf.image.sample_distorted_bounding_box

通过tf.image.sample_distorted_bounding_box函数可以实现图像的随机截取.通常用于随机截取图像上由信息量的部分以提高模型的鲁帮性/robustness.使训练出的模型不受识别物体的大小的影响.

#!/usr/bin/python
# coding:utf-8

import matplotlib.pyplot as plt
import tensorflow as tf
# 读取图像数据
img = tf.gfile.FastGFile('daibola.jpg').read()

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

    boxes = tf.constant([[[0.2, 0.2, 0.9, 0.7]]])
    # 为图像生成一个随机扭曲的边界框
    # 通过提供标注框的方式来告诉随机截取图像的范围
    begin, size, bbox_for_draw = tf.image.sample_distorted_bounding_box(tf.shape(img_data), bounding_boxes=boxes)
    # 在张量形状中插入1个维度
    batched1 = tf.expand_dims(tf.image.convert_image_dtype(img_data, tf.float32), 0)
    # 在图像上绘制边界框
    image_with_box = tf.image.draw_bounding_boxes(batched1, bbox_for_draw)
    # 从张量中提取切片
    distorted_image = tf.slice(img_data, begin, size)

    plt.subplot(121), plt.imshow(img_data.eval()), plt.title('original')
    plt.subplot(122), plt.imshow(distorted_image.eval()), plt.title('result')

    plt.show()

这里写图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值