How to Install OpenCV on CentOS 7

Install OpenCV on CentOS 7

In this article, we will have explained the necessary steps to install and configure OpenCV on CentOS 7. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

OpenCV, also known as Open Source Computer Vision Library, is an open-source cross-platform computer vision algorithm library. Nowadays, OpenCV is being widely used in all kinds of visual processing areas, such as facial recognition, gesture recognition, human-computer interaction, Object identification, motion tracking, etc.

Install OpenCV on CentOS

Step 1. The first command will update the package lists to ensure you get the latest version and dependencies.

sudo yum install epel-release
sudo yum update

Step 2. Install OpenCV.

  • Install OpenCV from Source

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

Next, create a directory that will hold the repositories and clone both OpenCV’s:

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

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

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

Then, 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 ..

Now 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 in your processor you can find it by typing nproc. My system has 4 cores, so I am using the -j4 flag:

make -j4

Install OpenCV with:

sudo make install

Next steps, 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:

pkg-config --modversion opencv4

To enable the Python cv2 module to run:

sudo ln -s /usr/local/lib/python2.7/site-packages/cv2  /usr/lib/python2.7/site-packages/
  • Install OpenCV from the CentOS Repository

Install the OpenCV packages using the following command:

sudo yum install opencv opencv-devel opencv-python

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

pkg-config --modversion opencv

Congratulation, you have learned how to install and configure OpenCV on CentOS 7. If you have any questions, please leave a comment below.