OpenCV is a library of computer vision abbreviated as Open Source Computer Vision Library. Open means it is an open-source library with bindings for python, C++, and Java and supports different Operating Systems like Windows and Linux. It is able to use multi-core processing and GPU acceleration for real-time operating tasks. OpenCV library can be used for a wide range of applications, including face recognition and detection, medical image analysis, motion tracking, surveillance video, 3D model extraction, and much more.
In this article, we will describe how to install the OpenCV library on Linux distribution Ubuntu 20.04.
You can install OpenCV using two different ways:
- Install OpenCV using Ubuntu repository
- Install OpenCV through the source
If you want to install the latest stable version of the OpenCV library on your system then, using the source method you can install it. Open the terminal by pressing ‘Ctrl+Alt+t’ and start the installation of OpenCV using the following methods:
Method 1: Installation of OpenCV using the Ubuntu repository
OpenCV library is available in the official Ubuntu repository in Ubuntu 20.04. Update the apt cache by using the following command:
$ sudo apt update
Install OpenCV by running the following command:
$ sudo apt install libopencv-dev python3-opencv
The above command will install all the necessary packages on your system.
After completing the installation of OpenCV, you can verify the installation by importing the cv2 module and print the installed version of OpenCV by executing the following command:
$ python3 -c "import cv2; print(cv2.__version__)"
The following output will display on your screen. Here, the 4.2.0 OpenCV version has been installed on this system.
Method 2: Install OpenCV through the source
This method is recommended to install the OpenCV library because using this way you can install the latest stable version of OpenCV on your system. This method will be particularly optimized for your system and you can easily control the build options as well.
Follow the following step to install the latest OpenCV version from the source:
Step 1: Install build tools
Install the required build tools and dependencies on your system by using the following command:
$ 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
Step 2: Clone OpenCV’s repositories
Make the directory and navigate in it. Clone the OpenCV repository by running the following command:
$ mkdir ~/opencv_build && cd ~/opencv_build $ git clone https://github.com/opencv/opencv.git
Then, clone the OpenCV contrib repositories by using the following command:
$ git clone https://github.com/opencv/opencv_contrib.git
At the time of writing this article, OpenCV 4.4.0 is available as the latest stable version.
Step 3: Setup OpenCV build
Once the download is completed, create a directory named ‘build’ and navigate to it as follows:
$ cd ~/opencv_build/opencv $ mkdir -p build && cd build
Now, setup OpenCV build with CMake by using the following 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=~/opencv_build/opencv_contrib/modules \ -D BUILD_EXAMPLES=ON ..
You will see the following output on the terminal at the end:
Step 4: Start a compilation
Start the compilation process by using the following command:
$ make j8
Modify the value of the ‘j’ flag according to your processors. If you don’t know about your processor cores then, type the nproc command. You can easily find the core of your processor in this way. The compilation process will take time according to your system configurations. So, be patient.
Step 5: Install OpenCV
Install the OpenCV by executing the following command:
$ sudo make install
After completing the installation process, type the following command to verify the OpenCV installation. For C++ binding:
$ pkg-config --modversion opencv4
For python binding run the command as follows:
$ python3 -c "import cv2; print(cv2.__version__)"
Congratulations! The newest OpenCV version 4.4.0 has been installed on your system now.
Conclusion
We have explained two different ways in this article to install OpenCV on Linux system Ubuntu 20.04. Choose the methods according to your preferences and requirements. Or works best for you. Installing the packaged OpenCV version from the Ubuntu repository is quite easier, but, building OpenCV from the source provides you more flexibility, and it recommended that you should choose this method first when installing OpenCV on your system.