Read in 3 minutes

last updated 

How to Install OpenCV on CentOS 7

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.

In this tutorial, we will show how to install OpenCV on CentOS 7.

Install OpenCV from the CentOS Repository

The OpenCV package is available from the CentOS 7 distribution repository but is it pretty outdated. At the time of writing, the version in the repositories is 2.4.5.

Install the OpenCV packages by typing:

sudo yum install opencv opencv-devel opencv-python

Once the installation is completed you can verify it by running:

pkg-config --modversion opencv
2.4.5

Or by importing the Python cv2 module and print the OpenCV version:

python -c "\
import cv2
print(cv2.__version__)"
2.4.5

Install OpenCV from Source

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

To install the latest OpenCV version from the source, follow these steps:

  1. Install the required and optional dependencies:

    sudo yum install epel-release git gcc gcc-c++ cmake3 qt5-qtbase-devel python python-devel python-pip cmake
    sudo yum install python-devel numpy python34-numpy gtk2-devel libpng-devel jasper-devel openexr-devel libwebp-devel
    sudo yum install libjpeg-turbo-devel libtiff-devel  libdc1394-devel tbb-devel eigen3-devel gstreamer-plugins-base-devel
    sudo yum install freeglut-devel mesa-libGL mesa-libGL-devel  boost boost-thread boost-devel libv4l-devel
  2. Create a directory which will hold the repositories and clone both OpenCV’s 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

    At the time of writing, the default version in the github repositories is version 4.0.0. If you want to install an older version of OpenCV, cd 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 ..

    Once the CMake build system is finalized you will see something like below:

    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/linuxize/opencv_build/opencv/build
  4. Start the compilation process by running the following command. 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. My system has 8 cores, so I am using the -j8 flag

    make -j8

    The compilation may take several minutes or more, depending on your system configuration. When completed you will see something like this:

    [100%] Built target example_tutorial_Threshold_inRange
    [100%] Linking CXX shared module ../../lib/cv2.so
    [100%] Built target opencv_python2
  5. Install OpenCV 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.0.1
  7. To enable the Python cv2 module run:

    sudo ln -s /usr/local/lib/python2.7/site-packages/cv2  /usr/lib/python2.7/site-packages/

    Import the module and print the OpenCV version:

    python -c "\
    import cv2
    print(cv2.__version__)"
    4.0.1-dev

Conclusion

We have shown you two different ways to install OpenCV on your CentOS 7 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.