Dark Light
We will toggle through a few ways of drawing objects with OpenCV in Python. It’s an introductory subject, but it’s very useful in daily computer vision activities.

Discover the basics of OpenCV with Python!

Computer vision is at the center of my focus today. It has so many use cases and opportunities that I will be posting quick tutorials about computer vision and deep learning in general.

In this tutorial, we’ll explore the basics of OpenCV – an open source library written in C++ with bindings in Python, Java and Matlab. Python is our choice.

This is a foundations tutorial – mastering OpenCV is essential for computer vision,

We will toggle through a few ways of drawing objects with OpenCV in Python. It’s an introductory subject, but it’s very useful in daily computer vision activities. Enough talk, let’s get our hands dirty.

Let’s get the party started

Soon to be updated:

  • Add the Google Colab of this lesson
  • Add the GitHub repository

Assuming that you’ve already setup Python 3.x in your system and installed OpenCV (Google it if you haven’t. I also recommend creating a specific Conda environment for dealing with OpenCV). If you want to get going quickly, Google Colab is also a good option. Log into your Google account and visit Colab:

http://colab.research.google.com/

  1. Create a folder that will serve as our project repository.
  2. Create a Python file with a similar name to basic_drawing.py

In basic_drawing, we will first import NumPy and OpenCV and declare our initial image as a numpy array:

import numpy as np
import cv2

canvas = np.zeros((300, 300, 3), dtype="uint8")

Note that zeros is a NumPy function that returns an array filled with 0s. Also, it has three channels (Red, Green and Blue). We’ve defined the datatype (uint8) as 8-bit unsigned integer.

Let’s draw a line:

green = (0, 255, 0) # tuple to define the color green
cv2.line(canvas, (0,0), (300,300), green)

cv2.imshow("Canvas", canvas)
cv2.waitKey(0)

By using BGR, we’ll define the green color and declare the function cv2.line to draw a line into our canvas image, starting in the (0, 0) x, y coordinates and ending in the (300, 300) coordinates, color green.

By executing the entire file, either by a Google Colab cell or by executing the file on terminal, you should have the following output:

Exciting, right? Well, knowing this makes a huge difference while you advance in computer vision.

In the next block of code, we will draw a thicker diagonal line, this time in red:

red = (0, 0, 255)
cv2.line(canvas, (300,0), (0,300), red, 3) # 3 pixel thick
cv2.imshow("Canvas", canvas)
cv2.waitKey(0)

Note that the last argument we’ve passed into the cv2.line function was the thickness (in pixels). You should now be able to see a thicker red line in the opposite direction of the green line.

The syntax of cv2.line is quite simple:

cv2.line(image, starting_point, ending_point, color, thickness)

Remember that we’ve created a 300×300 array? That’s why it starts in 0 (duh) and end at 300.

Drawing rectangles is fairly easy also. OpenCV offers a native function as well:

cv2.rectangle(image, starting_point, ending_point, color, thickness)

cv2.rectangle(canvas, (10,10), (60,60), green)
cv2.imshow("Canvas", canvas)
cv2.waitKey(0)

Here, we will draw a 50-pixel rectangle, starting in the coordinates (10, 10) and ending in (60, 60). The color will be green, since we already have it declared above.

This should be your output:

By following the same logic we did above, we can define the pixel thickness as the last argument passed into the function cv2.rectangle.

Circles. As geometric primitives, circles can also be drawn by a native OpenCV function: cv2.circle.

Before drawing a circle, let’s clear out our Canvas image – since we’re executing the same code along the Python script, we’re bound to its complete execution (unless you comment all line and rectangle functions above). Let’s reinitialise our canvas variable:

canvas = np.zeros((300, 300, 3), dtype="uint8")

Good. Now, we’ll declare two variables – centerX and centerY – they represent the (x,y)-coordinates of our image’s center. We will calculate the center by examining the shape of our NumPy array and then diving by two:

  • The number of rows is canvas.shape[0] and represents the height of the image
  • The number of columns is canvas.shape[1] and represents the width of the image

Also, we’ll declare a BGR white pixel.

(centerX, centerY) = (canvas.shape[1] // 2, 
    canvas.shape[0] // 2)
white = (255, 255, 255)

To draw a circle, we have to iterate through many radius values, and we should start at 0 and end at 150 px (since the 1/2 of our image array is 150 px). Note also the syntax of our for statement: going from 0 to 175 by adding 25, without including 175, so it stops at 150.

for r in range(0, 175, 25):
    # draw a white circle with the current radius size
    cv2.circle(canvas, (centerX, centerY), r, white)
cv2.imshow("Canvas", canvas)
cv2.waitKey(0)

This should be your output:

The syntax of the cv2.circle() function is: cv2.circle(image, center_coordinates, radius, color, thickness)

We’ve passed into this function (centerX, centerY), therefore the circles will be centered at the center of the image.

Quick tip: to really master what’s going on behind the scenes, you should start playing with all these functions. That’s what I do everytime.

Leave a Reply

Your email address will not be published. Required fields are marked *