How to Install OpenCV on Ubuntu 22.04
Choose a different version or distribution
Introduction
Before, we begin talking about the steps to install OpenCV on Ubuntu 22.04. First, let's understand - What is OpenCV?
OpenCV, short for Open Source Computer Vision, is a powerful library for computer vision and image processing tasks. It provides a wide range of functions and algorithms to analyze and manipulate images and videos. OpenCV is open source and free to use, making it a popular choice for developers and researchers.
Whether it's face detection, object recognition, or even augmented reality, OpenCV offers the tools needed to build advanced vision applications. Its versatility and extensive documentation make it a go-to solution for computer vision projects. With OpenCV, unlocking the potential of visual data becomes more accessible than ever.
In this tutorial, you will install OpenCV on Ubuntu 22.04. We will also address a few FAQs related to OpenCV installation.
Advantages of OpenCV
- Versatile: OpenCV provides a wide range of functions and algorithms for various computer vision tasks.
- Accessibility: Being open source, OpenCV is freely available to developers and researchers.
- Extensive Documentation: OpenCV offers comprehensive documentation, making it easier to understand and utilize its features.
- Community Support: OpenCV has a large and active community, providing support and resources for users.
- Widely Used: OpenCV is a popular choice in the industry, making it easier to find tutorials, examples, and code snippets for implementation.
Step 1 – Installing OpenCV from Ubuntu Repository
1) The OpenCV package is available from Ubuntu 22.04 distribution repository. Here, the version in the repositories is 4.2, but not the latest version. You will need to refresh the packages index. Also, install the OpenCV package by:
sudo apt update
sudo apt install python3-opencv
The command above will install all the necessary packages required to run the OpenCV.
2) Now, verify the installation. You now import cv2
module and print the OpenCV version:
python3 -c "import cv2; print(cv2.__version__)"
Output
4.2.0
The Default Python version in the Ubuntu 22.04 LTS is version 3.6. Also, if you want to install OpenCV with Python 2 bindings, do it by installing python-opencv
package.
Step 2 - Installing OpenCV from the Source
It is recommended to build the OpenCV library from the source. You will have complete control over the build options as they will be optimized for your particular system.
- Firstly, install the following 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
2) After that, clone both OpenCV’s and OpenCV contrib repositories:
mkdir ~/opencv_build && cd ~/opencv_build
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
3) Here, the default version in the GitHub repositories is 4.2.0. If you want to install an older version of OpenCV, then, cd to both opencv
and opencv_contrib
directories and run git checkout <opencv-version>
.
4) After, the download is complete, continue to create a temporary build directory. You can switch to it, using the following command:
cd ~/opencv_build/opencv
mkdir build && cd build
5) Then, set up the OpenCV build with CMake, by:
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 get the following output when the CMake build system is finalized:
6) Now, start the compilation process:
make -j8
7) Next, modify the -j
flag as per your processor. Moreover, if you do not know the number of cores on your processor, find them by typing nproc
.
The compilation will take several minutes. It will be depending upon your system configuration. After completing, you will see a similar output:
8) Now, install the OpenCV using the following command:
sudo make install
9) Proceed to verify if the OpenCV installation is successful, or not. Type the below command. You will see the OpenCV version:
pkg-config --modversion opencv4
Output
4.2.0
python3 -c "import cv2; print(cv2.__version__)"
Output
4.2.0-dev
FAQs to Install OpenCV on the Ubuntu 22.04
Are there any additional dependencies required for installing OpenCV on Ubuntu 22.04?
Yes, there might be additional dependencies needed. Use the command: sudo apt-get build-dep opencv
to install the necessary dependencies before installing OpenCV.
How can I verify if OpenCV is successfully installed on Ubuntu 22.04?
To verify the installation, open a Python terminal and type: import cv2
. If no error is raised, OpenCV is successfully installed.
Can I install OpenCV with GPU support on Ubuntu 22.04?
Yes, you can install OpenCV with GPU support on Ubuntu 22.04. Ensure you have compatible NVIDIA drivers installed and follow the official OpenCV documentation for GPU installation steps.
Is it possible to install a specific version of OpenCV on Ubuntu 22.04?
Yes, it is possible to install a specific version of OpenCV on Ubuntu 22.04. You can use the command: sudo apt install python3-opencv=<version_number>
to install a specific version.
How can I uninstall OpenCV from Ubuntu 22.04?
To uninstall OpenCV, use the command: sudo apt remove python3-opencv
. Additionally, you can run: sudo apt autoremove
to remove any unused dependencies.
Can I use OpenCV with virtual environments on Ubuntu 22.04?
Yes, you can use OpenCV with virtual environments on Ubuntu 22.04. Activate your virtual environment and install OpenCV using the package manager or pip.
Conclusion
We hope this detailed tutorial helped you to install OpenCV on Ubuntu 22.04.
If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.