OpenCV (Open Source Computer Vision Library) is a powerful library used for computer vision applications. It provides various tools for image processing, machine learning, and video analysis. This guide walks you through the steps to Installing OpenCV on Linux
Before installing OpenCV, ensure your system meets the following prerequisites:
- A Linux distribution (Ubuntu, Debian, Fedora, etc.)
- Basic knowledge of using the terminal
- Administrative (root) access to your system
Installing Dependencies for OpenCV on Linux
OpenCV requires several dependencies to be installed on your system. Open the terminal and run the following commands to install the necessary packages:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential cmake git pkg-config libgtk-3-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install libv4l-dev libxvidcore-dev libx264-dev
sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
sudo apt-get install gfortran openexr libatlas-base-dev
sudo apt-get install python3-dev python3-numpy libtbb2 libtbb-dev libdc1394-22-dev
These commands update your package list and install essential tools and libraries required for building and running OpenCV.
Downloading OpenCV
Download the latest version of OpenCV and the OpenCV contrib modules from GitHub. Open the terminal and execute the following commands:
cd ~
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
This clones the OpenCV and OpenCV contrib repositories into your home directory.
Building and Installing OpenCV
Navigate to the OpenCV directory and create a build folder:
cd ~/opencv
mkdir build
cd build
Configure the build with CMake, specifying the paths to the contrib modules:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules ..
Compile OpenCV with the following command, which may take some time depending on your system:
make -j$(nproc)
Install OpenCV:
sudo make install
sudo ldconfig
Verifying Installation of OpenCV on Linux
To verify the installation, open the Python interpreter and import OpenCV:
python3
>>> import cv2
>>> print(cv2.__version__)
OpenCV is installed correctly, the version number will be displayed. Following this guide, you should have OpenCV installed on your Linux system, ready for developing computer vision applications. OpenCV’s extensive library and tools provide a robust platform for various image and video processing tasks.