OpenCV (Open Source Computer Vision Library) is a widely used open source library for computer vision and image processing. Installing OpenCV4 on your system offers several advantages, especially if you are involved in the development of projects related to computer vision. In this article we see how to install the latest version of OpenCV4 on Ubuntu 23, after compiling it on this system.
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.
sudo apt update sudo apt upgrade
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
sudo apt install build-essential cmake unzip pkg-config
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.
sudo apt install libjpeg-dev libpng-dev libtiff-dev sudo apt install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
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).
sudo apt install libgtk-3-dev
Then we add two more libraries needed for OpenCV mathematical optimization: Fortran and Atlas.
sudo apt install libatlas-base-dev gfortran
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.
sudo apt install python3-dev
Download of OpenCV 4 source code
To install OpenCV 4 on our system two different packages are needed:
So let’s start by downloading these packages directly to our file system.
cd ~ wget -O opencv.zip https://github.com/opencv/opencv/archive/refs/tags/4.8.0.zip wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/refs/tags/4.8.0.zip
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
mv opencv-4.8.0 opencv mv opencv_contrib-4.8.0 opencv_contrib
Configuring the Python3-OpenCV4 virtual environment
When 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.
apt install python3-pip sudo pip3 install virtualenv 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.
sudo nano ~/.bashrc
scroll to the end of the file, and right at the bottom add the following lines of code.
#virtualenv and virtualenvwrapper
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
mkvirtualenv cv4 -p python3
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.
(cv4) user@hostname: ~$
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.
(cv4)$ pip install 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
cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D INSTALL_C_EXAMPLES=OFF \ -D OPENCV_ENABLE_NONFREE=ON \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \ -D PYTHON_EXECUTABLE=~/.virtualenvs/cv4/bin/python \ -D BUILD_EXAMPLES=ON ..
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.
sudo make install
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.
(cv4)$ cd ~/.virtualenvs/cv4/lib/python3.10/site-packages (cv4)$ ln -s /usr/local/python3/cv2 cv2 (cv4)$ cd ~
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,
>>> import cv2
If the version is displayed correctly then the installation was performed perfectly