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

来源:互联网 发布:浙江师范大学数据库 编辑:程序博客网 时间:2024/06/05 07:48

添加标注框

tf.image.draw_bounding_boxes

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

#!/usr/bin/python# coding:utf-8import matplotlib.pyplot as pltimport 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-8import matplotlib.pyplot as pltimport 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()

这里写图片描述

阅读全文
0 0
原创粉丝点击