Saltar al contenido principal

Installing OpenCV on CentOS

Welcome to our step-by-step tutorial on installing OpenCV on CentOS. OpenCV, which stands for Open Source Computer Vision Library, is a powerful tool that provides real-time computer vision. It's widely used in various fields, from robotics to medical imaging, and is a staple in the toolkit of any developer working with image processing or computer vision.

CentOS, known for its stability and reliability, is a popular choice for servers and enterprise environments. However, installing OpenCV on CentOS can be quite an adventure due to its strict adherence to stability, which often means older package versions. But fear not! This guide will navigate you through the process, ensuring you have a robust OpenCV setup to tackle your computer vision projects.

Prerequisites

Before we dive into the installation, let's ensure we have all the necessary tools and dependencies. You'll need:

  • A CentOS system (CentOS 7 or 8 will work for this guide).
  • sudo privileges to install packages.
  • Basic familiarity with the command line.

Step 1: Update Your System

First things first, let's update your CentOS system to ensure all existing packages are up to date. This is crucial because it minimizes the chances of conflicts during the installation process.

sudo yum update -y

Step 2: Install Dependencies

OpenCV relies on several dependencies to function correctly. Let's install them using CentOS's package manager, yum.

sudo yum groupinstall "Development Tools" -y
sudo yum install cmake3 ant epel-release -y
sudo yum install numpy scipy python-devel -y

For image format support:

sudo yum install libpng-devel -y
sudo yum install libjpeg-turbo-devel -y
sudo yum install libtiff-devel -y

For video support:

sudo yum install ffmpeg-devel -y
sudo yum install x264-devel -y
sudo yum install libwebp-devel -y

For GUI support (optional):

sudo yum install qt-devel -y
sudo yum install gtk2-devel -y

Step 3: Download OpenCV

Now, let's download the OpenCV source code. We'll use wget to fetch the latest version directly from the official OpenCV repository.

cd ~
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.5.1.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.5.1.zip

Extract the downloaded archives:

unzip opencv.zip
unzip opencv_contrib.zip

Step 4: Compile OpenCV

We'll use cmake to configure the build process. Create a build directory and navigate into it:

mkdir ~/opencv-4.5.1/build
cd ~/opencv-4.5.1/build

Now, run cmake with the appropriate flags. We'll include the path to the opencv_contrib repository to include extra modules.

cmake3 -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-4.5.1/modules \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D PYTHON3_EXECUTABLE=`which python3` \
-D PYTHON3_INCLUDE_DIR=`python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())"` \
-D PYTHON3_PACKAGES_PATH=`python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"` \
-D BUILD_EXAMPLES=ON ..

Once cmake completes successfully, 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 5: Install OpenCV

After the compilation is complete, install OpenCV:

sudo make install

Step 6: Verify Installation

To verify that OpenCV has been installed correctly, run the following command:

pkg-config --modversion opencv4

You should see the installed version of OpenCV printed on the screen.

Step 7: Test OpenCV with Python

If you've built the Python bindings, you can test OpenCV with a simple Python script. Create a file named test_opencv.py:

import cv2

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

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

Run the script:

python3 test_opencv.py

You should see a window displaying the image you loaded.

What to Watch Out For

  • Ensure that you have the correct version of Python installed and that python3 points to the version you intend to use with OpenCV.
  • The cmake command can be quite lengthy and complex. Double-check the paths and flags to avoid configuration errors.
  • If you encounter issues with missing packages or libraries, use yum to search for and install the required dependencies.
  • The OpenCV version in the wget command should be replaced with the latest stable release available at the time of your installation.

That's it! You've successfully installed OpenCV on your CentOS system. Feel free to consult the official OpenCV documentation for advanced usage and features.