Draw geometric shapes on images using OpenCV
Last Updated :
04 Jan, 2023
OpenCV provides many drawing functions to draw geometric shapes and write text on images. Let's see some of the drawing functions and draw geometric shapes on images using OpenCV.
Some of the drawing functions are :
cv2.line() : Used to draw line on an image.
cv2.rectangle() : Used to draw rectangle on an image.
cv2.circle() : Used to draw circle on an image.
cv2.putText() : Used to write text on image.
To demonstrate the uses of the above-mentioned functions we need an image of size 400 X 400 filled with a solid color (black in this case). Inorder to do this, We can utilize numpy.zeroes function to create the required image.
Python3
# Python3 program to draw solid-colored
# image using numpy.zeroes() function
import numpy as np
import cv2
# Creating a black image with 3 channels
# RGB and unsigned int datatype
img = np.zeros((400, 400, 3), dtype = "uint8")
cv2.imshow('dark', img)
# Allows us to see image
# until closed forcefully
cv2.waitKey(0)
cv2.destroyAllWindows()
Output :
Now, let's draw some geometric shapes on this solid black image.
Draw a line :
cv2.line(imageObjectName, ('start_coordinates'), ('end_coordinates'), ('color_in_bgr'), 'line_thickness')
Python3
# Python3 program to draw line
# shape on solid image
import numpy as np
import cv2
# Creating a black image with 3 channels
# RGB and unsigned int datatype
img = np.zeros((400, 400, 3), dtype = "uint8")
# Creating line
cv2.line(img, (20, 160), (100, 160), (0, 0, 255), 10)
cv2.imshow('dark', img)
# Allows us to see image
# until closed forcefully
cv2.waitKey(0)
cv2.destroyAllWindows()
Output :
Draw a rectangle :
cv2.rectangle(imageObjectName, ('top_left_vertex_coordinates'), ('lower_right_vertex_coordinates'), ('stroke_color_in_bgr'), 'stroke_thickness')
Python3
# Python3 program to draw rectangle
# shape on solid image
import numpy as np
import cv2
# Creating a black image with 3
# channels RGB and unsigned int datatype
img = np.zeros((400, 400, 3), dtype = "uint8")
# Creating rectangle
cv2.rectangle(img, (30, 30), (300, 200), (0, 255, 0), 5)
cv2.imshow('dark', img)
# Allows us to see image
# until closed forcefully
cv2.waitKey(0)
cv2.destroyAllWindows()
Output :

Draw a Circle :
cv2.circle(imageObjectName, ('center_coordinates'), ('circle_radius'), ('color_in_bgr'), 'stroke_thickness')
Python3
# Python3 program to draw circle
# shape on solid image
import numpy as np
import cv2
# Creating a black image with 3
# channels RGB and unsigned int datatype
img = np.zeros((400, 400, 3), dtype = "uint8")
# Creating circle
cv2.circle(img, (200, 200), 80, (255, 0, 0), 3)
cv2.imshow('dark', img)
# Allows us to see image
# until closed forcefully
cv2.waitKey(0)
cv2.destroyAllWindows()
Output :

Writing text :
cv2.putText(imageObjectName, 'TextContent', ('text_starting_point_coordinates'), 'fontToBeUsed', 'font_size', ('text_color', 'text_thickness', 'line_type')
Python
# Python3 program to write
# text on solid image
import numpy as np
import cv2
# Creating a black image with 3
# channels RGB and unsigned int datatype
img = np.zeros((400, 400, 3), dtype = "uint8")
# writing text
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, 'GeeksForGeeks', (50, 50),
font, 0.8, (0, 255, 0), 2, cv2.LINE_AA)
cv2.imshow('dark', img)
# Allows us to see image
# until closed forcefully
cv2.waitKey(0)
cv2.destroyAllWindows()
Output :
Applications of drawing shapes on images :
- Drawing geometrical shapes can help us highlight the particular portions of an image.
- Geometrical shapes like line can help us point or identify particular regions in image.
- Writing text on certain regions of images can add description to that region.
Reference :
https://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html
Similar Reads
How to Detect Shapes in Images in Python using OpenCV? OpenCV is an open source library used mainly for processing images and videos to identify shapes, objects, text etc. It is mostly used with python. In this article we are going to see how to detect shapes in image. For this we need cv2.findContours() function of OpenCV, and also we are going to use
3 min read
Drawing with Mouse on Images using Python-OpenCV OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human. In this article, we
3 min read
Python | Detect corner of an image using OpenCV OpenCV (Open Source Computer Vision) is a computer vision library that contains various functions to perform operations on Images or videos. OpenCV library can be used to perform multiple operations on videos. Let's see how to detect the corner in the image. cv2.goodFeaturesToTrack() method finds N
2 min read
Python | Detect Polygons in an Image using OpenCV Approach : The approach we would be used to detect the shape of a given polygon will be based on classifying the detected shape on the basis of a number of sides it has. For example, if the detected polynomial has 3 sides, then it could be considered as a triangle, if the polynomial has 4 sides then
2 min read
Draw Multiple Rectangles in Image using Python-Opencv In this article, we are going to see how to draw multiple rectangles in an image using Python and OpenCV. Function used:imread(): In the OpenCV, the cv2.imread() function is used to read an image in Python. Syntax: cv2.imread(path_of_image, flag) rectangle(): In the OpenCV, the cv2.rectangle functio
2 min read