Skip to content

Instantly share code, notes, and snippets.

@Mahedi-61
Last active August 4, 2023 03:47
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mahedi-61/33f937cc20deefe9d19883917f46e283 to your computer and use it in GitHub Desktop.
Save Mahedi-61/33f937cc20deefe9d19883917f46e283 to your computer and use it in GitHub Desktop.
Step by step instructions to build and install OpenCV from source on CentOS 7
#!/bin/bash
# This gist is a step by step instructions to build and install OpenCV from source on CentOS 7
# note: The easy and quick way to install is
# sudo yum install opencv opencv-devel opencv-python
# But this easy pypi installation can’t open video files on GNU/Linux distribution or on mac OS X system.
# And on some system opencv binaries provided packages are not compiled.
# Therefor we have no way rather than build it from source.
### first update and upgrade pre-install yum packages.
sudo yum update
sudo yum upgrade
sudo yum install -y epel-release git gcc gcc-c++ cmake3
sudo yum install -y qt5-qtbase-devel gtk2-devel
sudo yum install -y libpng-devel jasper-devel openexr-devel libwebp-devel
sudo yum install -y libjpeg-turbo-devel libtiff-devel libdc1394-devel
sudo yum install -y tbb-devel eigen3-devel gstreamer-plugins-base-devel
sudo yum install -y freeglut-devel mesa-libGL mesa-libGL-devel boost boost-thread boost-devel libv4l-devel
sudo yum install -y libavcodec-devel libavformat-devel libavutil-devel libswscale-devel
# installing dnf
sudo yum install dnf -y
sudo dnf install gstreamer1-devel gstreamer1-plugins-base-tools gstreamer1-devel-docs
sudo dnf gstreamer1-plugins-base-devel gstreamer1-plugins-base-devel-docs
### downloading opencv and opencv_contrib packages from their GitHub repositories
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
### checkout the same version opencv and opencv_contrib
cd opencv
git checkout 3.4.5
cd ..
cd opencv_contrib
git checkout 3.4.5
cd ..
#### now compile and install OpenCV with contrib modules
# create a build directory
cd opencv
mkdir build
cd build
# configure our build using cmake
cmake3 -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D PYTHON3_EXECUTABLE=/usr/bin/python3.6 \
-D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3.6 \
-D PYTHON3_VERSION_STRING="3.6.8" \
-D BUILD_opencv_cudacodec=OFF \
-D WITH_1394=OFF \
-D BUILD_EXAMPLES=ON ..
# OPENCV_EXTRA_MODULES_PATH path can be differenet depending upon opencv_contrib/modules location
# python executable path can be found by entering following code in python terminal
# for python3 execuatable path open the terminal and type python3. and then enter
# import sys; print(sys.executable)
# compile opencV in the same the build folder
make -j4
# here 4 is the availabe core of my processor
# to find number of CPU cores in your machine enter "nproc" command on your terminal
# to speed up compiling process enter highest number of available cores on your processor.
# now install
sudo make install
# my cv2.cpython-36m-x86_64-linux-gnu.so is saved here
cd /usr/local/lib/python3.6/site-packages/
sudo mv cv2.cpython-36m-x86_64-linux-gnu.so cv2.so
# to get site-packages location type: python3 -c "import site; print(site.getsitepackages())"
sudo cp cv2.so /usr/lib/python3.6/site-packages/
## well that's it! to confirm your installation go to the python terminal and enter
## import cv2; print(cv2.__version__)
## if that outputs "3.4.5" and we are done !!!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment