Drawing Polygons and Writing Text on the Images
Introduction
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.
blank_image=np.zeros(shape=(512,512,3),dtype=np.int16)
plt.imshow(blank_image)
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.
cv2.rectangle(blank_image,pt1=(384,0),pt2=(510,150),color=(0,255,0),thickness=10)
And the images looks in the similar way, obtained using the Matplotlib image plotting.
plt.imshow(blank_image)
Similarly, with in the same image multiple, rectangles can be drawn.
cv2.rectangle(blank_image,pt1=(200,200),pt2=(300,300),color=(0,0,255),thickness=10)
plt.imshow(blank_image)
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.
cv2.circle(img=blank_image,center=(100,100),radius=50,color=(255,0,0),thickness=10)
plt.imshow(blank_image)
The circle can also be entirely filled with the colour, by setting the thickness to -1.
cv2.circle(img=blank_image,center=(400,400),radius=50,color=(255,0,0),thickness=-1)
plt.imshow(blank_image)
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.
cv2.line(blank_image,pt1=(0,0),pt2=(512,512),color=(102,155,255),thickness=5)
plt.imshow(blank_image)
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.
blank_image=np.zeros(shape=(512,512,3),dtype=np.int16)
plt.imshow(blank_image)
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.
font=cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(blank_image,text='Hello',org=(10,500),fontFace=font, fontScale=4, color=(255,255,255), thickness=3, lineType=cv2.LINE_AA)
plt.imshow(blank_image)
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.
blank_img=np.zeros(shape=(512,512,3),dtype=np.int32)
plt.imshow(blank_img)
Now, all the coordinates of the vertices has to be stored into the array.
vertices=np.array([[100,300],[200,200],[400,300],[200,400]],dtype=np.int32)
The shape of the array, will will be (4,2), which is not suitable for opencv. Let us change the array into 3 dimensional.
pts=vertices.reshape((-1,1,2))
Now, using the cv2 we can obtain the polygon by specifying the image, vertices array, closed or open polygon, colour and thickness.
cv2.polylines(blank_img,[pts],isClosed=True,color=(255,0,0),thickness=5)
plt.imshow(blank_img)
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.