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 图像上绘制边界框。图像值应为 [0, 255] 范围内的 uint8 或 [0, 1] 范围内的浮点数。如果 fill 为 True,则生成的 Tensor 应另存为 PNG 图像。
- 参数:
image (Tensor) – 形状为 (C, H, W) 且 dtype 为 uint8 或 float 的 Tensor。
boxes (Tensor) – 大小为 (N, 4) 的 Tensor,包含 (xmin, ymin, xmax, ymax) 格式的边界框。请注意,框是相对于图像的绝对坐标。换句话说:0 <= xmin < xmax < W 且 0 <= ymin < ymax < H。
labels (List[str]) – 包含边界框标签的列表。
colors (color 或 颜色列表, 可选) – 包含框的颜色或所有框的单一颜色的列表。颜色可以表示为 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 (color 或 颜色列表, 可选) – 标签文本的颜色。有关详细信息,请参阅 colors 参数的描述。默认为框使用的相同颜色。
- 返回:
绘制了边界框的 dtype 为 uint8 的图像 Tensor。
- 返回类型:
img (Tensor[C, H, W])
使用
draw_bounding_boxes
的示例