Installing OpenCV in Ubuntu 18.04

OpenCV (Open Source Computer Vision Library) is a library in computer vision released under the BSD license, free for both personal and commercial use. It has interfaces for C++, Python, and Java, supports Windows, Linux, MacOS, IOS and Android. This library is used for analyzing video and images.

OpenCV has a modular structure, which means that the package consists of several static and dynamic libraries. In this article we will discuss how to install OpenCV on Ubuntu 18.04 from the official repository or source.

Installing OpenCV from the Ubuntu repositories

The OpenCV package is available in official Ubuntu repositories 18.04. Currently in the repositories is version 3.2 and on the official website you can already find a version of 4.0. To install OpenCV in Ubuntu 18.04 together with Python 3, execute:

sudo apt update

sudo apt install python3-opencv

Then you need to import the cv2 module in python interpreter and check the version to verify that the installation was successful:

python3 -c "
import cv2
print(cv2.__version__)"

3.2.0

Ubuntu 18.04 LTS default version of Python 3.6. If you want to install OpenCV with Python bindings, 2, download the package python-opencv.

Installing OpenCV using PIP

Pip package Manager allows to install various python packages into the system. You first need to install the package Manager itself:

sudo apt install python-pip

After that you can install the library itself:

sudo pip install opencv-python

After that, you can check the version of the library as described in the previous paragraph.

Installation of OpenCV from source code

In the official repository and the repository pip is not the newest version. At the time of this writing, the newest version is 4.1.0. If you want to install it, you need to compile OpenCV from 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 openexr gfortran libatlas-base-dev python3-dev python3-numpy libtbb2 libtbb-dev libdc1394-22-dev

Step 2. Clone the repository

Create a directory to store the repository of OpenCV and go to it

mkdir ~/opencv_build

cd ~/opencv_build

Then download the repository. Will be uploaded two repository – opencv and opencv-contrib. The second repository contains the computer vision algorithms and will be useful to anyone 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 older version of OpenCV, navigate to the directory both repositories and run them with the command:

git checkout <desired-version>

Step 3. Preparation for Assembly

When the download is complete, create a temporary directory and switch to it:

cd ~/opencv_build/opencv

mkdir build

cd build

Prepare build OpenCV using CMake:

cmake -D CMAKE_BUILD_TYPE=RELEASE 
-D CMAKE_INSTALL_PREFIX=/usr/local
-D INSTALL_C_EXAMPLES=ON
-D INSTALL_PYTHON_EXAMPLES=ON
-D OPENCV_GENERATE_PKGCONFIG=ON
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules
-D BUILD_EXAMPLES=ON ..

As soon as the CMake build is complete, you will see something similar to what is shown below:

Step 4. Compiling OpenCV

Change the-j flag in accordance with your processor. To know the number of processor cores with the command nproc. For example, the system has 4 cores, so it uses the flag –j4. Start the compilation process:

make

Compilation may take some time. It depends on your system configuration. After completion you will see something like this:

Step 5. Installing OpenCV

When the compilation is complete, install OpenCV with the command:

sudo make install

Step 6. The Version Of OpenCV

To verify that you did install OpenCV successfully, enter the following commands. You should see the version of OpenCV:

pkg-config --modversion opencv4

Conclusion:

4.0.1

python3 -c "
import cv2
print(cv2.__version__)"

Conclusion:

4.0.1-dev

Conclusion

In this article, we discussed how to install OpenCV in Ubuntu 18.04 from the repository or source code. Now you know how to obtain the latest version of the program. More information on using libraries available on the official website.

Source: losst.ru

(Visited 103 times, 2 visits today)