How to Install OpenCV on CentOS 8

Posted 

3 min read

OpenCV (Open Source Computer Vision Library) is an open-source computer vision library with bindings for C++, Python, and Java and supports all major operating systems. It can take advantage of multi-core processing and features GPU acceleration for real-time operation.

OpenCV is used for a very wide range of applications, including medical image analysis, stitching street view images, surveillance video, detecting and recognizing faces, tracking moving objects, extracting 3D models, and much more.

This article describes how to install OpenCV on CentOS 8. To install the latest stable version of OpenCV from source, scroll down to the Installing OpenCV from the Source section of this tutorial. Choose one of the installation options that works best for you.

Install OpenCV from the CentOS Repository

The OpenCV packages are available from the CentOS 8 standard repositories, but there are no bindings for Python.

Install the OpenCV packages by typing:

sudo dnf install opencv opencv-devel opencv-python

Once the installation is completed, verify that OpenCV library exists by running:

pkg-config --modversion opencv
3.4.1

Installing OpenCV from the Source

Building the OpenCV library from the source allows you to have the latest available version. It will be optimized for your particular system, and you will have complete control over the build options.

Perform the following steps to install the latest OpenCV version from the source:

  1. Install the required and optional dependencies:

    sudo dnf install epel-release git gcc gcc-c++ cmake3 qt5-qtbase-devel \    python3 python3-devel python3-pip cmake python3-devel python3-numpy \    gtk2-devel libpng-devel jasper-devel openexr-devel libwebp-devel \    libjpeg-turbo-devel libtiff-devel tbb-devel libv4l-devel \    eigen3-devel freeglut-devel mesa-libGL mesa-libGL-devel \    boost boost-thread boost-devel gstreamer1-plugins-base
  2. Clone both OpenCV’s and OpenCV contrib repositories:

    mkdir -p ~/opencv_build && cd ~/opencv_buildgit clone https://github.com/opencv/opencv.gitgit clone https://github.com/opencv/opencv_contrib.git

    At the time of writing, the default version in the github repositories is version 4.2.0. If you want to install an older version of OpenCV, navigate to both opencv and opencv_contrib directories and run git checkout <opencv-version>

  3. Once the download is completed, create a temporary build directory, and switch to it:

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

    Configure the OpenCV build with the following CMake command:

    cmake3 -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 ..

    The output will look something like below:

    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/vagrant/opencv_build/opencv/build
  4. Start the compilation process by running the following command:

    make -j8

    Modify the -j flag according to your processor. If you do not know the number of cores your processor, you can find it by typing nproc.

    The compilation may take several minutes or more, depending on your system resources.

  5. Install the OpenCV libraries with:

    sudo make install
  6. Create symlink opencv4.pc file to the /usr/share/pkgconfig directory and run ldconfig to rebuild the libraries cache.

    sudo ln -s /usr/local/lib64/pkgconfig/opencv4.pc /usr/share/pkgconfig/sudo ldconfig

    Check the OpenCV version by typing:

    pkg-config --modversion opencv4
    4.3.0
  7. To verify the Python cv2 module run:

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

Conclusion

We have shown you two different ways to install OpenCV on your CentOS 8 server. The method you choose depends on your requirements and preferences. Even though installing the packaged version from the CentOS repository is easier, building OpenCV from source gives you more flexibility, and it should be your first option when installing OpenCV.

If you have any questions or feedback, feel free to comment below.