How to install OpenCV on Ubuntu 20.04?

Overview

OpenCV is a Machine Learning software library. OpenCV aims to speed up the use of computer vision in commercial products. The software provides interfaces for different programming languages supporting various operating systems.

Prerequisites

There are certain prerequisites that need to be met before you begin.

  • A server running Ubuntu version 20.04.

  • Non-root sudo privileges.

Get Started

We can install OpenCV using any of the two methods outlined below:

Method 1 - Installing OpenCV from the Ubuntu Repository

  • Update the existing Ubuntu packages.

sudo apt update
  • To install OpenCV run the command given below:

sudo apt install libopencv-dev python3-opencv
  • To confirm the successful installation of OpenCV, import the cv2 module and display the version of OpenCV.

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

Method 2 - Installing OpenCV from the Source

To obtain the latest version of the OpenCV library, building it directly from the source code is advisable. This method is highly recommended for installing OpenCV.

Follow the steps given below to install OpenCV (latest version):

  • Set up the required build 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
  • Create a new directory named opencv_build in the user's home directory and change the current directory to opencv_build.

mkdir ~/opencv_build && cd ~/opencv_build
  • Clone the repositories of OpenCV.

git clone https://github.com/opencv/opencv.git
  • Clone the repositories of OpenCV contrib.

git clone https://github.com/opencv/opencv_contrib.git
  • Create a temporary build directory.

cd ~/opencv_build/opencv
mkdir -p build && cd build
  • Configure the OpenCV build 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 ..

The resulting output should resemble the following:

  • Determine the number of processing units (or CPU cores) available on the system.

nproc
  • Commence the compilation process:

make -j4

Adjust the value of the -j flag to match the number of processing units (or CPU cores) available on the system.

  • Install OpenCV from its source code.

sudo make install
  • To confirm the successful installation, enter the following commands, and you should observe the version of OpenCV being displayed.

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

Conclusion

We have successfully demonstrated how to install OpenCV on Ubuntu 20.04.

Last updated