How to Install OpenCV on Debian 10

OpenCV is an open-source and cross-platform library of computer vision and machine learning software. It is used to develop real-time computer vision applications including, tracking moving objects, surveillance video, medical image analysis and many more. Mostly computer vision is used in self-driving cars, robotics as well as in photo correction apps. It supports multiple languages including, C++, Java, Python, Android SDK and MATLAB/OCTAVE. It can be installed on most operating systems including, Linux, Windows, MacOS, Android, OpenBSD, FreeBSD and many more.

In this tutorial, we will learn how to install OpenCV on Debian.

Prerequisites

  • A server running Debian 10 - 12.
  • A root password is configured on your server.

Install OpenCV from Debian Repository

The OpenCV module is available in the Debian repository by default. You can install it with the following command:

apt-get install python3-opencv -y

After installing the OpenCV module, verify the installed version of a module with the following command:

python3 -c "import cv2; print(cv2.__version__)"

You should get the following output:

3.2.0

Install OpenCV from the Source

It is a recommended way to install the OpenCV from the source. So you can optimize it for a specific system.

First, install all the dependencies required to build OpenCV with the following command:

apt-get 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 -y

After installing all the dependencies, download the latest version of OpenCV and OpenCV contrib with the following command:

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

Next, change the directory to the opencv with the following command:

cd opencv

Next, create a temporary build directory and change the directory to it:

mkdir build
cd build

Next, set up the OpenCV build with 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_contrib/modules -D BUILD_EXAMPLES=ON ..

After successful completion, you should see the following screen:

Next, compile the OpenCV with the following command:

make -j2

The process may take some time to finish. Once finished, install the OpenCV with the following command:

make install

Next, verify the OpenCV installation with the following command:

pkg-config --modversion opencv4

You should get the following output:

4.2.0

You can also check the version of the OpenCV module with the following command:

python3 -c "import cv2; print(cv2.__version__)"

You should see the following output:

4.2.0-dev

Conclusion

Congratulations! You have successfully installed OpenCV on Debian. You can now start developing your first application with OpenCV.

Share this page:

0 Comment(s)