Learn 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 library for computer vision, it’s a completely free and cross-platform. OpenCV is used to develop a computer vision applications like face recognition and detection, 3d reconstruction, image processing, motion detection and tracking, augmented reality object detection, robotics etc. OpenCV can run on Linux, Windows, macOS, OpenBSD, FreeBSD, NetBSD operating systems. It also runs on android, iOS, Blackberry, Maemo operating systems. OpenCV is written in C and C++ programming language and also it has bindings in Java and Python.

In this article we are going to learn how to install OpenCV on Ubuntu 20.04. There are two methods are to install OpenCV on Ubuntu 20.04. We will install OpenCV from Ubuntu 20.04 repository and also we will install it from source.

Install OpenCV from Ubuntu Repository:

Update the packages then install the OpenCV from Ubuntu repository. We will install the latest OpenCV version 3.2. To install the OpenCV 3.2 from the Ubuntu repository run the commands below.

sudo apt update
sudo apt install libopencv-dev python3-opencv

After running the above commands all the needed packages will be installed for running OpenCV on your ubuntu 20.04.

Verify Installation:

To verify the OpenCV installation run the command below.

python3

After entering Python shell type the command oine by one.

import cv2 as cv
print(cv.__version__)

After running the above command you will see the following output

Output

3.2.0

After that exit the Python shell.

exit ()

Install OpenCV from Source:

Installing OpenCV from official source is the best and recommended way, because we customize the installation. We will install the OpenCV  latest version 4.2 from the official source.

Before install the OpenCV we need to install some needed packages. To install the needed packages on Ubuntu 20.04 run the commands below.

sudo apt install cmake git gcc g++ python3-dev python3-numpy libavcodec-dev libavformat-dev libswscale-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev sudo apt-get install libgtk-3-dev libpng-dev libjpeg-dev libopenexr-dev libtiff-dev libwebp-dev

After installing needed packages create a folder for your OpenCV installation.

mkdir ~/opencv_dev

After creating folder jump to that folder.

cd ~/opencv_dev

After that download the OpenCV from the official GitHub repository.

git clone https://github.com/opencv/opencv.git

Once the successful download jump to the following folder.

cd opencv

Now create a build folder for opencv

mkdir build
cd build

Run the following cmake command to config the OpenCV.

cmake ../

Now install OpenCV on Ubuntu 20.04 with make command.

make -j $(nproc)

sudo make install

Wait for sometime it takes some more time.

Verify Installation:

To verify the OpenCV installation run the command below.

python3

After entering the Python shell run the commands below one by one.

import cv2 as cv
print(cv.__version__)

Afetr running the above command you will see the output like below.

Output

4.3.0-dev

To exit the Python shell run the exit () command.

exit ()

Conclusion:

Successfully we have installed OpenCV on our ubuntu 20.04. In this article we have learned how to install OpenCV on Ubuntu 20.04 with two methods. Hope this helps.

Next Article: How to Install and Configure Gradle on Ubuntu 20.04

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like