본문으로 건너뛰기

Installing OpenCV on Debian

This is our guide on installing OpenCV (Open Source Computer Vision Library) on a Debian-based system. OpenCV is a powerful library used for real-time computer vision. It's widely used in various fields such as robotics, artificial intelligence, and even in medical image processing.

Throughout this tutorial, we'll walk through each step required to get OpenCV up and running on your Debian system. We'll cover the prerequisites, the installation process, and some basic usage examples to verify the installation. We'll also discuss common pitfalls and how to avoid them.

Prerequisites

Before we dive into the installation, ensure that your system is up to date:

sudo apt update
sudo apt upgrade

You'll also need the following prerequisites installed:

  • GCC: The GNU Compiler Collection is required for compiling OpenCV.
  • CMake: A cross-platform build system that OpenCV uses for its build process.
  • Python: (Optional) If you want to use OpenCV with Python, ensure you have Python installed.
  • OpenGL, GTK, and other dependencies for GUI support.

You can install these with the following command:

sudo apt install build-essential cmake python3-dev python3-numpy \
libgtk2.0-dev pkg-config libavcodec-dev \
libavformat-dev libswscale-dev libv4l-dev \
libxvidcore-dev libx264-dev libjpeg-dev \
libpng-dev libtiff-dev libgtk-3-dev \
libopenexr-dev libgstreamer-plugins-base1.0-dev \
libgstreamer1.0-dev

Step 1: Download OpenCV

First, create a directory for downloading and building OpenCV:

mkdir ~/opencv_build && cd ~/opencv_build

Next, download the latest version of OpenCV and the contrib repository (optional, for extra modules):

wget -O opencv.zip https://github.com/opencv/opencv/archive/4.x.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.x.zip

Replace 4.x with the latest version number available on the OpenCV GitHub repository.

Unzip the downloaded files:

unzip opencv.zip
unzip opencv_contrib.zip

Step 2: Compile OpenCV

Now, we'll use CMake to configure the build process:

cd opencv-4.x
mkdir build && cd 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-4.x/modules \
-D BUILD_EXAMPLES=ON ..

Once CMake has successfully configured the build, compile OpenCV using make:

make -j$(nproc)

The -j$(nproc) flag tells make to use all available CPU cores to speed up the build process.

Step 3: Install OpenCV

After the compilation is complete, install OpenCV:

sudo make install
sudo ldconfig

Step 4: Verify the Installation

To verify that OpenCV has been installed correctly, you can run the following commands:

pkg-config --modversion opencv4

This should output the version of OpenCV installed.

For Python, you can verify the installation by running:

import cv2
print(cv2.__version__)

You should see the OpenCV version printed in the terminal.

Usage Examples

Here's a simple C++ example to capture video from the first camera device:

#include <opencv2/opencv.hpp>

int main() {
cv::VideoCapture cap(0); // open the default camera
if (!cap.isOpened()) {
std::cerr << "Error: Could not open camera." << std::endl;
return 1;
}

cv::Mat frame;
while (true) {
cap >> frame; // get a new frame from camera
if (frame.empty()) {
std::cerr << "Error: Blank frame grabbed." << std::endl;
break;
}
cv::imshow("Camera Feed", frame);
if (cv::waitKey(1) == 'q') break; // quit on 'q'
}
cap.release();
cv::destroyAllWindows();
return 0;
}

Compile this code with:

g++ -o camera_capture camera_capture.cpp `pkg-config --cflags --libs opencv4`

Run the compiled program:

./camera_capture

You should see a window displaying the camera feed.

What to Watch Out For

  • Ensure that you have sufficient permissions to write to the installation directory (/usr/local by default).
  • If you encounter issues with video codecs, make sure you have installed all the necessary multimedia libraries.
  • When using Python, ensure that the OpenCV Python package is correctly installed and that you're using the correct version of Python that OpenCV was built against.

Enjoy exploring the world of computer vision!