Jan 27, 2023 3 min read

How to Install OpenCV on Debian 10 Linux

In this tutorial, you will set up OpenCV on Debian 10, Buster.

Install OpenCV on Debian 10 Linux
Table of Contents

Choose a different version or distribution

Introduction

Before we begin talking about how to install OpenCV on Debian 10 Linux, let's briefly understand – What is OpenCV?

OpenCV (Open Source Computer Vision Library) is an open-source computer vision library with C++, Python, and Java bindings. It is used for a wide variety of tasks, including 3D model extraction, face detection and recognition, street view image stitching, medical image analysis, tracking moving objects, and surveillance video.

OpenCV has GPU acceleration for real-time operation and can make use of multicore processing.

In this tutorial, you will set up OpenCV on Debian 10, Buster. The most convenient method for most users to install OpenCV on Debian is through the apt management tool. Scroll down to the section of this tutorial titled Installing OpenCV from the Source if you want to install the most recent stable version of OpenCV from the source.

Select the installation method that best meets your requirements.

Install OpenCV from the Debian Repository

The default Debian repository includes the OpenCV Python module. OpenCV version 3.2, which is no longer supported, is available in the default Debian repository as of the time of writing.

Install the OpenCV Python module by typing:

sudo apt update
sudo apt install python3-opencv

All the packages required to run OpenCV will be installed by the aforementioned command.

Import the cv2 module and print the OpenCV version to confirm the installation:

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

3.2.0

Install the python-opencv package if you intend to install OpenCV with Python 2 bindings.

Installing OpenCV from the Source

Installing OpenCV is best done by building the OpenCV library from the source. You will have total control over the build choices, and they will be tailored to your specific system.

Follow these steps to install the most recent OpenCV version directly from the source

1) Install the following dependencies, both essential and optional:

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

2) Use the commands below to clone the OpenCV and OpenCV contrib repositories:

mkdir ~/opencv_build && cd ~/opencv_build
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

Version 4.2.0 is the default version at the time of writing in the GitHub repositories. cd to the opencv and opencv_contrib directories, then run git checkout < opencv-version> to install an older version of OpenCV.

3) When the download is completed, make a temporary build directory and navigate to it:

cd ~/opencv_build/opencv
mkdir build && cd build

Using CMake, configure the OpenCV 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 OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \
    -D BUILD_EXAMPLES=ON ..

You will see something like the following after the CMake build system is complete:

Output

...
-- Configuring done
-- Generating done
-- Build files have been written to: /home/pi/opencv_build/opencv/build

4) Start the compilation procedure:

make -j2

The -j flag should be changed to suit your processor. If you are unsure about your processor's core count, type nproc to find out.

Depending on your system configuration, the compilation may take several minutes or longer. When finished, you should see something similar to this:

Output
...
[100%] Linking CXX executable ../../bin/example_tutorial_imgcodecs_imwrite
[100%] Built target example_tutorial_goodFeaturesToTrack_Demo
[100%] Built target example_tutorial_imgcodecs_imwrite

5) Type the following to install OpenCV:

sudo make install
Output

...
-- Installing: /usr/local/share/opencv4/samples/python/video_threaded.py
-- Installing: /usr/local/share/opencv4/samples/python/video_v4l2.py
-- Installing: /usr/local/share/opencv4/samples/python/watershed.py

6) Enter the following command to see the OpenCV version and determine whether OpenCV has been successfully installed:

pkg-config --modversion opencv4
Output

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

4.2.0-dev

Conclusion

We have demonstrated two methods for installing OpenCV on Debian 10. The approach you select is determined by your interests and requirements. Installing OpenCV from the Ubuntu repository's packaged version is simpler, but creating OpenCV from source allows you more versatility and should be your first choice.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.