Data Science

Getting Started with OpenCV in Ubuntu 20.40

Open Source Computer Vision Library, or OpenCV, is a machine learning software library of open source programming utilities used for real-time computing. It is composed of over 2,500 well-optimized algorithms used to detect and recognize faces, for 3D modeling, and for overlapping photos to create maps and elevations, carry out photometric analysis, object trajectory tracking, motion tracking, and detection of gestures and movements. Coupled with other utilities, OpenCV can even assess the intentions of the subject in a photograph.

With such a wide array of functionalities, OpenCV has found its uses over the years in facial recognition technology, medical imagery, surveillance video, and even in commercial services, such as VFX and motion tracking in the film industry.

This article provides a tutorial that shows you how to install OpenCV 4 on your Ubuntu 20.40 system and how to get the most out of this library by installing the most recommended packages.

We will begin by installing the OpenCV dependencies before installing OpenCV.

Step 1: Update Your System

First, open the terminal and type in the following to update your system:

$ sudo apt update && upgrade

Step 2: Download Additional Libraries

Next, request for and install the developer tools and visual I/O libraries to view and edit image and video files. Use the following commands to do so:

$ sudo apt install build-essential cmake unzip pkg-config

$ sudo apt install libjpeg-dev libpng-dev libtiff-dev

$ sudo apt install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev

$ sudo apt install libxvidcore-dev libx264-dev

To further enhance OpenCV, install the packages below:

$ sudo apt install libatlas-base-dev gfortran

Step 3: Download Gnome ToolKit

Gnome Toolkit (GTK) is a free and open-source widget toolkit that can be used to create GUIs. Type the command below to download the Gnome Toolkit:

$ sudo apt install libgtk-3-dev

Step 4: Install Python 3 Development Headers

Use the following codes to install the Python 3 development headers:

$ sudo apt-get install python3-dev

Note: If you could not already tell, we are performing the installation with Python 3 bindings.

With the dependencies installed, we can now move on to downloading OpenCV.

Step 5: Download OpenCV

Downloading OpenCV is fairly straightforward. Simply head over to your home folder and download the OpenCV and opencv_contrib, which comes with some mods that add to OpenCV’s functionality. To do so, enter the following:

$ cd ~
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.0.0.zip

$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.0.0.zip

Next, extract the archives with the commands below:

$ unzip opencv.zip

$ unzip opencv_contrib.zip

This step is optional. You can choose to rename the directories by entering the commands below:

$ mv opencv-4.0.0 opencv

$ mv opencv_contrib-4.0.0 opencv_contrib

Step 6: Optimize Python 3 VE for OpenCV

In this step, we will install pip. Enter the following commands to do so:

$ wget https://bootstrap.pypa.io/get-pip.py

$ sudo python3 get-pip.py

Step 7: Get Used to the VE for Python Development

With a virtual environment, you can work with two parallel software version dependencies.

Here, we will use the virtualenv and virtualenvwrapper packages to navigate the Python virtual environments.

To install the virtualenv and virtualenvwrapper packages and create python VEs with them, enter the following:

$ sudo pip install virtualenv virtualenvwrapper

$ sudo rm -rf ~/get-pip.py ~/.cache/pip

Enter the following code to append your ~/.bashrc file:

$ echo -e "\n# virtualenv and virtualenvwrapper" >> ~/.bashrc

$ echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrc

$ echo "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3" >> ~/.bashrc

$ echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc

Now, source the~/.bashrc file with the following:

$ source ~/.bashrc

Step 8: Creating a Virtual Environment

Now, we can move on to creating an OpenCV 4 + Python 3 virtual environment. Enter the following code to create a Python 3 Virtual Environment.

$ mkvirtualenv cv -p python3

As you can see, we named our VE ‘cv;’ You can give any name to your VE, although it is preferable to keep them short and relevant.

We have created the py3cv4 VE to use with Python 3 + OpenCV 4. To see what VE you are in, enter the following command:

$ workon cv

Step 9: Install NumPy

NumPy is a Python library package that adds to it by installing high-level mathematical functions and supporting multi-dimensional arrays and matrices. It is likely to come in handy when you are using OpenCV, so we recommend that you install it. Issue the following command to do so:

$ pip install numpy

Step 10: Link OpenCV 4 with the Virtual Environment

Look up the Python version installed on your system by entering the following:

$ workon cv

$ python –version

Now that you know the Python version that you are using, you can rework the site-packages directory.

Step 11: Verifying the Installation

You can check that OpenCV is installed properly and will work without problems by entering the following command:

$ workon cv

$ python

This activates the virtual environment and runs the Python interpreter linked with the VE you just activated.

Conclusion

And that about wraps it up. This article provided a walkthrough to help you get OpenCV up and running on your Ubuntu system. We looked at various dependencies that add more functionality to OpenCV and showed you how to install these dependencies. We also created a Python 3 Virtual Environment and linked it with our OpenCV install. After following the steps listed in this article, you should have successfully installed OpenCV on your Ubuntu 20.24 server.

About the author

Younis Said

I am a freelancing software project developer, a software engineering graduate and a content writer. I love working with Linux and open-source software.