draw_bounding_boxes¶
- torchvision.utils.draw_bounding_boxes(image: Tensor, boxes: Tensor, labels: Optional[list[str]] = None, colors: Optional[Union[list[Union[str, tuple[int, int, int]]], str, tuple[int, int, int]]] = None, fill: Optional[bool] = False, width: int = 1, font: Optional[str] = None, font_size: Optional[int] = None, label_colors: Optional[Union[list[Union[str, tuple[int, int, int]]], str, tuple[int, int, int]]] = None, fill_labels: bool = False) Tensor [源代码]¶
在给定的 RGB 图像上绘制边界框。图像值应为 uint8(范围 [0, 255])或 float(范围 [0, 1])。如果 fill 为 True,则结果 Tensor 应保存为 PNG 图像。
- 参数:
image (Tensor) – Shape 为 (C, H, W),dtype 为 uint8 或 float 的 Tensor。
boxes (Tensor) – Size 为 (N, 4) 或 (N, 8) 的 Tensor,包含边界框。对于 (N, 4),格式为 (xmin, ymin, xmax, ymax),边界框是相对于图像的绝对坐标。换句话说:0 <= xmin < xmax < W 和 0 <= ymin < ymax < H。对于 (N, 8),格式为 (x1, y1, x2, y2, x3, y3, x4, y4),边界框是相对于底层对象的绝对坐标,因此无需验证后两个不等式。
labels (List[str]) – 包含边界框标签的列表。
colors (颜色 或 颜色列表, 可选) – 包含边界框颜色的列表或所有边界框的单一颜色。颜色可以表示为 PIL 字符串,例如 “red” 或 “#FF00FF”,或者表示为 RGB 元组,例如
(240, 10, 157)
。默认情况下,会为边界框生成随机颜色。fill (bool) – 如果为 True,则使用指定的颜色填充边界框。
width (int) – 边界框的宽度。
font (str) – 包含 TrueType 字体的文件名。如果在此文件名中找不到文件,加载器可能还会搜索其他目录,例如 Windows 上的 fonts/ 目录或 macOS 上的 /Library/Fonts/、/System/Library/Fonts/ 和 ~/Library/Fonts/。
font_size (int) – 以磅为单位的请求字体大小。
label_colors (颜色 或 颜色列表, 可选) – 标签文本的颜色。有关详细信息,请参阅 colors 参数的说明。默认为与边界框相同的颜色,或者如果
fill_labels
为 True,则为黑色。fill_labels (bool) – 如果为 True,则使用指定的边界框颜色(来自
colors
参数)填充标签背景。默认值:False。
- 返回:
绘制了边界框的图像 Tensor,dtype 为 uint8。
- 返回类型:
img (Tensor[C, H, W])
使用
draw_bounding_boxes
的示例