In this article, we’ll talk about Drawing with OpenCV. If you haven’t done so already, be sure to read the previous article on setting pixels with Python OpenCV.
A large part of working with Image Processing has to do with identifying objects and contours in a provided image.
This can be done by marking these shapes during the processing.
But, how do we do so?
The same question that plagued the scientists working on Image Processing led to the development of drawing in OpenCV.
We’ll be using a few of these functions to begin working with drawing in OpenCV in this article.
Note that this is the third article of the Image Processing Series, and it might a good idea to look into the introductory session before proceeding through this article!
Drawing with Python OpenCV
Let’s start drawing with the OpenCV module.
Setting it all up
In order to get started, we’ll first need to import the modules that we’ll be using for drawing in OpenCV.
This, of course, consists of the
cv2 module, but, another module we’ll be using is the
In case you don’t have numpy already installed in your system, here’s the command to install it via pip:
pip install numpy
We’ll then proceed to utilize these modules in the code by importing them into the script:
# importing the numpy module to create a canvas of 0 value pixels. import numpy as np # importing the OpenCV module import cv2
In order to draw anything, we’ll first need to have something to draw upon, so, let’s initialize a canvas to do so.
We’ll be doing this by creating a canvas using the
Numpy module and make one that has 300×300 pixels with a value of each pixel set to 0.
This means that we’ll be presented with a 900 pixel black canvas, as the value for 0 is black in Images.
# setting up a black canvas of 900 pixels canvas = np.zeros((300, 300, 3), dtype="uint8")
We’ll also set up a color to a variable that allows us to use it when we draw objects in the canvas.
# setting the color green to a variable. green = (0, 255, 0)
Now that we’re all set up, let’s get to drawing items on the canvas!
Let’s draw lines
We’ll now draw two lines across both the diagonals using the
To show the difference between thickness, we’ll be changing up the color for the other diagonal.
# To draw a green line across the canvas whose size you have to adjust through the editor? cv2.line(canvas, (0, 0), (300, 300), green) cv2.imshow("Canvas", canvas) cv2.waitKey(0) # Draws a three-inch red line across the other diagonal red = (0, 0, 255) cv2.line(canvas, (300, 0), (0, 300), red, 3) cv2.imshow("Canvas", canvas) cv2.waitKey(0)
The output that you should receive should look sometime like this,
Working with Boxes
Proceeding forwards, we can use the
rectangle() function from the
cv2 module to draw rectangles in the canvas.
# While this command draws a rectangle given the right dimensions cv2.rectangle(canvas, (10, 10), (60, 60), green) cv2.imshow("Canvas", canvas) cv2.waitKey(0) # Draws a rectangle when specified the co-ordinates, with a thickness for the edges cv2.rectangle(canvas, (20, 20), (70, 70), red, 5) cv2.imshow("Canvas Red", canvas) # The duration for which the canvas stays by the defined value cv2.waitKey(0) blue = (255, 0, 0) # -1 thickness causes the entire box to get filled. cv2.rectangle(canvas, (200, 50), (225, 125), blue, -1) cv2.imshow("Canvas Blue", canvas) cv2.waitKey(0)
The output that you should be receiving upon doing so, should look something like this:
Circles and Randomization
Mixing things up, we’ll now proceed to working with circles. Here’s two examples of working with circles.
Here, we are centering the point of origin for all the circles, and drawing them in increments of the radius.
# centers the canvas to the center and forms rings based on the increment of the radius canvas = np.zeros((300, 300, 3), dtype="uint8") (centerX, centerY) = (int(canvas.shape / 2), int(canvas.shape / 2)) white = (255, 255, 255) for r in range(0, 175, 25): cv2.circle(canvas, (centerX, centerY), r, white) cv2.imshow("Canvas White", canvas) cv2.waitKey(0)
The output that you receive, should look something like this,
Here, we are using the
numpy module to randomize the size of the circles and the points at which they will be placed in the canvas.
We’ll also place a
waitkey() such that we can view each circle individually before proceeding to the next one.
# Drawing circles on random with varying radii, # throughout the canvas for i in range(0, 25): radius = np.random.randint(5, high=200) color = np.random.randint(0, high=256, size=(3,)).tolist() pt = np.random.randint(0, high=300, size=(2,)) cv2.circle(canvas, tuple(pt), radius, color, -1) cv2.imshow("Canvas", canvas) cv2.waitKey(0)
Working with the circles, the output that you receive will be random, and as such, it’s better to test it out on your own 😉
Altering existing images
We can alter existing images, by loading them into a variable using the
# reading the image into a variable using the imread() function image = cv2.imread( "D:\\image-processing\\assets\\ey.jpg" ) # drawing three circles and a rectangle on the image cv2.circle(image, (168, 188), 90, (0, 0, 255), -2) cv2.circle(image, (150, 164), 10, (0, 0, 255), -1) cv2.circle(image, (192, 174), 10, (0, 0, 255), -1) cv2.rectangle(image, (134, 200), (186, 218), (0, 0, 255), -1) # showing the output of the process cv2.imshow("Output", image) cv2.waitKey(0)
I’ve input an image which is present in the assets of the repository, but you might wish to use your own image.
This can be done by modifying the image location!
We’ve learnt how to perform drawing with OpenCV. The use cases for working with drawing lines in OpenCV are definitely not useless.
Throughout the course of working with Image Processing, we can draw contours on images, draw boxes while identifying objects, and use it for live tracking objects in videos.
The implementations of drawing in OpenCV allows for us to work with Machine Learning models in the future as well.
Want to look at the future where we work with Image Processing on a larger scale?