Installing OpenCV 3 or 4 on Ubuntu 18.04, 20.04, or 22.04: A Comprehensive Guide
OpenCV (Open Source Computer Vision Library) is a powerful and versatile library for computer vision tasks, encompassing a wide range of functionalities from basic image processing to complex object detection and machine learning. This guide provides a detailed walkthrough of installing OpenCV 3 and 4 on various Ubuntu versions (18.04, 20.04, and 22.04), catering to both beginners and experienced users. We’ll cover building from source for maximum flexibility and customization, as well as utilizing pre-built packages for a quicker installation.
Choosing the Right OpenCV Version and Installation Method:
Deciding between OpenCV 3 and 4 depends on your project requirements. OpenCV 4 offers newer features, performance improvements, and updated dependencies. However, if you’re working with legacy code or require specific functionalities only present in OpenCV 3, sticking with the older version might be necessary.
We’ll explore two primary installation methods:
-
Building from Source: This method offers greater control over the compilation process, allowing you to customize features, optimize for specific hardware, and integrate third-party libraries. It’s recommended for advanced users who require specific functionalities or performance optimizations.
-
Installing Pre-built Packages: This approach is quicker and simpler, ideal for beginners or projects where customization isn’t paramount. Pre-built packages are readily available through Ubuntu’s package manager (apt).
Preparing Your System:
Before diving into the installation, ensure your system is updated and has the necessary build tools and dependencies installed. Open a terminal and execute the following commands:
bash
sudo apt update
sudo apt upgrade -y
sudo apt install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libjpeg-dev libpng-dev libtiff-dev gfortran openexr libatlas-base-dev g++ wget unzip
These commands update your system’s package list, upgrade existing packages, and install essential build tools, video codecs, image format libraries, and optimization libraries required for compiling OpenCV.
I. Building OpenCV from Source:
This section provides a step-by-step guide for building OpenCV from source, offering maximum flexibility and customization.
1. Downloading the Source Code:
Download the desired OpenCV version from the official GitHub repository or the OpenCV website. You can use wget
or manually download and extract the archive. For example, to download OpenCV 4.5.5:
bash
wget -O opencv-4.5.5.zip https://github.com/opencv/opencv/archive/4.5.5.zip
unzip opencv-4.5.5.zip
2. Creating the Build Directory:
Navigate to the extracted source directory and create a build directory:
bash
cd opencv-4.5.5
mkdir build
cd build
3. Configuring CMake:
CMake is a cross-platform build system generator. Use it to configure the build process:
bash
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_CUDA=OFF -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=1 -D BUILD_EXAMPLES=ON ..
This command configures the build with the following options:
CMAKE_BUILD_TYPE=RELEASE
: Optimizes the build for performance.CMAKE_INSTALL_PREFIX=/usr/local
: Sets the installation directory.WITH_CUDA=OFF
: Disables CUDA support (enable if you have a CUDA-capable GPU).ENABLE_FAST_MATH=1
,CUDA_FAST_MATH=1
,WITH_CUBLAS=1
: Enable optimizations for faster mathematical operations (especially beneficial with CUDA).BUILD_EXAMPLES=ON
: Compiles the example code (optional).
Adjust these options based on your needs. For example, to enable CUDA support, change WITH_CUDA=OFF
to WITH_CUDA=ON
and ensure CUDA is correctly installed on your system.
4. Compiling OpenCV:
After configuring CMake, compile OpenCV using make
:
bash
make -j$(nproc)
The -j$(nproc)
option utilizes all available processor cores for faster compilation.
5. Installing OpenCV:
Once compilation is complete, install OpenCV:
bash
sudo make install
6. Updating System Cache:
Update the system’s dynamic linker cache to recognize the newly installed library:
bash
sudo ldconfig
II. Installing OpenCV from Pre-built Packages:
This method provides a quicker and easier installation using Ubuntu’s package manager.
1. Installing OpenCV from the Ubuntu Repositories:
The simplest way is to install the packages directly from the Ubuntu repositories:
bash
sudo apt install libopencv-dev python3-opencv
This will install the core OpenCV libraries and the Python bindings. Note that the version available in the repositories might not be the latest.
2. Installing Specific OpenCV Versions (if available):
Sometimes, specific OpenCV versions might be available through alternative repositories or PPAs. Check the OpenCV website or community forums for instructions on adding these repositories and installing specific versions.
Verifying the Installation:
Regardless of the installation method, verify the installation by running a sample OpenCV program or checking the version:
bash
pkg-config --modversion opencv4 # Replace opencv4 with opencv if you installed OpenCV 3
python3 -c "import cv2; print(cv2.__version__)"
Troubleshooting:
- Compilation Errors: Carefully examine the error messages during compilation. They usually indicate missing dependencies or configuration issues. Consult online forums or documentation for solutions.
- Runtime Errors: Ensure that the necessary libraries are linked correctly and the environment variables are set appropriately.
- Python Bindings Issues: Verify that the correct Python version is used and the Python bindings are installed correctly.
Conclusion:
This comprehensive guide covers the installation of OpenCV 3 and 4 on various Ubuntu versions using both source compilation and pre-built packages. By understanding the different installation methods and configuring the build process appropriately, you can tailor OpenCV to your specific project needs and leverage its powerful computer vision capabilities. Remember to consult the official OpenCV documentation and online forums for further assistance and advanced configuration options. This guide provides a solid foundation for beginning your journey into the world of computer vision with OpenCV. Remember to choose the installation method that best suits your needs and project requirements, and don’t hesitate to explore the extensive documentation and online resources available for further learning and troubleshooting. Happy coding!