How to install OpenCV on Ubuntu 20.04?

Computer vision is increasingly being used for image analysis and processing using artificial intelligence algorithms. To get started in this field, it is best to install OpenCV in Ubuntu 20.04, which is precisely what we are going to show you how to do in this post.

OpenCV is a library made in Python that allows us to create artificial vision applications thanks to the platform it gives us. One of its main advantages is that it has a BSD license, which allows us to use and modify the code and use it in many different projects. Another advantage is that there is a very active community behind the development and support of the library.

Another great advantage of OpenCV is that this great library has interfaces for multiple languages, including Python, Java, and C++. So we can use it in almost any application we create.

Some of the things you can do with OpenCV by integrating it into your favorite programming language are identifying objects, faces, classifying human actions in video, tracking object movements, extracting 3D models, finding similar images, and so on.

Two ways to install OpenCV

Like almost everything in Linux, we have several ways to install OpenCV and it depends on your computer and your needs to install it. In any case, both installation methods work fine on Ubuntu 20.04.

Note in this post, we will install OpenCV prepared for Python.

Method 1: Install OpenCV on Ubuntu 20.04 via APT

The first installation method is the easiest you can imagine because it is by using the Ubuntu repositories. And yes, OpenCV is available in these repositories.

So, you need to open a terminal either from the main menu or from a shortcut like a dock or a shortcut and execute these commands

sudo apt update
sudo apt python3-opencv

And if you want to verify the installation, you can execute

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

You will get a screen output similar to this:

4.2.0

With this OpenCV will be installed.

Method 2: Install OpenCV from the source code

There is another way and that is to install it from the source code. This way it will be highly optimized for your system and you will always get the latest stable version. Although it will take a little longer, I think it is convenient.

So, first install the dependencies:

sudo apt install build-essential libatlas-base-dev python3-dev python3-numpy libtbb2 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 libtbb-dev libdc1394-22-dev libopenexr-dev libgstreamer-plugins-base1. 0-dev libgstreamer1.0-dev cmake git

Then, create a directory to host the source code and access the folder:

mkdir opencv-install
cd opencv-install

Now with the git command, you can clone the opencv and opencv_contrib repositories.

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

It will create a folder called opencv that you will have to access:

cd opencv

And inside it create another one called build where we will compile the code. And access it.

mkdir -p build
cd build

Now, configure the package with the command:

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=/home/user/opencv-install/opencv_contrib/modules -D BUILD_EXAMPLES=ON . .

After that, you can start the compilation with this command.

make -j2

Note: The number 2 refers to the fact that the compilation process will use 2 processor cores. You can modify this value according to your hardware.

Next, install OpenCV by running:

sudo make install

To check the result, just run:

pkg-config --modversion opencv4

Sample Output:

4.5.2

And the command:

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

Sample Output:

4.5.2

Now you have the latest available version of OpenCV installed.

Conclusion

There are many libraries that programmers can use and one of the most useful in the field of computer vision. As we have noticed, both processes are quite simple to execute. Each with its pros and cons.

Scroll to Top