Choose a different version or distribution
Introduction
Before we begin talking about how to install OpenCV on Ubuntu 20.04, let's briefly understand – What is OpenCV?
OpenCV, short for Open Source Computer Vision, is a widely-used open-source library that helps developers create computer vision applications. It provides a vast collection of tools and algorithms for image and video processing, enabling tasks like object detection, facial recognition, and motion tracking.
OpenCV supports multiple programming languages and platforms, making it versatile for various projects. With its extensive capabilities and active developer community, OpenCV continues to empower the field of computer vision and unlock innovative applications in diverse industries.
In this tutorial, we will show how to install OpenCV on Ubuntu 20.04.
Advantages of OpenCV
- Versatility: OpenCV supports multiple programming languages and platforms, providing flexibility for developers.
- Vast Toolbox: OpenCV offers a wide range of tools and algorithms for image and video processing.
- Active Community: OpenCV has a vibrant developer community that constantly contributes to its improvement.
- Widely Used: OpenCV is extensively used in academia, research, and industry for computer vision applications.
- Innovation Potential: OpenCV empowers developers to create innovative applications in fields like robotics, augmented reality, and surveillance.
Installing OpenCV from the Ubuntu Repository
Installing OpenCV is possible using the standard Ubuntu 20.04 repository. Run the following command to install it:
sudo apt update
sudo apt install libopencv-dev python3-opencv
The aforementioned command will set up every package required to run OpenCV.
Import the cv2
module and print the OpenCV version to confirm the installation:
python3 -c "import cv2; print(cv2.__version__)"
The version in the repositories is 4.2, at the time of writing this tutorial.
Output
4.6.0
Installing OpenCV from the Source
You may get the most recent version of the OpenCV library by building it yourself from the source code. You will have total control over the build choices, and they will be tailored for your specific system. This method of installing OpenCV is recommended.
To install the most recent OpenCV version directly from the source, follow these steps:
1) Install the necessary dependencies and build tools:
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
2) The OpenCV and OpenCV contrib repositories should be cloned:
mkdir ~/opencv_build && cd ~/opencv_build
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
Version 4.6.0 is the currently available default version in the GitHub repository. Install an earlier version of OpenCV by using the command git checkout
<opencv-version>
while in the OpenCV and opencv_contrib
folders.
3) Create a temporary build directory, then go to it when the download is finished:
cd ~/opencv_build/opencv
mkdir -p build && cd build
Set up the 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 ..
The output will look something like below:
Output
-- Configuring done
-- Generating done
-- Build files have been written to: /home/vagrant/opencv_build/opencv/build
4) Now, begin the compilation process:
make -j8
Edit the -j
option to your processor's needs. If you are unsure about your processor's core count, you can discover it by entering nproc
.
Depending on your system settings, the compilation may take several minutes or longer.
5) Install OpenCV with the following command:
sudo make install
6) Type the following commands to check the installation, and you should see the OpenCV version.
C++ bindings:
pkg-config --modversion opencv4
Output
4.6.0
Python bindings:
python3 -c "import cv2; print(cv2.__version__)"
Output
4.6.0-dev
FAQs to Install OpenCV on Ubuntu 20.04
Does OpenCV support Python on Ubuntu 20.04?
Yes, OpenCV has excellent Python support on Ubuntu 20.04. You can use it with Python to develop computer vision applications.
What are the system requirements for installing OpenCV on Ubuntu 20.04?
OpenCV can be installed on Ubuntu 20.04 with modest system requirements. Ensure you have enough disk space and meet the minimum RAM and CPU specifications.
Can I use OpenCV with other programming languages on Ubuntu 20.04?
Yes, OpenCV supports multiple programming languages, including C++, Java, and MATLAB, making it compatible with various languages on Ubuntu 20.04.
How can I verify the successful installation of OpenCV on Ubuntu 20.04?
You can verify the OpenCV installation by running a simple script that imports the OpenCV library and checking for any errors.
Can I install a specific version of OpenCV on Ubuntu 20.04?
Yes, you can install a specific version of OpenCV on Ubuntu 20.04 by specifying the version number during the installation process.
How can I update OpenCV to the latest version on Ubuntu 20.04?
To update OpenCV to the latest version on Ubuntu 20.04, you can use the package manager to upgrade the installed OpenCV package.
Conclusion
We have shown you two alternative approaches to install OpenCV on your Ubuntu 20.04 system, take the decision based on your preferences and requirements. Building OpenCV from the source provides you more freedom and should be your first choice when installing OpenCV even if installing the packaged version from the Ubuntu repository is simpler.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.