Thursday, September 5, 2024

How to Install OpenCV on Ubuntu 22.04 Server

OpenCV, the Open Source Computer Vision Library, is a powerful tool that empowers developers to build sophisticated applications that can see, analyze, and interpret images and videos, just like humans. This comprehensive guide will walk you through the process of installing OpenCV on Ubuntu 22.04, opening the door to a world of possibilities in computer vision, machine learning, and artificial intelligence.

Prerequisites:

Before embarking on the installation journey, ensure you have the following in place:

  • Ubuntu 22.04: You need to have Ubuntu 22.04 installed on your system.

  • Administrative privileges: An account with sudo or administrative privileges is necessary to install system-wide packages.

  • Terminal access: You will need a terminal or command line interface to execute the installation commands.

Installation Methods:

There are four primary methods to install OpenCV on Ubuntu 22.04:

  1. Installation from Ubuntu Repositories: This is the simplest and quickest method, providing a readily available version of OpenCV. However, it might not be the most up-to-date version, potentially leading to compatibility issues.

  2. Building from Source: This method offers the most flexibility, allowing you to install the latest version of OpenCV and customize the build process to suit your specific needs.

  3. Installation with Anaconda: Anaconda is a popular Python distribution that bundles a package manager called Conda. Conda makes installing, managing, and updating packages across different Python environments a breeze.

  4. Installation with pip: Virtual environments provide a way to isolate Python projects and their dependencies, preventing conflicts that arise from different library versions. This is particularly crucial when working on multiple projects with varying library requirements.

Method #1: Installing OpenCV from Ubuntu Repositories

This method utilizes the official Ubuntu repositories to quickly install OpenCV.

Steps:

  1. Update the system repository package: Open your terminal and execute the following command:

          sudo apt update
        
  2. Install OpenCV: Use the apt package manager to install the development library:

          sudo apt install libopencv-dev
        
  3. Verify the installation: Check the installed version of OpenCV:

          dpkg -l libopencv-dev
        

    The output will display the installed version.

Method #2: Installing OpenCV from Source

This method allows you to install the latest version of OpenCV and customize the build process.

Steps:

  1. Update Ubuntu packages: Start by updating the package list:

          sudo apt update
        
  2. Install necessary tools and libraries: Install dependencies for development, image, and video processing:

          sudo apt install build-essential cmake git libgtk-3-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev openexr libatlas-base-dev libopenexr-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev python3-dev python3-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-dev gfortran -y
        
  3. Create a build directory: Create a directory for the OpenCV build:

          mkdir ~/opencv_build && cd ~/opencv_build
        
  4. Download OpenCV source code: Download the source code from the official repository using git:

          git clone https://github.com/opencv/opencv.git
        
  5. Download extra modules: Download the OpenCV contrib repository for additional modules:

          git clone https://github.com/opencv/opencv_contrib.git
        
  6. Create a build directory: Create a directory within the OpenCV folder for build files:

          mkdir -p ~/opencv_build/opencv/build && cd ~/opencv_build/opencv/build
        
  7. Configure the build process: Use cmake to configure the build:

          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 BUILD_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules ..
        

    This command installs both C++ and Python code examples. Use cmake -LH to explore available customization options.

  8. Build OpenCV: Use the make command to build OpenCV:

          make -j3
        

    Adjust the -j3 flag based on your system's CPU cores.

  9. Install OpenCV: Install OpenCV using the make command:

          sudo make install
        
  10. Verify the installation: Check the installed OpenCV version:

      pkg-config --modversion opencv4
    

Method #3: Installing OpenCV via Anaconda

Anaconda offers a convenient way to install OpenCV through its package manager, Conda.

Steps:

  1. Create a new Python environment: Create a new environment using conda:

          conda create -n myenv python=3.10.6
        

    Replace myenv with your desired environment name.

  2. Activate the environment: Activate the newly created environment:

          conda activate myenv
        
  3. Install OpenCV: Install OpenCV using conda:

          conda install -c conda-forge opencv
        
  4. Verify the installation: Open a Python interactive shell:

          python3
        

    Import OpenCV and print its version:

          import cv2
    print(cv2.__version__)
        

Method #4: Installing OpenCV via pip

Virtual environments provide a clean and isolated environment for Python projects, preventing dependency conflicts.

Steps:

  1. Install the venv module: Install the venv module (available in Python 3.3 and later):

          sudo apt install python3.10-venv -y
        

    Adjust the command for your Python version.

  2. Create a virtual environment: Create a virtual environment using venv:

          sudo python3 -m venv myenv
        
  3. Activate the environment: Activate the created environment:

          source myenv/bin/activate
        
  4. Install OpenCV: Install OpenCV using pip:

          sudo pip3 install opencv-python
        

    To install both main and contrib modules:

          sudo pip3 install opencv-contrib-python
        
  5. Verify the installation: Open a Python interactive shell:

          python3
        

    Import OpenCV and print its version:

          import cv2
    print(cv2.__version__)
        

Conclusion:

Installing OpenCV on Ubuntu 22.04 provides you with a powerful toolkit to build computer vision applications that unlock the insights hidden within visual data. Whether you opt for the quick and easy installation from Ubuntu repositories, the flexibility of building from source, the convenience of Anaconda, or the isolation of pip with virtual environments, you have a choice that aligns with your needs and preferences. With OpenCV installed, you are ready to explore the exciting world of computer vision, where machines can truly "see" and understand the world around them.

0 comments:

Post a Comment