Draw a rectangle on an image using OpenCV in Python

In this tutorial, we will learn some simple techniques to draw a rectangle on an image using OpenCV in Python. By the end of this tutorial, you will learn how to draw a rectangle and you will also be able to draw other polygons with ease.

This will help to outline the region of interest whenever we want to highlight a particular region. So, let’s begin the tutorial

Install OpenCV

This is the prerequisite to proceed to use OpenCV. If you have not installed it, you can install it by using the below command in the command prompt.

pip install opencv-python

cv2.rectangle() to draw a rectangle on an image in Python

First, we will read the input image using the method cv2.imread(). This method takes the argument as the name of the input image with extension.

img = cv2.imread("caa.JPG")

The input image is

Draw a rectangle on an image in Python using OpenCV

Then we use cv2.rectangle() method. This method takes 5 arguments:

cv2.rectangle(input_image,start_coordinates,end_coordinates,color,thickness)

input_image is the image on which the rectangle is to be drawn.

start_coordinates point to the top corner of the rectangle.

end_coordinates point to the bottom corner of the rectangle.

color is specified in BGR format

thickness is used to specify the width of the outer border.

img = cv2.rectangle(img, (200,450), (450,210), (0,0,255), 3)

The final image with a rectangle is displayed using cv2.imshow(). This method takes 2 arguments. The first is the label of the output image. The second argument is the image that is to be displayed. The image will be displayed until there is an interrupt from the keyboard by the user. This is done by using the cv2.waitKey() method. At last, the window is destroyed by using the method cv2.destroyAllWindows(). Putting everything together we have,

import cv2 
img = cv2.imread("caa.JPG")
img = cv2.rectangle(img, (200,450), (450,210), (0,0,255), 3)
cv2.imshow("rectangle", img) 
cv2.waitKey()
cv2.destroyAllWindows()

Using cv2.line()

From the basic definition of a rectangle, 4 line segments together form a rectangle. The same concept is used in this section. This method takes 5 arguments.

cv2.line(input_image,start_coordinates,end_coordinates,color, thickness)

All the arguments are the same as the arguments of the previous method with a small difference. Here, only a line segment is drawn from start_coordinate to end_coordinate.To form a complete rectangle, we need 4 such line segments.

img = cv2.line(img, (200,480), (440,480), (0,0,255), 3)

Draw lines such that, length=breadth i.e, the distance between 2 sides should be the same and the other 2 sides should also be the same. Then display the final image using the method cv2.imshow(). The final code is

import cv2 
img = cv2.imread("caa.JPG")
img = cv2.line(img, (200,480), (440,480), (0,0,255), 3)
img = cv2.line(img, (200,220), (200,480), (0,0,255), 3)
img = cv2.line(img, (200,220), (440,220), (0,0,255), 3)
img = cv2.line(img, (440,220), (440,480), (0,0,255), 3)
cv2.imshow("rectangle", img) 
cv2.waitKey()
cv2.destroyAllWindows()

Using cv2.polylines()

This method is used to draw multiple lines. This method consists of an array that has the coordinates of all the points to be connected. To access the array, we import NumPy.This method takes 5 arguments

cv2.polylines(input_image,[array],True/False,color,thickness)

input_image is the image on which the rectangle is to be drawn

[array] takes the coordinate values

True/False Indicate whether the figure should be opened or closed. True indicates closed. False indicates open.

color and thickness are the same as the previous methods.

The final code is

import cv2
import numpy as rect
img = cv2.imread("caa.JPG")
coordinates = rect.array([[200,480], [200,220], [440,220], [440,480] ])
cv2.polylines(img, [coordinates], True, (0,0,255),3)
cv2.imshow("rectangle", img)
cv2.waitKey()
cv2.destroyAllWindows()

Output

The final output of all three codes is the same and it is as given below,

Draw a rectangle on an image in Python using OpenCV

 

So we have seen three different ways to draw a rectangle on an image with OpenCV Python.

Leave a Reply

Your email address will not be published. Required fields are marked *