OpenCV (Open Source Computer Vision Library) is a computer vision library released under the BSD license, free for both personal and commercial use. It has interfaces for C ++, Python and Java, supported by Windows, Linux, MacOS, IOS and Android. This library is used to analyze videos and images.
OpenCV has a modular structure, which means that the package consists of several static and dynamic libraries. In this article, we will look at how to install OpenCV on Ubuntu 18.04 from the official repository or sources.
The content of the article:
Install OpenCV from Ubuntu Repository
The OpenCV package is available in the official repositories of Ubuntu 18.04. At the moment, version 3.2 is in the repositories, and version 4.0 can already be found on the official website. To install OpenCV on Ubuntu 18.04 along with Python 3, run:
sudo apt update
sudo apt install python3-opencv
Then you need to import the cv2 module in the python interpreter and look at the version to make sure that the installation was successful:
python3 -c "
On Ubuntu 18.04 LTS, the default version of Python is 3.6. If you need to install OpenCV with Python 2 bindings, download the python-opencv package.
Installing OpenCV Using PIP
The pip package manager allows you to install various python packages on your system. First you need to install the package manager itself:
sudo apt install python-pip
Then it remains to install the library itself:
sudo pip install opencv-python
After that, you can check the library version as described in the previous paragraph.
Install OpenCV from source
The official repository and pip repository is not the latest version. At the time of writing, the newest version is 4.1.0. If you want to install it, you will need to compile OpenCV from the source code.
Step 1. Installing dependencies
First, install the dependencies:
sudo apt install build-essential cmake git pkg-config libgtk-3-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libjpeg-dev libpng-dev libtiff-dev gfortran openexr libatlas-base-dev python3-dev python3-numpy libtbb2 libtbb-dev libdc1394-22-dev
Step 2. Cloning the repository
Create a directory in which the OpenCV repositories will be saved and go to it
Then download the repositories. Two repositories will be downloaded – opencv and opencv-contrib. The second repository contains computer vision algorithms and will be useful to everyone who wants to work with this technology.
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
If you want to install an old version of OpenCV, go to the directory of both repositories and run the command in them:
git checkout <нужная-версия>
Step 3. Preparing for assembly
When the download is complete, create a temporary assembly directory and switch to it:
Prepare your OpenCV build using CMake:
cmake -D CMAKE_BUILD_TYPE=RELEASE
-D BUILD_EXAMPLES=ON ..
Once the CMake build is complete, you will see something similar to the one shown below:
Step 4. Compiling OpenCV
Change the -j flag to suit your processor. You can find out the number of processor cores by using the nproc command. For example, the system has 4 cores, so the –j4 flag is used. Start the compilation process:
Compilation may take some time. It depends on your system configuration. Upon completion, you will see something like this:
Step 5. Installing OpenCV
When the compilation process is complete, install OpenCV with the command:
sudo make install
Step 6. OpenCV Version
To verify that the OpenCV installation was successful, enter the following commands. You should see the version of OpenCV:
pkg-config --modversion opencv4
python3 -c "
In this article, we examined how to install OpenCV on Ubuntu 18.04 from a repository or source code. Now you know how to get the latest version of the program. More information on using the library is available on the official website.