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.
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 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.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.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.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.
Update the system repository package: Open your terminal and execute the following command:sudo apt update
Install OpenCV: Use the apt package manager to install the development library:sudo apt install libopencv-dev
Verify the installation: Check the installed version of OpenCV:dpkg -l libopencv-dev
The output will display the installed version.
Update Ubuntu packages: Start by updating the package list:sudo apt update
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
Create a build directory: Create a directory for the OpenCV build:mkdir ~/opencv_build && cd ~/opencv_build
Download OpenCV source code: Download the source code from the official repository using git:git clone https://github.com/opencv/opencv.git
Download extra modules: Download the OpenCV contrib repository for additional modules:git clone https://github.com/opencv/opencv_contrib.git
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
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. Build OpenCV: Use the make command to build OpenCV:make -j3
Adjust the -j3 flag based on your system's CPU cores. Install OpenCV: Install OpenCV using the make command:sudo make install
Verify the installation: Check the installed OpenCV version:
pkg-config --modversion opencv4
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. Activate the environment: Activate the newly created environment:conda activate myenv
Install OpenCV: Install OpenCV using conda:conda install -c conda-forge opencv
Verify the installation: Open a Python interactive shell:python3
Import OpenCV and print its version: import cv2 print(cv2.__version__)
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. Create a virtual environment: Create a virtual environment using venv:sudo python3 -m venv myenv
Activate the environment: Activate the created environment:source myenv/bin/activate
Install OpenCV: Install OpenCV using pip:sudo pip3 install opencv-python
To install both main and contrib modules: sudo pip3 install opencv-contrib-python
Verify the installation: Open a Python interactive shell:python3
Import OpenCV and print its version: import cv2 print(cv2.__version__)
0 comments:
Post a Comment