Share your love
How To Install OpenCV on Centos 7
In this article, we want to teach you How To Install OpenCV on Centos 7.
OpenCV is an open-source library that is very useful for computer vision applications such as video analysis, CCTV footage analysis, and image analysis.
It is written in C++ and has more than 2,500 optimized algorithms.
Install OpenCV on Centos 7
You can install OpenCV on your server in two ways. First, you can install it from the Centos repository. Second, you can install OpenCV on Centos 7 from the source.
If you want to install the latest stable version of OpenCV you need to install it from the source.
You need to log in to your server as a non-root user with sudo privileges. To do this, follow our article about the Initial Server Setup with Centos 7.
Install OpenCV from the source
First of all, you need to install the OpenCV dependencies with the following command:
sudo yum install epel-release git gcc gcc-c++ cmake3 qt5-qtbase-devel \
python python-devel python-pip cmake python-devel python34-numpy \
gtk2-devel libpng-devel jasper-devel openexr-devel libwebp-devel \
libjpeg-turbo-devel libtiff-devel libdc1394-devel tbb-devel numpy \
eigen3-devel gstreamer-plugins-base-devel freeglut-devel mesa-libGL \
mesa-libGL-devel boost boost-thread boost-devel libv4l-devel
Now clone the OpenCV and OpenCV contrib repositories with the following commands:
$ mkdir ~/opencv_build && cd ~/opencv_build
$ git clone https://github.com/opencv/opencv.git
$ git clone https://github.com/opencv/opencv_contrib.git
when your download is completed, you need to create a temporary build directory and switch to it with the following command:
cd ~/opencv_build/opencv && mkdir build && cd build
Now configure the OpenCV build on Centos 7 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 ..
In your output you will see:
Output
--
-- Configuring done
-- Generating done
-- Build files have been written to: /root/opencv_build/opencv/build
Then, start the process with the following command:
make -j8
This will take some minutes to complete.
In your output you will see:
Output
[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 following command:
sudo make install
Here create the symbolic link opencv4.pc file to the /usr/share/pkgconfig directory and run the ldconfig to rebuild the libraries cache:
$ sudo ln -s /usr/local/lib64/pkgconfig/opencv4.pc /usr/share/pkgconfig/
$ sudo ldconfig
You can check the OpenCV version with the following command:
pkg-config --modversion opencv4
Output
4.2.0
Now enable the Python cv2 module with the following command:
sudo ln -s /usr/local/lib/python2.7/site-packages/cv2 /usr/lib/python2.7/site-packages/
Then, import the module and verify the installation by printing the OpenCV version:
python -c "import cv2; print(cv2.__version__)"
Also, you can install OpenCV from the default Centos repository.
Install OpenCV from the Centos Repository
You can use the following command to install the OpenCV packages:
sudo yum install opencv opencv-devel opencv-python
When your installation is finished, verify it by its version:
pkg-config --modversion opencv
Output
2.4.5
Or you can import the Python cv2 module and print the OpenCV version:
python -c "import cv2; print(cv2.__version__)"
Conclusion
At this point, you learn to install OpenCV on your server in two different ways.
Hope you enjoy it.