OpenCV, otherwise known as Open Source Computer Vision Library is a rather useful image processing library with bindings for C++, Python and Java. It can take advantage of a dedicated GPU and multiple CPU cores to be able to process images in real-time.
Using the library you can build applications that can detect faces, colours, moving objects and all sorts of things in both photos and videos.
In this article, we’ll be taking a look at how to install OpenCV in Ubuntu 20.04.
Also read: How to enable SSH on Ubuntu?
How to install OpenCV on Ubuntu 20.04 LTS?
There are two ways you can install OpenCV on an Ubuntu machine.
- Using the apt package manager.
- By downloading and building OpenCV from source (recommended).
Installing OpenCV using apt
OpenCV is available for installation from the default Ubuntu 20.04 repositories. This means that you can quickly download and install OpenCV in a couple of commands.
sudo apt update
sudo apt install libopencv-dev python3-opencv
You can verify the installation by importing the cv2 function inside a Python 3 terminal and printing the OpenCV version.
python3 -c "import cv2; print(cv2.__version__)"
In spite of being extremely easy to install, this method isn’t recommended when it comes to installing OpenCV primarily because the version you’ll end up installing might not be the latest one.
Also read: How to check the Ubuntu version?
Installing OpenCV from source
Downloading the source code, building the library and then installing it lets you have the latest stable version of OpenCV. This way, the installation will be optimised for your system and you’ll also have control over the build options.
Step 1: First up, we’re going to have to install the build tools and all other 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
If the above command fails, run sudo apt-get update and try again.
Step 2: Now we’re going to clone the latest OpenCV Github repositories. At the time of writing, the latest version is 4.5.1.
Type the following commands one at a time pressing enter after each command.
mkdir ~/opencv_build && cd ~/opencv_build
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
If you’re looking to install an older version, just add the version number between the file name and .git extension. For example, opencv4.3.0.git
Step 3: Once the download is complete, we’re going to make a temporary build directory and enter it to proceed.
cd ~/opencv_build/opencv
mkdir -p build && cd build
Step 4: Now setup the 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 ..
Wait for the setup to complete successfully. Make sure there are no errors in the build before proceeding.
Step 5: Start the compilation process using the following command,
make -j1
Make sure to update the -j flag according to the number of cores in your processor. You can use the nproc command the check the number of cores you have. Since we’re running Ubuntu in a virtual machine, we’ve only got one core to work with.
The compilation can take a few minutes to complete, depending on your hardware.
Step 6: Once the compilation is complete, install OpenCV
sudo make install
You can confirm the installed version just like we did when we installed OpenCV using apt.
python3 -c "import cv2; print(cv2.__version__)"
Also read: How to install Chrome in Ubuntu?
How to install OpenCV on Ubuntu 18.04 LTS?
The process for installing OpenCV on Ubuntu 18.04 is exactly identical to the one we just described above for 20.04. Do keep in mind though that if you’re using the package manager to install OpenCV, you’ll be installing version 3.4.0 as that’s the default version in the 18.04 repositories.
Also read: Mint vs Ubuntu: Linux distro comparison