How to Install OpenCV on Debian 11

OpenCV (an acronym of Open-Source Computer Vision) is a well-known mega open-source library to accomplish several computer vision and machine learning-related tasks. The Computer Vision concepts assist machines to understand the graphical content specifically images and videos; however, it also defines a set of rules to retrieve and manipulate data from images and videos. In short, Computer Vision is the process of giving access to computing machines so that they can act like humans.

The OpenCV library is primarily based on C/C++; however, it provides support for Java and Python as well. Usually, execution in python is slower as compared to C++/C but it is easy to use; therefore, you can code in C/C++ and then create python wrappers of that code; these wrappers are referred to as modules and can be used to effectively solve the targeted problems. Moreover, this library contains thousands of algorithms based on the machine learning concepts for computer vision; these algorithms are available publicly for commercial as well as academic purposes.

Real-world application of OpenCV

In daily life, we encounter the implementation of OpenCV on several platforms and locations. Few commonly experienced daily lives applications of OpenCV are listed below:

  • Analyzing the images related to the medical field
  • Driverless car control system
  • Count number of vehicles and their speeds
  • Count number of people at several gatherings
  • Surveillance system

How to install OpenCV on Debian

To get access to OpenCV on Debian 11; there are two methods that we will explain in detail:

  • Method 1: Using an apt command
  • Method 2: Using source on Debian 11

Both methods are perfect to install OpenCV on Debian 11: the upcoming sections contain a brief explanation of both methods.

Method 1: Install OpenCV using apt command

You can get the python module of OpenCV from the Debian repository, so, firstly, update the packages list with the help of the following command:

$ sudo apt update

And now run the following command in the terminal to get the OpenCV python module:

$ sudo apt install python3-opencv

Method 2: Install OpenCV using source

The OpenCV library can be built using source on Debian 11 to install OpenCV, Follow the steps in this section to get access to OpenCV on your Debian 11 system:

Step 1: Install the required dependencies

Initially, you must install the required dependencies, however, it is recommended to get optional dependencies the below mentioned command will install optional as well as required dependencies as well,

$ 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

Step 2: Fetch “OpenCV_contrib” and “OpenCV” repositories

Make a new directory and change the present working directory to that:

$ mkdir opencv_git
$ cd opencv_git

To clone OpenCV; execute the following command:

$ git clone https://github.com/opencv/opencv.git

And for OpenCV; clone it using the command written below:

$ git clone https://github.com/opencv/opencv_contrib.git

Step 3: Setup and compile the build

After cloning, create a directory and switch to that newly made folder:

You have to make this directory inside the cloned project “opencv”; and in our case the path to “opencv” is “~/opencv_git/opencv”; firstly, navigate to “opencv” directory and then create a new build directory:

$ cd opencv
$ mkdir cvbuild
$ cd cvbuild 

Now setup the newly made build with the help of CMake:

$ 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/module \ -D BUILD_EXAMPLES=ON ..

After setting up the build, use the following command to start the compilation process; the “-j” refers to a number of cores of your processor:

Note: You can check the number of cores of your processor by executing “nproc” in your terminal:

$ make -j1

Step 4: Install OpenCV

After setting up the environment; you are ready to install OpenCV by using the following make command:

$ sudo make install 

How to remove OpenCV from Debian 11

You can remove OpenCV from your Debian 11 by executing the below-mentioned command in terminal:

$ sudo apt-get purge python3-opencv

Conclusion

OpenCV is an open-source library that contains around 2500 optimization algorithms to assist in accomplishing several computer vision and machine learning tasks. Initially, this project was started by Intel to get to the advanced level of computer vision phenomena by assisting developers to build a readable and transferable common infrastructure. In this descriptive post, we have described two ways to install OpenCV on Debian 11. The first method is simple and easy for novice users; however, the source method is helpful in building an effective OpenCV environment in Debian 11; therefore, Method 2 is recommended to get OpenCV.