Aller au contenu principal

Installing OpenCV on Ubuntu

This is our guide on how to install OpenCV (Open Source Computer Vision Library) on Ubuntu. OpenCV is a powerhouse in the world of computer vision, providing an extensive set of tools and algorithms to help you build applications that can "see" and interpret the world around them. Be it robotics, image processing, or artificial intelligence.

Before we dive into the installation process, let's understand why OpenCV is a big deal. Imagine you're building a robot that needs to navigate through a maze. With OpenCV, you can equip your robot with the ability to detect and follow paths, recognize obstacles, and make decisions in real-time. OpenCV is like giving your projects a pair of eyes and the intelligence to understand what they're looking at.

In this tutorial, we'll cover the step-by-step process of installing OpenCV on Ubuntu, one of the most popular Linux distributions. We'll ensure that you have all the necessary dependencies, compile OpenCV from source, and verify the installation. By the end of this guide, you'll be ready to integrate OpenCV into your projects and start experimenting with computer vision.

Prerequisites

Before we begin, make sure you have the following:

  • A fresh installation of Ubuntu (18.04 or later is recommended).
  • Basic knowledge of the terminal and command-line operations.
  • An internet connection to download the necessary packages.

Step 1: Update and Upgrade Ubuntu

First things first, let's update our package lists and upgrade the existing packages to their latest versions. Open a terminal and execute the following commands:

sudo apt update
sudo apt upgrade

Step 2: Install Dependencies

OpenCV relies on several dependencies that need to be installed before we can compile the library. These include the build tools, image format libraries, and other essential packages. Install them using the following command:

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 libgstreamer-plugins-base1.0-dev \
libgstreamer1.0-dev libssl-dev librtmp-dev libdc1394-22-dev libdrm-dev \
libopencv-dev python3-dev python3-numpy

Step 3: Download OpenCV

We will download the latest version of OpenCV from the official repository. As of this writing, OpenCV 4.x is the latest stable release. Use the following commands 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

The opencv_contrib repository contains extra modules that are not included in the main OpenCV repository.

Step 4: Compile OpenCV

Now, let's compile OpenCV. This process can take some time, so grab a cup of coffee while the magic happens.

cd ~/opencv_build/opencv
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/modules \
-D BUILD_EXAMPLES=ON ..
make -j$(nproc)

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

Step 5: Install OpenCV

Once the compilation is successful, install OpenCV using the following command:

sudo make install

Step 6: Verify the Installation

To verify that OpenCV has been installed correctly, run the following command to display the version of OpenCV:

pkg-config --modversion opencv4

You should see the version number of OpenCV printed in the terminal.

Step 7: Test OpenCV with a Sample Code

Let's write a simple Python script to test our OpenCV installation. Create a file named test_opencv.py and add the following code:

import cv2

# Load an image file
image = cv2.imread('sample.jpg')

# Display the image in a window
cv2.imshow('Sample Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Make sure you have an image file named sample.jpg in the same directory as your script. Run the script using Python 3:

python3 test_opencv.py

A window should pop up displaying the image.

What to Watch Out For

  • Ensure that you have enough disk space before starting the download and compilation process.
  • The compilation process is resource-intensive. Close unnecessary applications to free up system resources.
  • If you encounter any errors during compilation, carefully read the error messages. They often point to missing dependencies or configuration issues.
  • If you're using a virtual environment for Python, make sure to activate it before testing the OpenCV installation with Python.

That's it, we've successfully installed OpenCV on your Ubuntu system. You're now equipped with one of the most powerful tools in computer vision, ready to embark on projects that can analyze and interpret visual data. Explore its documentation, experiment with its examples, and most importantly, have fun building your computer vision applications!