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:
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:
Create a folder that will serve as our project repository.
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
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)
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: