Linux

How to Install OpenCV on Ubuntu 20.04

How to Install OpenCV on Ubuntu 20.04

OpenCV (Open Source Computer Vision Library) is an open-source computer vision library with bindings for C ++, Python, and Java and supports all major operating systems. It can utilize multi-core processing and GPU acceleration features for real-time operations.

OpenCV is used for a variety of applications, including medical image analysis, stitching street view images, video surveillance, detecting and recognizing faces, tracking moving objects, extracting 3D models, and more.

This article explains how to install OpenCV on Ubuntu 20.04. To install the latest stable version of OpenCV from source, scroll down to the OpenCV Installation from the Sources section of this tutorial. Choose the installation method that works best for you.

Install OpenCV from the Ubuntu Repository

OpenCV is available for installation from the default Ubuntu 20.04 repository. To install it run:
sudo apt update
sudo apt install libopencv-dev python3-opencv

The above command will install all the packages needed to run OpenCV.

Verify the installation by importing the cv2 module and printing the OpenCV version:

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

At the time of writing, the version in the repository is 4.2:

Output :

4.2.0

Install OpenCV from Source

Building an OpenCV library from the source allows you to have the latest version available. This will be optimized for your specific system, and you will have full control over the build options. This is the recommended way to install OpenCV.

Perform the following steps to install the latest version of OpenCV from source:

  1. Install tools and 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 libopenexr-dev \
    libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
  2. Clone the OpenCV and OpenCV contribution repositories:
    mkdir ~/opencv_build && cd ~/opencv_build
    git clone https://github.com/opencv/opencv.git
    git clone https://github.com/opencv/opencv_contrib.git
  3. At the time of writing, the default version on the github repository is version 4.3.0. If you want to install an older version of OpenCV, cd into the opencv and opencv_contrib directory and run git checkout <opencv-version>
  4. After the download is complete, create a temporary building directory, and navigate to it:
    cd ~/opencv_build/opencv
    mkdir -p build && cd build

Prepare OpenCV build with 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 ..

The output will look like this:

Output :

Configuring done
-- Generating done
-- Build files have been written to: /home/vagrant/opencv_build/opencv/build

5. Start the compilation process:

make -j8

Change the -j flag to suit your processor. If you don’t know the core number for your processor, you can find it by typing nproc.

Compilation can take several minutes or more, depending on your system configuration.

6. Install OpenCV with:

sudo make install

7. To verify installation, type the following command and you will see the OpenCV version.

Binding C ++:

pkg-config --modversion opencv4

Output :

4.3.0

Python bindings:

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

Output :

4.3.0-dev

Conclusion

We have shown you two different ways to install OpenCV on your Ubuntu 20.04 machine. The method you choose depends on your needs and preferences. Although installing the package version of the Ubuntu repository is easier, building OpenCV from source gives you more flexibility, and that should be your first choice when installing OpenCV.

If you have questions or feedback, feel free to comment below.

Related posts

How To Install NTP (Chrony) On CentOS 8 / CentOS 7 & RHEL 8 / RHEL 7

Linux

How to Install Apache Cassandra on CentOS 8

Linux

How To Remove Snap From Ubuntu

Linux

How to Install Laravel on Ubuntu 20.04

Linux

How to install Steam and activate Proton on Fedora

Linux

How to create files in Python and more

Linux

How to Install PHP on Ubuntu 20.04

Linux

How to Install R on Ubuntu 20.04

Linux

How to get started with Kali Linux in 2020

Linux