• Home
  • >
  • Install
  • >
  • Install OpenCV 3.4.4 on CentOS 7 (C++ and Python)

Install OpenCV 3.4.4 on CentOS 7 (C++ and Python)

OpenCV released OpenCV-3.4.4 and OpenCV-4.0.0 on 20th November. There have been a lot of bug fixes and other changes in these versions. The release highlights are as follows: OpenCV is now C++11 library and requires C++11-compliant compiler. Minimum required CMake version has been raised to 3.5.1. A lot of C

rhel opencv feature image
rhel opencv feature image

OpenCV released OpenCV-3.4.4 and OpenCV-4.0.0 on 20th November. There have been a lot of bug fixes and other changes in these versions. The release highlights are as follows:

  • OpenCV is now C++11 library and requires C++11-compliant compiler. Minimum required CMake version has been raised to 3.5.1.
  • A lot of C API from OpenCV 1.x has been removed.
  • Persistence (storing and loading structured data to/from XML, YAML or JSON) in the core module has been completely reimplemented in C++ and lost the C API as well.
  • New module G-API has been added, it acts as an engine for very efficient graph-based image procesing pipelines.
  • dnn module now includes experimental Vulkan backend and supports networks in ONNX format.
  • The popular Kinect Fusion algorithm has been implemented and optimized for CPU and GPU (OpenCL)
    QR code detector and decoder have been added to the objdetect module.
  • Very efficient and yet high-quality DIS dense optical flow algorithm has been moved from opencv_contrib to the video module.

In this post, we will provide a bash script for installing OpenCV-3.4.4 (C++, Python 2.7 and Python 3.4) on CentOS 7. We will also briefly study the script to understand what’s going in it. Note that this script will install OpenCV in a local directory and not on the entire system. Let’s jump in 🙂

If you are still not able to install OpenCV on your system, but want to get started with it, we suggest using our docker images with pre-installed OpenCV, Dlib, miniconda and jupyter notebooks along with other dependencies as described in this post.

1. Select OpenCV Version to install

 echo "OpenCV installation by learnOpenCV.com" echo "Installing OpenCV - 3.4.4" #Specify OpenCV version cvVersion="3.4.4" 

We are also going to clean build directories and create installation directory.

 # Clean build directories rm -rf opencv rm -rf opencv_contrib mkdir installation mkdir installation/OpenCV-"$cvVersion" 

Finally, we will be storing the current working directory in cwd variable. We are also going to refer to this directory as OpenCV_Home_Dir throughout this article.

 # Save current working directory cwd=$(pwd) 

2. Install packages

Next we are going to install Python 3.4, and other libraries and packages that will be required for OpenCV installation.

 sudo yum -y install epel-release sudo yum -y install git gcc gcc-c++ cmake3 sudo yum -y install qt5-qtbase-devel sudo yum install -y python34 python34-devel python34-pip sudo yum install -y python python-devel python-pip sudo yum -y install python-devel numpy python34-numpy sudo yum -y install gtk2-devel sudo rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm sudo yum install -y ffmpeg sudo yum install -y ffmpeg-devel sudo yum install -y libpng-devel sudo yum install -y jasper-devel sudo yum install -y openexr-devel sudo yum install -y libwebp-devel sudo yum -y install libjpeg-turbo-devel sudo yum install -y freeglut-devel mesa-libGL mesa-libGL-devel sudo yum -y install libtiff-devel sudo yum -y install libdc1394-devel sudo yum -y install tbb-devel eigen3-devel sudo yum -y install boost boost-thread boost-devel sudo yum -y install libv4l-devel sudo yum -y install gstreamer-plugins-base-devel 
Download Code To easily follow along this tutorial, please download code by clicking on the button below. It's FREE!

3. Create Python Virtual Environments

We will create Python virtual environments to properly differentiate between different OpenCV versions.
We are going to install virtualenv and virtualenvwrapper modules to create Python virtual environments

 sudo pip3 install virtualenv virtualenvwrapper echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrc echo "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3" >> ~/.bashrc echo "source /usr/bin/virtualenvwrapper.sh" >> ~/.bashrc export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 source /usr/bin/virtualenvwrapper.sh 

Now let’s create the virtual environments and install some modules.

3.1. Python 3.4 Virtual Environment

 mkvirtualenv OpenCV-"$cvVersion"-py3 -p python3 workon OpenCV-"$cvVersion"-py3 pip install cmake pip install numpy scipy matplotlib scikit-image scikit-learn ipython dlib # quit virtual environment deactivate 

3.2. Python 2.7 Virtual Environment

 mkvirtualenv OpenCV-"$cvVersion"-py2 -p python2 workon OpenCV-"$cvVersion"-py2 pip install cmake pip install numpy scipy matplotlib scikit-image scikit-learn ipython dlib # quit virtual environment deactivate 

4. Clone GitHub Repositories

We will next clone opencv and opencv_contrib GitHub repositories.

 git clone https://github.com/opencv/opencv.git cd opencv git checkout 3.4 cd .. git clone https://github.com/opencv/opencv_contrib.git cd opencv_contrib git checkout 3.4 cd .. 

5. Build OpenCV

Now comes the part we have been so eagerly waiting for – building OpenCV. First, we will create build directory.

 cd opencv mkdir build cd build 

Next, we will use CMake and make to build OpenCV.

 cmake3 -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=$cwd/installation/OpenCV-"$cvVersion" \ -D INSTALL_C_EXAMPLES=ON \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D WITH_TBB=ON \ -D WITH_V4L=ON \ -D OPENCV_SKIP_PYTHON_LOADER=ON \ -D OPENCV_GENERATE_PKGCONFIG=ON \ -D OPENCV_PYTHON3_INSTALL_PATH=$HOME/.virtualenvs/OpenCV-$cvVersion-py3/lib/python3.4/site-packages \ -D OPENCV_PYTHON2_INSTALL_PATH=$HOME/.virtualenvs/OpenCV-$cvVersion-py2/lib/python2.7/site-packages \ -D WITH_QT=ON \ -D WITH_OPENGL=ON \ -D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3 \ -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \ -D ENABLE_CXX11=ON \ -D BUILD_EXAMPLES=ON .. make -j$(nproc) make install cd $cwd 

And voila! We have installed OpenCV!

6. How to use OpenCV in C++

There are two ways to use OpenCV in C++, the preferred way is to use CMake, the other one being command line compilation using g++. We will have a look at both ways.

6.1. Using CMakeLists.txt

The basic structure of your CMakeLists.txt will stay the same. Only difference being, that you will have to set OpenCV_DIR as shown below.

 SET(OpenCV_DIR <OpenCV_Home_Dir>/installation/OpenCV-3.4.4/share/OpenCV/) 

Make sure that you replace OpenCV_Home_Dir with correct path. For example, in my case:

 SET(OpenCV_DIR /home/hp/OpenCV_installation/installation/OpenCV-3.4.4/share/OpenCV/) 

Once you have made your CMakeLists.txt, follow the steps given below.

 mkdir build && cd build cmake .. cmake --build . --config Release 

This will generate your executable file in build directory.

6.2. Using g++

To compile a sample file (let’s say my_sample_file.cpp), use the following command.

 g++ `pkg-config --cflags --libs <OpenCV_Home_Dir>/installation/OpenCV-3.4.4/lib/pkgconfig/opencv.pc` my_sample_file.cpp -o my_sample_file 

7. How to use OpenCV in Python

To use the OpenCV version installed using Python script, first we activate the Python Virtual Environment.

For OpenCV-3.4.4 : Python 3

 workon OpenCV-3.4.4-py3 

For OpenCV-3.4.4 : Python 2

 workon OpenCV-3.4.4-py2 

Once you have activated the virtual environment, you can enter Python shell and test OpenCV version.

 ipython import cv2 print(cv2.__version__) 

Hope this script proves to be useful for you :). Stay tuned for more interesting stuff. In case of any queries, feel free to comment below and we will get back to you as soon as possible.



Read Next

VideoRAG: Redefining Long-Context Video Comprehension

VideoRAG: Redefining Long-Context Video Comprehension

Discover VideoRAG, a framework that fuses graph-based reasoning and multi-modal retrieval to enhance LLMs' ability to understand multi-hour videos efficiently.

AI Agent in Action: Automating Desktop Tasks with VLMs

AI Agent in Action: Automating Desktop Tasks with VLMs

Learn how to build AI agent from scratch using Moondream3 and Gemini. It is a generic task based agent free from…

The Ultimate Guide To VLM Evaluation Metrics, Datasets, And Benchmarks

The Ultimate Guide To VLM Evaluation Metrics, Datasets, And Benchmarks

Get a comprehensive overview of VLM Evaluation Metrics, Benchmarks and various datasets for tasks like VQA, OCR and Image Captioning.

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.

Subscribe to receive the download link, receive updates, and be notified of bug fixes

Which email should I send you the download link?

 

Get Started with OpenCV

Subscribe To Receive

We hate SPAM and promise to keep your email address safe.​