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 standard repositories,  but is it pretty outdated. If you want to install the latest stable  version of OpenCV from source, scroll down to the Installing OpenCV from the Source section of this tutorial.

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

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.

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

Install the required and optional dependencies:

sudo yum install epel-release git gcc gcc-c++ cmake3 qt5-qtbase-devel \

Clone both OpenCV’s and OpenCV contrib repositories:

mkdir ~/opencv_build && cd ~/opencv_build

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, cd to  both opencv and opencv_contrib directories and run git checkout <opencv-version>

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 \

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

-- Configuring done
-- Generating done
-- Build files have been written to: /home/black/opencv_build/opencv/build

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

Install OpenCV with:

sudo make install

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/

Check the OpenCV version by typing:

pkg-config --modversion opencv4
4.2.0

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 verify the installation by printing the OpenCV version:

python -c "import cv2; print(cv2.__version__)"

Copy

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