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 uses of the above-mentioned functions we need an image of size 400 X 400 filled with a solid colour (black in this case). Inorder to do this, We can utilize numpy.zeroes function to create the required image.
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’)
Draw a rectangle :
cv2.rectangle(imageObjectName, (‘top_left_vertex_coordinates’), (‘lower_right_vertex_coordinates’), (‘stroke_color_in_bgr’), ‘stroke_thickness’)
Draw a Circle :
cv2.circle(imageObjectName, (‘center_coordinates’), (‘circle_radius’), (‘color_in_bgr’), ‘stroke_thickness’)
Writing text :
cv2.putText(imageObjectName, ‘TextContent’, (‘text_starting_point_coordinates’), ‘fontToBeUsed’, ‘font_size’, (‘text_color’, ‘text_thickness’, ‘line_type’)
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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Arithmetic Operations on Images using OpenCV | Set-2 (Bitwise Operations on Binary Images)
- Draw Color Filled Shapes in Turtle - Python
- Python | Create video using multiple images using OpenCV
- Erosion and Dilation of images using OpenCV in python
- Stitching input images (panorama) using OpenCV with C++
- Addition and Blending of images using OpenCV in Python
- Python | Denoising of colored images using opencv
- Arithmetic Operations on Images using OpenCV | Set-1 (Addition and Subtraction)
- Python | Grayscaling of Images using OpenCV
- Python - Process images of a video using OpenCV
- Drawing with Mouse on Images using Python-OpenCV
- Measure similarity between images using Python-OpenCV
- Concatenate images using OpenCV in Python
- Python OpenCV - Depth map from Stereo Images
- Draw a triangle with centroid using OpenCV
- Find and Draw Contours using OpenCV | Python
- Draw a rectangular shape and extract objects using Python's OpenCV
- OpenCV - Facial Landmarks and Face Detection using dlib and OpenCV
- Python Tkinter | Create different shapes using Canvas class
- Calculating Areas Of Different Shapes Using Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.