Draw Shapes Using OpenCV – A Complete How-To Guide

Feautured Img Draw On Image OpenCV

Hey there! Have you ever wished to draw on the matplotlib plots that you plot every other day? Well, the wish gets fulfilled in this tutorial right here! Today, we will be learning how to draw various objects on the plots.

Let’s Begin!

Also read: Live Sketch Using Webcam with Python OpenCV [Easy Guide]


Introduction to OpenCV

Firstly, OpenCV comes with many drawing functions to draw geometric shapes and even write text on images.

Draw On Image Sample
Draw On Image Sample

Before anything else, let’s start by introducing the drawing functions that we are going to use in the tutorial right here.

The functions are listed below:

  1. cv2.line: This function is used to draw a straight line on the image which starts at a specified (x, y) pair of coordinates and end at another (x, y) pair of coordinates.
  2. cv2.circle: This function is used to draw a circle on an image specified by the center given by (x, y) co-ordinates and also the length of radius.
  3. cv2.rectangle: This function is helpful to draw a rectangle on an image specified by the top-left corner and bottom-right corner in form of (x, y)-coordinates.

Drawing Shapes in Python with OpenCV

Now that we are clear with what magic is going to happen by end of this tutorial, let’s work on our magic!

Step 1: Import Modules/Libraries

In this step, we need to import all the necessary modules and libraries needed for drawing on the images using OpenCV. The obvious module is cv2 and along with this, we have two supporting modules, i.e. numpy and matoplotlib modules.

Finally, we are going to change the plot style to seaborn to get cleaner plots.

import cv2
import numpy as np
from matplotlib import pyplot as plt
plt.style.use('seaborn')

Step 2: Creating a Black Canvas to Draw on

In order to draw anything, we need a canvas to draw on. In this case, we will use an image of size 512 x 512 filled with a single solid color (black in this case).

In order to achieve this, We make use of the numpy. zeroes and set all pixel values to zero in order to have them all black in color.

canvas = np.zeros((512,512,3), np.uint8) 
plt.axis('off')
plt.title("Blank Canvas")
plt.imshow(cv2.cvtColor(canvas, cv2.COLOR_BGR2RGB))
plt.show()
Original Blank Canvas
Original Blank Canvas

Step 3: Drawing a line on the Canvas

In order to draw a line, we will be using cv2.line function which requires a number of properties which include the name of the canvas object created, starting and ending coordinates of the straight line, the color of the line using the RGB tuples.

Have a look at the code mentioned below to get a diagonal green line on your canvas.

canvas = np.zeros((512,512,3), np.uint8)
cv2.line(canvas, (0,0), (511,511), (0,255,0),50)
plt.imshow(cv2.cvtColor(canvas, cv2.COLOR_BGR2RGB))
plt.title('Green Straight Linear line')
plt.axis('off')
plt.show()
Straight Line Canvas OpenCV
Straight Line Canvas OpenCV

Step 4: Drawing a Rectangle on the Canvas

In order to draw the rectangle, we make use of the cv2.rectangle method. The method is identical to the cv2.line method and takes the following properties of the rectangle:

  1. Canvas on which rectangle is being drawn
  2. Top left cordinates of the rectangle
  3. Bottom right coordinates of the rectangle
  4. Mention the color of the rectangle in RGB tuple form
  5. The last argument is the thickness of the border of the rectangle

The code and output for the same is shown below.

canvas = np.zeros((512,512,3), np.uint8)
cv2.rectangle(canvas, (100,100), (300,250), (255,0,0), 20)
plt.imshow(cv2.cvtColor(canvas, cv2.COLOR_BGR2RGB))
plt.title('Blue Rectangle on the Canvas')
plt.axis('off')
plt.show()
Rectangle Canvas OpenCV
Rectangle Canvas OpenCV

Now, what if we want a completely filled rectangle. In order to achieve that we make the thickness negative or using cv2.FILLED keyword.

canvas = np.zeros((512,512,3), np.uint8)
cv2.rectangle(canvas, (100,100), (300,250), (0,0,255), -1)
plt.imshow(cv2.cvtColor(canvas, cv2.COLOR_BGR2RGB))
plt.title('Red Filled Rectangle on the Canvas')
plt.axis('off')
plt.show()
Filled Rectangle Canvas OpenCV
Filled Rectangle Canvas OpenCV

Step 5: Drawing a Circle on the Canvas

In order to draw a circle, we make use of the cv2.circle method. The method needs the following properties:

  1. Canvas on which circle is being drawn
  2. Center of the circle that needs to be drawn
  3. The radius of the circle
  4. Mention the color of the circle in RGB tuple form
  5. The last argument is the thickness of the border of the circle

The code and output for the same are shown below.

canvas = np.zeros((512,512,3), np.uint8)
cv2.circle(canvas, (350, 350), 100, (15,75,50), 20) 
plt.imshow(cv2.cvtColor(canvas, cv2.COLOR_BGR2RGB))
plt.title('Olive Color Circle Drawn')
plt.axis('off')
plt.show()
Circle Canvas OpenCV
Circle Canvas OpenCV

Drawing a filled circle is similar to drawing a filled rectangle on the canvas. Look at the code and output below.

canvas = np.zeros((512,512,3), np.uint8)
cv2.circle(canvas, (350, 350), 100, (155,175,250), -1) 
plt.imshow(cv2.cvtColor(canvas, cv2.COLOR_BGR2RGB))
plt.title('Peach Color Filled Circle Drawn')
plt.axis('off')
plt.show()
Filled Circle Canvas OpenCV
Filled Circle Canvas OpenCV

Complete Code to Draw Shapes in Python Using OpenCV

Now that we have learned to draw basic shapes on the canvas. Let’s visualize all the plots with the help of subplots using the code mentioned below.

import cv2
import numpy as np
from matplotlib import pyplot as plt
plt.style.use('seaborn')
plt.figure(figsize=(10,10))

canvas = np.zeros((512,512,3), np.uint8) 

plt.subplot(3,3,1)
plt.axis('off')
plt.title("Blank Canvas")
plt.imshow(cv2.cvtColor(canvas, cv2.COLOR_BGR2RGB))

plt.subplot(3,3,2)
canvas = np.zeros((512,512,3), np.uint8)
cv2.line(canvas, (0,0), (511,511), (0,255,0),50)
plt.imshow(cv2.cvtColor(canvas, cv2.COLOR_BGR2RGB))
plt.title('Green Straight Linear line')
plt.axis('off')

plt.subplot(3,3,3)
canvas = np.zeros((512,512,3), np.uint8)
cv2.rectangle(canvas, (100,100), (300,250), (255,0,0), 20)
plt.imshow(cv2.cvtColor(canvas, cv2.COLOR_BGR2RGB))
plt.title('Blue Rectangle on the Canvas')
plt.axis('off')

plt.subplot(3,3,4)
canvas = np.zeros((512,512,3), np.uint8)
cv2.rectangle(canvas, (100,100), (300,250), (0,0,255), -1)
plt.imshow(cv2.cvtColor(canvas, cv2.COLOR_BGR2RGB))
plt.title('Red Filled Rectangle on the Canvas')
plt.axis('off')

plt.subplot(3,3,5)
canvas = np.zeros((512,512,3), np.uint8)
cv2.circle(canvas, (350, 350), 100, (15,75,50), 20) 
plt.imshow(cv2.cvtColor(canvas, cv2.COLOR_BGR2RGB))
plt.title('Olive Color Circle Drawn')
plt.axis('off')

plt.subplot(3,3,6)
canvas = np.zeros((512,512,3), np.uint8)
cv2.circle(canvas, (350, 350), 100, (155,175,250), -1) 
plt.imshow(cv2.cvtColor(canvas, cv2.COLOR_BGR2RGB))
plt.title('Peach Color Filled Circle Drawn')
plt.axis('off')

plt.tight_layout()
plt.show()
Final Output Draw On Canvas
Final Output Draw On Canvas

Conclusion

I hope you understood the concept and loved the outputs. Try making scenery or a cartoon character using the same basic shapes and get amazed with the results.

Happy Coding! 😇

Want to learn more? Check out the tutorials mentioned below:

  1. Matplotlib Subplots – Plot Multiple Graphs Using Matplotlib
  2. Matplotlib Plotting Tips
  3. Pygame: Creating Interactive Shapes