VOOZH about

URL: https://www.geeksforgeeks.org/python/how-to-draw-bounding-boxes-on-an-image-in-pytorch/

⇱ How to draw bounding boxes on an image in PyTorch? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to draw bounding boxes on an image in PyTorch?

Last Updated : 22 Apr, 2022

In this article, we are going to see how to draw bounding boxes on an image in PyTorch.

draw_bounding_boxes() method

The draw_bounding_boxes function helps us to draw bounding boxes on an image. With tensor we provide shapes in [C, H, W], where C represents the number of channels and  H, W represents the height and width respectively, this function returns an Image Tensor with bounding boxes. The bounding box should contain four points in format as (xmin, ymin, xmax, ymax), the top-left point=(xmin, ymin), and bottom-right point = (xmax, ymax). 

Syntax - torch.utils.draw_bounding_boxes(image, box)

Parameter:

  • image: Tensor of shape (C x H x W) 
  • box: Bounding boxes size in (xmin, ymin, xmax, ymax).

Return: Image Tensor of dtype uint8 with bounding boxes plotted.

The below image is used for demonstration:

👁 Image
 

Example 1:

The following example is to know how to change and fill colors in bounding boxes.

Output:

👁 How to draw bounding boxes on an image in PyTorch
 

Example 2:

The following example is to understand how to draw multiple bounding boxes on an image.

Comment
Article Tags: