OpenCV 4.x installation on Ubuntu 18

For more than a year, there have been some articles published on this site that make use of the fantastic OpenCV 3 library. The new OpenCV 4 version has just been released and as usual we will publish the compilation and installation procedure on Linux Ubuntu 18 systems.

Installiamo OpenCV 4 su Linux Ubuntu 18

The new features of the OpenCV 4 version

In this procedure we will use the version of Python 3.x. By now we must all begin to become familiar with this version, which has now become the official language standard.

Installation of OpenCV 4 dependencies

Before starting the compilation and installation of the OpenCV 4 library, it will be necessary to install some fundamental packages in our system for the various processes that we will see below. Especially some tools needed for compilation.

So the first thing to do is to make sure that our Ubuntu system is perfectly updated. On Ubuntu 18 it is no longer necessary to add apt-get (okay again) but you can use apt directly.

Now that we have updated the system we begin to install these very important development tools that we will need for compiling the OpenCV 4 source code

In addition to the previous packages there are a series of I / O libraries necessary for OpenCV 4 to manage the image and video formats.

Now another important library necessary for a correct compilation of the OpenCV 4 source code is the GTK library, useful for managing the GUI (graphical user interfaces).

Then we add two more libraries needed for OpenCV mathematical optimization: Fortran and Atlas.

And finally, as mentioned above, we will rely on the version of Python 3.x. So if it is not yet installed on your system, the time has come to do it.

Download of OpenCV 4 source code

To install OpenCV 4 on our system two different packages are needed:

  • opencv
  • opencv_contrib

So let’s start by downloading these packages directly to our file system.

Once both ZIP packages have been downloaded, we can extract them.

Once we have extracted their contents, we will find in our working directory two new folders with the relative version in the name. Let’s rename the two packages

Configuring the Python3-OpenCV4 virtual environment

496/5000When you want to compile a complex library like OpenCV4, full of correlations with a wide range of packages and libraries (let’s talk about Python packages), it is good practice to create a specific virtual environment. Virtual environments in Python allow us to work on a project (like OpenCV4 can be) in perfect isolation without affecting in the least other projects in Python that will most likely use a different configuration of packages and versions.

The direct installation of OpenCV4 on the Python default environment would risk compromising the perfect functioning of other previous projects, or preclude other new ones. This is because over time some projects will have shared libraries, some of which will be needed in two different versions. Updating one of these libraries for the operation of a project will often have unwanted consequences on other previous projects.

Instead thanks to the virtual environments you can use all the projects we want, even work with Python2 and Python3 simultaneously, or work with two different versions of OpenCV4. For example, you can leave your old version of OpenCV3 (maybe in Python 2) on your system and install the new OpenCV4 version in a virtual environment.

So we install two packages needed to build and manage virtual environments: virtualenv and virtualenvwrapper.

To complete the installation of these two tools, we need to edit a particular hidden file in our HOME directory. Open the .bashrc file with a text editor.

scroll to the end of the file, and right at the bottom add the following lines of code.

Then save the file and exit the text editor. Now run the program to update these latest changes.

Now let’s create the virtual environment for Python3 + OpenCV4

We have thus created a completely isolated virtual environment, which we can activate each time by running the following command:

We will recognize the activation of the virtual environment from the text on the command line.

As you can see the name of the virtual environment is shown at the beginning between two round brackets.

Numpy installation in the new virtual environment

Just as proof of what has been said previously, we install one of the useful Python packages for OpenCV4: Numpy.

So we will have the Numpy library installed in our virtual environment cv4. This will not be accessible (nor visible) to system default Python and when the virtual environment is removed, it will leave your default Python configuration completely unaffected.

Building OpenCV4 with CMake

Now we can exit our virtual environment and go directly to the compilation of OpenCV4

First we need to configure the OpenCV4 build with CMake

Now we can finally compile. This operation will take plenty of time, so take the opportunity to take a break (maybe a coffee).

In my case I used the -j4 option because my system works on 4 processors. But use the value corresponding to your system. To know the number of processors on your system run the command:

Once the installation is complete, we can install the compiled in our system.

Add the OpenCV4 Link to the virtual environment

Now we have to make sure to add a link to make the compiled OpenCV4 accessible in our virtual environment. So first we activate the virtual environment.

Now that we have entered the virtual environment we can create the symbolic link. First go into the package directory of the Python version you are using in virtualenv.

This step is very important, because without this link it would not be possible to import opencv into Python.

Testing the installation

Now the installation of Open CV 4 on Ubuntu is complete. We can check if everything went correctly,

If the version is displayed correctly then the installation was performed perfectly

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.