
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. In this tutorial, we will show you how to install OpenCV on CentOS 8 system.
OpenCV can be deployed on various platforms, including Windows, Linux, Android, iOS, etc. The OpenCV installation is quite simple.
Prerequisites
Before you start to install OpenCV on CentOS machine, you must logged in as root or sudo user.
Install OpenCV from the CentOS Repository
By default, CentOS 8 standard repositories includes OpenCV package. Run the following command to install the OpenCV packages:
sudo dnf install opencv opencv-devel opencv-python
Verify OpenCV installation by checking version, type:
pkg-config --modversion opencv
3.4.1
Install OpenCV from the Source
If you would prefer the latest version of OpenCV library, you can get it directly from the source. Perform the following steps to install the latest OpenCV version from the source:
1. Install dependencies
Run the following command to install required dependencies:
sudo dnf install epel-release git gcc gcc-c++ cmake3 qt5-qtbase-devel \ python3 python3-devel python3-pip cmake python3-devel python3-numpy \ gtk2-devel libpng-devel jasper-devel openexr-devel libwebp-devel \ libjpeg-turbo-devel libtiff-devel tbb-devel libv4l-devel \ eigen3-devel freeglut-devel mesa-libGL mesa-libGL-devel \ boost boost-thread boost-devel gstreamer1-plugins-base
2. Clone repositories
Next, clone OpenCV and OpenCV contrib repositories using following commands:
mkdir -p ~/opencv_build && cd ~/opencv_build
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
Currently, while writing this tutorial, the latest stable version is 4.3.0
.
3. Create build directory
Now, create a temporary build directory and navigate to inside it:
cd ~/opencv_build/opencv && mkdir build && cd build
Use the CMake command to configure the OpenCV 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_build/opencv_contrib/modules \ -D BUILD_EXAMPLES=ON ..
It should return output as below:
-- Configuring done -- Generating done -- Build files have been written to: /home/vagrant/opencv_build/opencv/build
4. Start compilation
Execute the below command to start the compilation process:
make -j4
Ensure that here in -j
option you should provide number of processor as per your system. You can find number of processor by typing nproc
.
Depending on your system resources, compilation process may take few minutes.
5. Install OpenCV libraries
To install OpenCV libraries, type:
sudo make install
6. Create 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
7. Verify installation
pkg-config --modversion opencv4
4.3.0
Conclusion
You successfully learned multiple ways to install OpenCV on your CentOS 8 server.
If you have any questions or feedback, feel free to comment below.
Hi, I am using CentOS 8 stream. I new to linux itself. I want cv2 for programming. I tried the above mentioned method. I could not install cmake3. But I tried installing with cmake. But at the end, when I run pkg-config –modversion opencv4 command, I am getting the error stating that the cv2 module does not exist. Can you clear me this doubt?