一、函数定义
tf.image.draw_bounding_boxes(
images,
boxes,
name=None
)
Draw bounding boxes on a batch of images.
在一个batch的每一张图片上绘制边界框。
Outputs a copy of images
but draws on top of the pixels zero or more bounding boxes specified by the locations in boxes
. The coordinates of the each bounding box in boxes are encoded as [y_min, x_min, y_max, x_max]
. The bounding box coordinates are floats in [0.0, 1.0]
relative to the width and height of the underlying image.
输出images
的一个副本,在图片上像素上绘制boxes
中由位置指定的零个或多个边界框的。 框中每个边界框的坐标编码为[y_min,x_min,y_max,x_max]
。 边界框坐标相对于底层图像的宽度和高度浮动在[0.0,1.0]
中。
For example, if an image is 100 x 200 pixels (height x width) and the bounding box is [0.1, 0.2, 0.5, 0.9]
, the upper-left and bottom-right coordinates of the bounding box will be (40, 10)
to (180, 50)
(in (x,y) coordinates).
例如,如果图像为100 x 200像素(高x宽)并且边界框为“[0.1,0.2,0.5,0.9]”,则边界框的左上角和右下角坐标为(40, 10)
到(180, 50)
(在(x,y)坐标中)。
Parts of the bounding box may fall outside the image.
边界框的某些部分可能会落在图像之外。
需要注意的是边界框的坐标信息是相对于长宽的小数,而不是实际的像素坐标。
二、参数
参数 | ||
---|---|---|
images | A Tensor . Must be one of the following types: float32 , half . 4-D with shape [batch, height, width, depth] . A batch of images. | 一个Tensor ,数据类型必须为float32 , half 。4-D形状[batch, height, width, depth] 。 表示一个batch的图片。 |
boxes | A Tensor of type float32 . 3-D with shape [batch, num_bounding_boxes, 4] containing bounding boxes. | 一个Tensor ,数据类型为float32 ,shape为 [batch, num_bounding_boxes, 4] 的包含边界框的秩为3的矩阵。 |
name | A name for the operation (optional). | 名称(可选参数) |
三、代码
原图如下:
import tensorflow as tf
import cv2
import numpy as np
tf.enable_eager_execution()
img = cv2.imread("timg.jpeg") / 255.0
bbox = [[0, 0.1, 0.5, 0.5], [0.6, 0.7, 0.9, 1.0]]
bbox_img = tf.image.draw_bounding_boxes([img], [bbox])
cv2.imwrite('bbox.jpg', np.asarray(bbox_img.numpy()[0] * 255, np.uint8))
结果如下: