How Tos

How to Install and Configure OpenCV on Ubuntu 20.04

Share on Facebook Share on Twitter Pinterest LinkedIn Tumblr

OpenCV (Open Source Computer Vision Library) is open-source software with bindings for C++, Python, and Java. OpenCV is used for a variety of applications, including medical image analysis, creating street scene images, surveillance video, detecting and recognizing faces, tracking moving objects, extracting 3D models, and more.

OpenCV can take advantage of multi-core processing and GPU acceleration features for real-time operations.

This tutorial, shows how to install OpenCV on Ubuntu 20.04 and Ubuntu 21.04, For most people, the easiest way to install OpenCV on Ubuntu is to install it using apt package management. If you want to install the latest stable version of OpenCV from source, scroll down on this tutorial page.

Choose one of the installation options that suits you best.

Install OpenCV from Ubuntu 20.04 Repository

The OpenCV Python module is available from the standard Ubuntu repositories. At the time of writing, Ubuntu’s default repository is OpenCV version 3.2, which is a bit behind the latest version.

To install the OpenCV Python module, enter the command:

sudo apt update
sudo apt install python3-opencv 

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

How to Install GNOME 40.1 Stable Update on Ubuntu

To verify the installation, import the module cv2and print the OpenCV version:

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

If you want to install OpenCV with Python 2 bindings, install the package python-opencv.

Installing OpenCV from Source

Building the OpenCV library from source is the recommended way to install OpenCV. It will be optimized for your particular system, and you will have complete control over the build options.

To install the latest version of OpenCV from source, perform the following steps:

1) Install the required and optional 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

2) Clone the OpenCV contrib and OpenCV repository with the following command:

mkdir ~/opencv_build && cd ~/opencv_buildgit 
clone https://github.com/opencv/opencv.gitgit 
clone https://github.com/opencv/opencv_contrib.git 

At the time of writing, the default version in the github repository is version 4.2.0. If you want to install an older version of OpenCV, do so cd to the directory opencvand opencv_contriband run git checkout <opencv-version>

3) Once the download is complete, create a temporary build directory, and go to it:

cd ~/opencv_build/opencv mkdir build && cd build 

Set up an 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 .. 

When the CMake build system is complete, you should see something like below:

4) Start the compilation process:

make -j2 

Change the flag -jaccording to your processor. If you don’t know the number of cores on your processor, you can find it by typing the command nproc.

The Compilation process may take a few minutes or more, depending on your system configuration. Once done, you should see something like below:

5) Install OpenCV by typing:

sudo make install

6) To check if OpenCV has been installed successfully, enter the following command and you will see the OpenCV version:

pkg-config --modversion 
opencv4 4.2.0 
python3 -c "import cv2; print(cv2.__version__)"
 4.2.0-dev

How to install and configure Java on Ubuntu 20.04

Conclusion

We have shown you two different ways to install OpenCV on an Ubuntu 20.04 and Ubuntu 21.04 server. The method you choose depends on your needs and preferences.

While installing a packaged version from the Ubuntu repositories is easier, building OpenCV from source gives you more flexibility, and it should be your first choice when installing OpenCV.

Write A Comment