Fundamentals of OpenCV in Python

From installation to basic image manipulation

Leonardo Godói
6 min readMar 17, 2022

OpenCV is a popular open-source computer vision library available for different programming languages, such as Python, C++, and JavaScript. It provides a rich set of tools for processing and analyzing images and videos, allowing you from resizing a single picture to building a complex object recognition application. The Python programming language, in addition to have been widely adopted as a standard for most of the image processing and computer vision applications, allows OpenCV to be installed in your computer in a really straightforward way.

In this tutorial, I will show you the steps required to install, setup and perform some basic manipulations on images using OpenCV, highlighting important fundamentals regarding the concepts behind this library.

Before we start, it is worthy mentioning that these steps were performed using Python3 in a Linux-based operating system. So, if you are using different configurations, minor changes may be necessary.

Setting up a virtual environment

When working with Python, it is often recommended to run your applications within a virtual environment. By doing that, you can install only the packages you need to run your application, making it independent from the rest of the system. In case you still do not have a virtual environment tool installed on your computer, I recommend you to install the following.

 $ python3 -m pip install virtualenv

Once installed, you can use this tool to create a new virtual environment. For this, navigate to your desired directory and run the following command. Note that I chose the name my_venv for my environment, but you can choose any name you want.

$ python3 -m venv my_venv

Finally, you just have to activate your virtual environment, so that anything you run in this terminal will be done within it.

$ source my_venv/bin/activate

Installing OpenCV

Start by updating your package manager and installing OpenCV for Python3.

$ sudo apt update
$ sudo apt install python3-opencv

Then, install OpenCV in your virtual environment using pip.

$ pip install opencv-python

Opening an image

Using your favorite IDE, create a new file with extension .py and start by importing the OpenCV class.

import cv2

The first thing we do here is to load an image using the imread() method and open it using the imshow() method. Please replace the path below by the path where your image is located.

image = cv2.imread("messi.jpg")
cv2.imshow("Original image", image)
Original image

The first interesting point about how OpenCV interprets images is that imread() returns an n-dimensional array object. In the case of an RGB image, such as the one used in this example, this array has the shape:

[width × height × number of channels]

You can confirm this by printing the image shape.

print("Original image shape:", image.shape)

Changing the image color space

A color space is a range of colors on a spectrum that is used to describe an image. Although there are several color space available, the most common are RGB and grayscale. OpenCV contains a particular method for color space conversion. In order to convert a image from RGB to grayscale, you need to apply the cvtColor() method.

image_grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow(“Grayscale image”, image_grayscale)
Grayscale image

It is important to note that now your image no more contains three dimensions. That is because the third dimension, corresponding to channels, is not used in the grayscale color space.

print("Grayscale image shape:", image_grayscale.shape)

Editing image channels

Returning to the original image, containing three color channels, you can now manipulate the intensity values of each of them in order to suppress or highlight a specific channel. Contrary to what is usually assumed, the RGB channels are not accessed in the same order as in the acronym. In OpenCV, the channels are actually available in the BGR format:

0 = B (blue)

1 = G (green)

2 = R (red)

In the example below, I retrieve all the pixels in the first two channels and make them zero, so that the green and blue channels will take no effect on the resulting image, which will consequently assume an exclusively red tone.

image_red = image.copy()
image_red[:, :, :2] = 0
cv2.imshow("Red image", image_red)
Image with null blue and green channels

When printing the red image shape, note that there is still a third dimension, otherwise the resulting image would be described as a grayscale.

print("Red image shape:", image_red.shape)

Rotating the image

In addition to colors, it is also possible to perform manipulations on the image shape, e.g., size and orientation. You can start by obtaining the image dimensions just like you would do in a regular NumPy object.

width, height, _ = image.shape

The center points of the image can then be calculated by taking the half of its width and height.

center = (width//2, height//2)

In order to rotate the image, you have first to create a rotation matrix, passing as arguments the center points, the desired rotation in degrees, and the image scale, respectively. Using a scale different from 1 would change the image size.

rotation_matrix = cv2.getRotationMatrix2D(center, 45, 1.0) 

The rotation matrix object itself is not enough to do what we want. It is passed as an argument to the warpAffine() method, which is what actually performs the rotation.

image_rotated = cv2.warpAffine(image, rotation_matrix, (width, height))
cv2.imshow(“Rotated image”, image_rotated)
Rotated image

Resizing the image

You can use the height and width values obtained previously to define the new dimensions for the image. In the example below I set them to reduce the image to a third of its original size.

new_size = (width//3, height//3)

The method in charge of resizing the image is resize(), which needs as arguments the original image object, the desired new size, and the interpolation method, respectively.

image_resized = cv2.resize(image, new_size, interpolation=cv2.INTER_LINEAR)
cv2.imshow(“Resized image”, image_resized)
Resized image

You can then check the dimensions of the resized image.

print(“Resized image shape:”, image_resized.shape)

Inserting drawings into the image

In some applications, it may be necessary to add markers to the image. the OpenCV class provides multiple methods for drawing lines and shapes on it. A line is added as follows. The arguments here are the original image, the initial position, the final position, the line color RGB code, and the line width. Note that, actually, a copy of the original image is used since the drawing methods can affect the original image.

image_line = cv2.line(image.copy(), (0, 0), (height//2, width//2), (0, 255, 255), 10)
cv2.imshow(“Image with line”, image_line)
Image with a line

Similarly, there is a specific method for drawing rectangles in the image. In this case, the second and the third arguments are the positions of the rectangle opposite vertices (top left and bottom right).

image_rectangle = cv2.rectangle(image.copy(), (500, 150), (900, 550), (0, 255, 255), 5)
cv2.imshow(“Image with rectangle”, image_rectangle)
Image with a rectangle

You can also draw a circle by specifying its center position and its radius as the second and third arguments, respectively.

image_circle = cv2.circle(image.copy(), (300, 300), 200, (0, 255, 255), 5)
cv2.imshow(“Image with circle”, image_circle)
Image with a circle

The full script

The script generated in this tutorial is available on the following repository.

https://github.com/lfgodoi/tutorials

To finish

Finally, I would like to list links to interesting content for those who want to go deeper into the computer vision with OpenCV.

--

--

Leonardo Godói

Control and automation engineer, master, and currently pursuing a PhD in Artificial Intelligence. Interested in how technology can help to explore the universe.