How to Install and Configure OpenCV on CentOS 7 for Python

OpenCV which is short for Open Source Computer Vision Library is, just as the name suggests, an open source computer vision library with bindings for multiple programming languages such as C++, Python, and Java and supports all major operating systems.

OpenCV is also designed to 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 image analysis, video metadata extraction, stitching street view images, surveillance video, detecting and recognizing faces, tracking moving objects, extracting 3D models and much more.

In this article, we will go over how to install and configure OpenCV on CentOS 7. We will be taking a look at two approaches, one would be installing the OpenCV package from the CentOS repo and the second approach would be installing it from the source code.

1. Install OpenCV from the CentOS Repository

The OpenCV package is available from the CentOS 7 distribution repository using the yum package manager, the only issue here is that since CentOS always strives to use proven and stable releases the package here is outdated. At the time of writing, the version in the repositories is 2.4.5.

Install the OpenCV packages by typing:

sudo su
yun update
yum install opencv opencv-devel opencv-python

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

pkg-config --modversion opencv

The out of this command will look like this:

2.4.5

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

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

#output
2.4.5

Install OpenCV from Source

Even though this is a more involving option, compiling 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:

Install the required and optional dependencies as thr root user:

sudo su

yum install epel-release git gcc gcc-c++ cmake3 qt5-qtbase-devel python python-devel python-pip cmake
yum install python-devel numpy python34-numpy gtk2-devel libpng-devel jasper-devel openexr-devel libwebp-devel
yum install libjpeg-turbo-devel libtiff-devel  libdc1394-devel tbb-devel eigen3-devel gstreamer-plugins-base-devel
yum install freeglut-devel mesa-libGL mesa-libGL-devel  boost boost-thread boost-devel libv4l-devel

Now, create a directory which will hold the repositories and clone both OpenCV’s and OpenCV contributed repositories:

sudo su
mkdir ~/opencv_build && cd ~/opencv_build
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

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

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

sudo su
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: /root/opencv_build/opencv/build

Start the compilation process

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

Now you can install OpenCV with the command below:

sudo make install

Symlink

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

#Output
4.0.1

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__)"

#Output
4.0.1-dev

You have successfully installed OpenCV on your CentOS 7 server and configured it with Python. If you have any questions or feedback, feel free to comment below.

Dotlayer is a knowledge-sharing platform built around a community of knowledge-seekers. We aim to make Dotlayer our readers' go-to source for all things tech-related. If you are interested in advertising or sponsorship opportunities, we are happy to discuss. Please submit all inquiries [email protected]. And if you enjoyed this post, we recommend reading our latest featured stories.

How to Install and Configure OpenCV on CentOS 7 for Python
Rate this post

Join over 20,000 users and get our best content

Each week we send 1 email to 20,000 smart entrepreneurs just like you. Enter your email below to join the crew.

Thank you for subscribing.

Something went wrong.

Leave a Comment