How to install OpenCV (CV2) on CentOS or RHEL 7

This post will guide you how to install OpenCV with the default YUM repositories on your CentOS or RHEL 7 Linux server. How do I install OpenCV-Python from source code on CentOS or RHEL Linux 7.

What is OpenCV (CV2)?


OpenCV  (Open Source Computer Vision Library) is released under a BSD license and hence it’s free for both academic and commercial use. It has C++, Python and Java interfaces and supports Windows, Linux, Mac OS, iOS and Android. OpenCV has a modular structure, which means that the package includes several shared or static libraries. And OpenCV is a multi-platform open source computer vision library.

install opencv1

Installing OpenCV from the Default YUM Repository


The OpenCV package is available from the default CentOS 7 or RHEL 7 Repository. You can use the following command to install it, type:

$ yum install opencv-python opencv-devel

Outputs:

[root@devops ~]# yum install opencv-python opencv-devel
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package opencv-devel.x86_64 0:2.4.5-3.el7 will be installed
--> Running transaction check
---> Package libXxf86vm.x86_64 0:1.1.4-1.el7 will be installed
---> Package libwayland-client.x86_64 0:1.15.0-1.el7 will be installed
---> Package libwayland-server.x86_64 0:1.15.0-1.el7 will be installed
---> Package libxshmfence.x86_64 0:1.2-1.el7 will be installed
---> Package mesa-libgbm.x86_64 0:18.0.5-4.el7_6 will be installed
---> Package mesa-libglapi.x86_64 0:18.0.5-4.el7_6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=========================================================================================================================
Package Arch Version Repository Size
=========================================================================================================================
Installing:
opencv-devel x86_64 2.4.5-3.el7 base 447 k
opencv-python x86_64 2.4.5-3.el7 base 376 k
Installing for dependencies:
OpenEXR-libs x86_64 1.7.1-7.el7 base 217 k
atk x86_64 2.28.1-1.el7 base 263 k
atlas x86_64 3.10.1-12.el7 base 4.5 M
blas x86_64 3.4.2-8.el7 base 399 k
cairo x86_64 1.15.12-3.el7 base 741 k
cdparanoia-libs x86_64 10.2-17.el7 base 56 k
cups-libs x86_64 1:1.6.3-35.el7 base 357 k
dejavu-fonts-common noarch 2.33-6.el7 base 64 k
dejavu-sans-fonts noarch 2.33-6.el7 base 1.4 M
fontconfig x86_64 2.13.0-4.3.el7 base 254 k
fontpackages-filesystem noarch 1.44-8.el7 base 9.9 k
fribidi x86_64 1.0.2-1.el7 base 79 k
gdk-pixbuf2 x86_64 2.36.12-3.el7 base 570 k
graphite2 x86_64 1.3.10-1.el7_3 base 115 k
gstreamer x86_64 0.10.36-7.el7 base 958 k
gstreamer-plugins-base x86_64 0.10.36-10.el7 base 1.2 M
gstreamer-tools x86_64 0.10.36-7.el7 base 27 k
gtk-update-icon-cache x86_64 3.22.30-3.el7 base 28 k
gtk2 x86_64 2.24.31-1.el7 base 3.4 M
harfbuzz x86_64 1.7.5-2.el7 base 267 k
hicolor-icon-theme noarch 0.12-7.el7 base 42 k
ilmbase x86_64 1.0.3-7.el7 base 100 k
iso-codes noarch 3.46-2.el7 base 2.7 M
jasper-libs x86_64 1.900.1-33.el7 base 150 k
jbigkit-libs x86_64 2.0-11.el7 base 46 k
lapack x86_64 3.4.2-8.el7 base 5.4 M
libX11 x86_64 1.6.5-2.el7 base 606 k
libX11-common noarch 1.6.5-2.el7 base 164 k
libXau x86_64 1.0.8-2.1.el7 base 29 k
libXcomposite x86_64 0.4.4-4.1.el7 base 22 k
libXcursor x86_64 1.1.15-1.el7 base 30 k
libXdamage x86_64 1.1.4-4.1.el7 base 20 k
libXext x86_64 1.3.3-3.el7 base 39 k
libXfixes x86_64 5.0.3-1.el7 base 18 k
libXft x86_64 2.3.2-2.el7 base 58 k
libXi x86_64 1.7.9-1.el7 base 40 k
libXinerama x86_64 1.1.3-2.1.el7 base 14 k
libXrandr x86_64 1.5.1-2.el7 base 27 k
libXrender x86_64 0.9.10-1.el7 base 26 k
libXv x86_64 1.0.11-1.el7 base 18 k
libXxf86vm x86_64 1.1.4-1.el7 base 18 k
libgfortran x86_64 4.8.5-36.el7_6.1 updates 300 k
libglvnd x86_64 1:1.0.1-0.8.git5baa1e5.el7 base 89 k
libglvnd-egl x86_64 1:1.0.1-0.8.git5baa1e5.el7 base 44 k
libglvnd-glx x86_64 1:1.0.1-0.8.git5baa1e5.el7 base 125 k
libjpeg-turbo x86_64 1.2.90-6.el7 base 134 k
libogg x86_64 2:1.3.0-7.el7 base 24 k
libquadmath x86_64 4.8.5-36.el7_6.1 updates 189 k
libthai x86_64 0.1.14-9.el7 base 187 k
libtheora x86_64 1:1.1.1-8.el7 base 136 k
libtiff x86_64 4.0.3-27.el7_3 base 170 k
libv4l x86_64 0.9.5-4.el7 base 194 k
libvisual x86_64 0.4.0-16.el7 base 138 k
libvorbis x86_64 1:1.3.3-8.el7.1 base 204 k
libwayland-client x86_64 1.15.0-1.el7 base 33 k
libwayland-server x86_64 1.15.0-1.el7 base 39 k
libxcb x86_64 1.13-1.el7 base 214 k
libxshmfence x86_64 1.2-1.el7 base 7.2 k
mesa-libEGL x86_64 18.0.5-4.el7_6 updates 102 k
mesa-libGL x86_64 18.0.5-4.el7_6 updates 162 k
mesa-libgbm x86_64 18.0.5-4.el7_6 updates 38 k
mesa-libglapi x86_64 18.0.5-4.el7_6 updates 44 k
numpy x86_64 1:1.7.1-13.el7 base 2.8 M
opencv x86_64 2.4.5-3.el7 base 3.9 M
opencv-core x86_64 2.4.5-3.el7 base 1.8 M
orc x86_64 0.4.26-1.el7 base 166 k
pango x86_64 1.42.4-1.el7 base 280 k
pixman x86_64 0.34.0-1.el7 base 248 k
python-nose noarch 1.3.7-1.el7 base 276 k

Transaction Summary
=========================================================================================================================
Install 2 Packages (+69 Dependent packages)

Total download size: 37 M
Installed size: 146 M
Is this ok [y/d/N]:y

After the installation process of OpenCV is completed with the above commands on your CentOS system, you can use the following commands to load the appropriate cv2 library in Python terminal, so that you can verify if the OpenCV has been installed successfully, type:

import cv2
cv2.__version__

Outputs:

[root@devops ~]# python
Python 2.7.5 (default, Oct 30 2018, 23:45:53)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'2.4.5'
>>>

Or you can run anther command to get the version of OpenCV, type:

$ pkg-config --modversion opencv

Outputs:

[root@devops ~]# pkg-config --modversion opencv
2.4.5

From the above outputs, you should notice that the current version of OpenCV is 2.4.5. and this version is also very outdated.

Installing OpenCV from Source Code


If you want to install the latest version of OpenCV on your CentOS 7 or RHEL 7 system, you can install it from source code of OpenCV. Just do the following steps:

#1 updating your system with yum update command to ensure it is up-to-date, type:

$ yum cleall all
$ yum update -y

#2 installing some dependency packages for OpenCV:

$ yum groupinstall "Development Tools"
$ yum install epel-release cmake3 gcc gtk2-devel numpy pkconfig

#3 Downloading the latest source of OpenCV library from OpenCV’s GitHub Repository  and uncompress archive file with the following commands:

$ wget https://github.com/opencv/opencv/archive/4.0.1.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.0.1.zip

$ unzip 4.0.1.zip
$ unzip opencv_contrib.zip

Outputs:

[root@devops ~]# wget https://github.com/opencv/opencv/archive/4.0.1.zip
--2018-09-17 13:22:46-- https://github.com/opencv/opencv/archive/4.0.1.zip
Resolving github.com (github.com)... 52.74.223.119, 13.250.177.223, 13.229.188.59
Connecting to github.com (github.com)|52.74.223.119|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://codeload.github.com/opencv/opencv/zip/4.0.1 [following]
--2018-09-17 13:22:48-- https://codeload.github.com/opencv/opencv/zip/4.0.1
Resolving codeload.github.com (codeload.github.com)... 13.250.162.133, 13.229.189.0, 54.251.140.56
Connecting to codeload.github.com (codeload.github.com)|13.250.162.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 91163087 (87M) [application/zip]
Saving to: ‘4.0.1.zip’

100%[===============================================================================>] 91,163,087 2.58MB/s in 32s

2018-09-17 13:23:22 (2.69 MB/s) - ‘4.0.1.zip’ saved [91163087/91163087]

[root@devops ~]# wget wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.0.1.zip
--2018-09-17 14:54:30-- http://wget/
Resolving wget (wget)... failed: Name or service not known.
wget: unable to resolve host address ‘wget’
--2018-09-17 14:54:30-- https://github.com/opencv/opencv_contrib/archive/4.0.1.zip
Resolving github.com (github.com)... 13.250.177.223, 52.74.223.119, 13.229.188.59
Connecting to github.com (github.com)|13.250.177.223|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://codeload.github.com/opencv/opencv_contrib/zip/4.0.1 [following]
--2018-09-17 14:54:32-- https://codeload.github.com/opencv/opencv_contrib/zip/4.0.1
Resolving codeload.github.com (codeload.github.com)... 13.229.189.0, 13.250.162.133, 54.251.140.56
Connecting to codeload.github.com (codeload.github.com)|13.229.189.0|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/zip]
Saving to: ‘opencv_contrib.zip’

[ <=> ] 61,456,879 2.88MB/s in 34s

2018-09-17 14:55:07 (1.75 MB/s) - ‘opencv_contrib.zip’ saved [61456879]

FINISHED --2018-09-17 14:55:07--
Total wall clock time: 37s
Downloaded: 1 files, 59M in 34s (1.75 MB/s)

[root@devops ~]# unzip 4.0.1.zip
......
inflating: opencv-4.0.1/samples/wp8/OpenCVXaml/OpenCVXaml/Properties/AssemblyInfo.cs
inflating: opencv-4.0.1/samples/wp8/OpenCVXaml/OpenCVXaml/Properties/WMAppManifest.xml
creating: opencv-4.0.1/samples/wp8/OpenCVXaml/OpenCVXaml/Resources/
inflating: opencv-4.0.1/samples/wp8/OpenCVXaml/OpenCVXaml/Resources/AppResources.Designer.cs
inflating: opencv-4.0.1/samples/wp8/OpenCVXaml/OpenCVXaml/Resources/AppResources.resx
inflating: opencv-4.0.1/samples/wp8/readme.txt

Note: It will create one folder “opencv-4.0.1” in current directory.

#4 changing the current directory to opencv-4.0.1, and create a new build directory and navigate to it. type:

$ cd opencv-4.0.1
$ mkdir build
$ cd build

#5 setup OpenCV build using Cmake, type:

$ 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_contrib-4.0.1/modules \
-D BUILD_EXAMPLES=ON ..

Outputs:

[root@devops build]# 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_contrib-4.0.1/modules \
-D BUILD_EXAMPLES=ON ..

......
-- The CXX compiler identification is GNU 4.8.5
-- The C compiler identification is GNU 4.8.5
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
......
-- Excluding from source files list: <BUILD>/modules/dnn/layers/layers_common.avx512_skx.cpp
--
-- General configuration for OpenCV 4.0.1 =====================================
-- Version control: unknown
--
-- Platform:
-- Timestamp: 2018-09-17T17:39:51Z
-- Host: Linux 3.10.0-957.10.1.el7.x86_64 x86_64
-- CMake: 3.13.4
-- CMake generator: Unix Makefiles
-- CMake build tool: /usr/bin/gmake
-- Configuration: DEBUG
--
-- CPU/HW features:
-- Baseline: SSE SSE2 SSE3
-- requested: SSE3
-- Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX AVX2
-- requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
-- SSE4_1 (7 files): + SSSE3 SSE4_1
-- SSE4_2 (2 files): + SSSE3 SSE4_1 POPCNT SSE4_2
-- FP16 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
-- AVX (5 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
-- AVX2 (13 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
--
-- C/C++:
-- Built as dynamic libs?: YES
-- C++ Compiler: /usr/bin/c++ (ver 4.8.5)
-- C++ flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -Wno-missing-field-initializers -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O2 -DNDEBUG -DNDEBUG
-- C++ flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -Wno-missing-field-initializers -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
-- C Compiler: /usr/bin/cc
-- C flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -Wno-missing-field-initializers -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -O2 -DNDEBUG -DNDEBUG
-- C flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -Wno-missing-field-initializers -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG
-- Linker flags (Release):
-- Linker flags (Debug):
-- ccache: NO
-- Precompiled headers: YES
-- Extra dependencies: dl m pthread rt
-- 3rdparty dependencies:
--
-- OpenCV modules:
-- To be built: calib3d core dnn features2d flann gapi highgui imgcodecs imgproc java_bindings_generator ml objdetect photo python_bindings_generator stitching ts video videoio
-- Disabled: world
-- Disabled by dependency: -
-- Unavailable: java js python2 python3
-- Applications: tests perf_tests apps
-- Documentation: NO
-- Non-free algorithms: NO
--
-- GUI:
-- GTK+: YES (ver 2.24.31)
-- GThread : YES (ver 2.56.1)
-- GtkGlExt: NO
-- VTK support: NO
--
-- Media I/O:
-- ZLib: /lib64/libz.so (ver 1.2.7)
-- JPEG: libjpeg-turbo (ver 1.5.3-62)
-- WEBP: build (ver encoder: 0x020e)
-- PNG: /lib64/libpng.so (ver 1.5.13)
-- TIFF: build (ver 42 - 4.0.9)
-- JPEG 2000: build (ver 1.900.1)
-- OpenEXR: build (ver 1.7.1)
-- HDR: YES
-- SUNRASTER: YES
-- PXM: YES
-- PFM: YES
--
-- Video I/O:
-- DC1394: NO
-- FFMPEG: NO
-- avcodec: NO
-- avformat: NO
-- avutil: NO
-- swscale: NO
-- avresample: NO
-- GStreamer: NO
-- v4l/v4l2: linux/videodev2.h
--
-- Parallel framework: pthreads
--
-- Trace: YES (with Intel ITT)
--
-- Other third-party libraries:
-- Intel IPP: 2019.0.0 Gold [2019.0.0]
-- at: /root/opencv-4.0.1/build/3rdparty/ippicv/ippicv_lnx/icv
-- Intel IPP IW: sources (2019.0.0)
-- at: /root/opencv-4.0.1/build/3rdparty/ippicv/ippicv_lnx/iw
-- Lapack: NO
-- Eigen: NO
-- Custom HAL: NO
-- Protobuf: build (3.5.1)
--
-- OpenCL: YES (no extra features)
-- Include path: /root/opencv-4.0.1/3rdparty/include/opencl/1.2
-- Link libraries: Dynamic load
--
-- Python (for build): /usr/bin/python2.7
--
-- Java:
-- ant: NO
-- JNI: NO
-- Java wrappers: NO
-- Java tests: NO
--
-- Install to: /usr/local
-- -----------------------------------------------------------------
--
-- Configuring done
-- Generating done
-- Build files have been written to: /root/opencv-4.0.1/build
[root@devops build]# echo $?
0

#6 Compiling OpenCV with the following make command, type:

$ make

After compiling completed, you should see the outputs like below:

[100%] Built target opencv_interactive-calibration
Scanning dependencies of target opencv_version
make[2]: Warning: File `../apps/version/opencv_version.cpp' has modification time 8254735 s in the future
[100%] Building CXX object apps/version/CMakeFiles/opencv_version.dir/opencv_version.cpp.o
[100%] Linking CXX executable ../../bin/opencv_version
make[2]: warning: Clock skew detected. Your build may be incomplete.
[100%] Built target opencv_version

Note: The compiling process will take a few minutes to complete. If your compiling fails or hangs, you need to delete your build directory, and recreate it, and re-run the above cmake command and make command.

#7 Installing OpenCV with make install command, type:

$ sudo make install

Outputs:

$ sudo make install
......
-- Installing: /usr/local/share/opencv4/lbpcascades/lbpcascade_frontalface_improved.xml
-- Installing: /usr/local/share/opencv4/lbpcascades/lbpcascade_profileface.xml
-- Installing: /usr/local/share/opencv4/lbpcascades/lbpcascade_silverware.xml
-- Installing: /usr/local/bin/opencv_annotation
-- Set runtime path of "/usr/local/bin/opencv_annotation" to "/usr/local/lib64"
-- Installing: /usr/local/bin/opencv_visualisation
-- Set runtime path of "/usr/local/bin/opencv_visualisation" to "/usr/local/lib64"
-- Installing: /usr/local/bin/opencv_interactive-calibration
-- Set runtime path of "/usr/local/bin/opencv_interactive-calibration" to "/usr/local/lib64"
-- Installing: /usr/local/bin/opencv_version
-- Set runtime path of "/usr/local/bin/opencv_version" to "/usr/local/lib64"

[root@devops build]# echo $?
0

#8 you can verify if OpenCV is installed successfully on your system with the following command to check the OpenCV version:

$ opencv_version

Outputs:

[root@devops build]# opencv_version
4.0.1

Conclusion


You should know that how to install OpenCV library on CentOS 7 or RHEL 7 from this guide, and you also know how to install the latest version of OpenCV from source on CentOS 7 Linux server. If you want to see more information about OpenCV, you can go the official web site of OpenCV directly.

You might also like:

Sidebar



back to top