Drawing Polygons and Writing Text on the Images


In the previous tutorials, we have seen the Numpy Image Basics and the Open CV Image processing and got to know about the operations on the images. In this tutorial, we shall look into the drawing of the different shapes and writing the text on the images. These polygons, like rectangles, can be useful in the applications to generate the border when the specific object is detected.

Drawing Rectangles & Circles on to the Image

Now, let us move on to the Jupyter Notebook code implementation and understand it accordingly.

Initially, let us import the CV2, NumPy and Matplotlib libraries.

import cv2
import numpy as np

import matplotlib.pyplot as plt
%matplotlib inline

Let us define the image and store it in the NumPy array, with suitable dimensions. The values in the array can be declared as zeros so that the entire image will be in the black colour.


Then, the implementation of the rectangle on the image is possible, by selecting the two coordinates on the image graph that represents as the opposite vertices of the rectangle. The image value and the thickness of the rectangle can be set with the suitable values.


And the images looks in the similar way, obtained using the Matplotlib image plotting.

image processing

Similarly, with in the same image multiple, rectangles can be drawn.

image processing

Now, let us look into the implementation of the circle on the image. Using the cv2 circle can be drawn by entering the centr coordinate, radius, colour and the thickness values to draw the circle.

image processing

The circle can also be entirely filled with the colour, by setting the thickness to -1.

image processing

Similarly, line segment can also be drawn on to the image, by setting the two end coordinates of the line, colour and thickness values accordingly.

line segment

Writing Text on to the Image

Till now we have seen the drawing of the different shapes on to the image in Open CV. Now, we shall look into the writing of the text on to the image.

Initially, let us load the new image with complete black colour.


To write the text on to the image we need to select the font type initially. Later, the parameters like image, text that has to be written, font selected, scale of the font, color of the text, thickness and the line type are to be selected accordingly.

cv2.putText(blank_image,text='Hello',org=(10,500),fontFace=font, fontScale=4, color=(255,255,255), thickness=3, lineType=cv2.LINE_AA)

Drawing Polygons on to the Image

Finally, we shall look into the drawing of the polygons, on to the image.

Initially, let us load the blank image with complete black colour.


Now, all the coordinates of the vertices has to be stored into the array.


The shape of the array, will will be (4,2), which is not suitable for opencv. Let us change the array into 3 dimensional.


Now, using the cv2 we can obtain the polygon by specifying the image, vertices array, closed or open polygon, colour and thickness.


This completes all bout this tutorial, to draw the different shapes and write text on to the images. In the further upcoming tutorials we shall look into the much more interesting concepts and projects. For, any sort of doubts or the questions regarding this tutorial, you can reach out through the comment box.

Spread knowledge

Leave a Comment

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