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:
Example 1:
The following example is to know how to change and fill colors in bounding boxes.
Python3
# Import the required libraries
import torch
import torchvision
from torchvision.io import read_image
from torchvision.utils import draw_bounding_boxes
# read input image from your computer
img = read_image('a3.png')
# bounding box are xmin, ymin, xmax, ymax
box = [330, 190, 660, 355]
box = torch.tensor(box)
box = box.unsqueeze(0)
# draw bounding box and fill color
img = draw_bounding_boxes(img, box, width=5,
colors="green",
fill=True)
# transform this image to PIL image
img = torchvision.transforms.ToPILImage()(img)
# display output
img.show()
Output:
Example 2:
The following example is to understand how to draw multiple bounding boxes on an image.
Python3
# Import the required libraries
import torch
import torchvision
from torchvision.io import read_image
from torchvision.utils import draw_bounding_boxes
# read input image from your computer
img = read_image('a3.png')
# create boxes
box_1 = [330, 220, 450, 350]
box_2 = [530, 200, 650, 320]
box = [box_1, box_2]
box = torch.tensor(box, dtype=torch.int)
# draw bounding box and fill color
img = draw_bounding_boxes(img, box, width=5, colors=[
"orange", "blue"], fill=True)
# transform this image to PIL image
img = torchvision.transforms.ToPILImage()(img)
# display output
img.show()
Output:
Similar Reads
How to crop an image at center in PyTorch? In this article, we will discuss how to crop an image at the center in PyTorch. CenterCrop() method We can crop an image in PyTorch by using the CenterCrop() method. This method accepts images like PIL Image, Tensor Image, and a batch of Tensor images. The tensor image is a PyTorch tensor with [C,
2 min read
How to convert an image to grayscale in PyTorch In this article, we are going to see how to convert an image to grayscale in PyTorch. torchvision.transforms.grayscale method Grayscaling is the process of converting an image from other color spaces e.g. RGB, CMYK, HSV, etc. to shades of gray. It varies between complete black and complete white. t
2 min read
How to Make a grid of Images in PyTorch? In this article, we are going to see How to Make a grid of Images in PyTorch. we can make a grid of images using the make_grid() function of torchvision.utils package. make_grid() function: The make_grid() function accept 4D tensor with [B, C ,H ,W] shape. where B represents the batch size, C repres
3 min read
How to adjust the contrast of an image in PyTorch In this article, we are going to see how to adjust the contrast of an image in PyTorch using Python. We can adjust the contrast of an image by using the adjust_contrast() method. adjust_contrast() method adjust_contrast() method accepts the PIL and tensor images as input. tensor image is a tensor wi
2 min read
How to pad an image on all sides in PyTorch? In this article, we will discuss how to pad an image on all sides in PyTorch. transforms.pad() method Paddings are used to create some space around the image, inside any defined border. We can set different paddings for individual sides like (top, right, bottom, left). transforms.Pad() method is us
2 min read