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) Tensor [源代码]¶
在给定的 RGB 图像上绘制边界框。图像值应为 uint8 类型,范围 [0, 255];或 float 类型,范围 [0, 1]。如果 fill 为 True,结果 Tensor 应保存为 PNG 图像。
- 参数:
image (Tensor) – 形状为 (C, H, W) 且数据类型为 uint8 或 float 的 Tensor。
boxes (Tensor) – 形状为 (N, 4) 的 Tensor,包含 (xmin, ymin, xmax, ymax) 格式的边界框。请注意,边界框是相对于图像的绝对坐标。换句话说:0 <= xmin < xmax < W 且 0 <= ymin < ymax < H。
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 参数的描述。默认为用于框的颜色。
- 返回:
绘制有边界框的 uint8 数据类型的图像 Tensor。
- 返回类型:
img (Tensor[C, H, W])
使用
draw_bounding_boxes
的示例