• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Learn OpenCV

OpenCV, PyTorch, Keras, Tensorflow examples and tutorials

  • Home
  • Getting Started
    • Installation
    • PyTorch
    • Keras & Tensorflow
    • Resource Guide
  • Courses
    • Opencv Courses
    • CV4Faces (Old)
  • Resources
  • AI Consulting
  • About

Install OpenCV 4 on Ubuntu 18.04 (C++ and Python)

Avatar Vishwesh Shrimali
September 19, 2018 13 Comments
Install OpenCV 4

September 19, 2018 By 13 Comments

Install OpenCV 4 on Ubuntu

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-4.0 (C++ and Python 3.6) on Ubuntu 18.04. 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.

Looking for installation script for Ubuntu 16.04? Have a look at this blog.

Install OpenCV on Windows for CPP

Step 0: Select OpenCV version to install

echo "OpenCV installation by learnOpenCV.com"
# Define OpenCV Version to install
cvVersion="master"
We are also going to clean build directories and create installation directory.
# Clean build directories
rm -rf opencv/build
rm -rf opencv_contrib/build
# Create directory for installation
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 blog.

# Save current working directory
cwd=$(pwd)

Step 1: Update Packages

sudo apt -y update
sudo apt -y upgrade
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 blog.

Step 2: Install OS Libraries

sudo apt -y remove x264 libx264-dev

## Install dependencies
sudo apt -y install build-essential checkinstall cmake pkg-config yasm
sudo apt -y install git gfortran
sudo apt -y install libjpeg8-dev libpng-dev

sudo apt -y install software-properties-common
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt -y update

sudo apt -y install libjasper1
sudo apt -y install libtiff-dev

sudo apt -y install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev
sudo apt -y install libxine2-dev libv4l-dev
cd /usr/include/linux
sudo ln -s -f ../libv4l1-videodev.h videodev.h
cd "$cwd"

sudo apt -y install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt -y install libgtk2.0-dev libtbb-dev qt5-default
sudo apt -y install libatlas-base-dev
sudo apt -y install libfaac-dev libmp3lame-dev libtheora-dev
sudo apt -y install libvorbis-dev libxvidcore-dev
sudo apt -y install libopencore-amrnb-dev libopencore-amrwb-dev
sudo apt -y install libavresample-dev
sudo apt -y install x264 v4l-utils

# Optional dependencies
sudo apt -y install libprotobuf-dev protobuf-compiler
sudo apt -y install libgoogle-glog-dev libgflags-dev
sudo apt -y install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen
Looking for installation script for OpenCV 3.4.4 on Ubuntu 18.04? Have a look at this blog.

Step 3: Install Python Libraries

sudo apt -y install python3-dev python3-pip
sudo -H pip3 install -U pip numpy
sudo apt -y install python3-testresources
We are also going to install virtualenv and virtualenvwrapper modules to create Python virtual environments.
cd $cwd
############ For Python 3 ############
# create virtual environment
python3 -m venv OpenCV-"$cvVersion"-py3
echo "# Virtual Environment Wrapper" >> ~/.bashrc
echo "alias workoncv-$cvVersion=\"source $cwd/OpenCV-$cvVersion-py3/bin/activate\"" >> ~/.bashrc
source "$cwd"/OpenCV-"$cvVersion"-py3/bin/activate

# now install python libraries within this virtual environment
pip install wheel numpy scipy matplotlib scikit-image scikit-learn ipython dlib

# quit virtual environment
deactivate
[/bash]
<div class="download-code">

<strong> Download Installation Script</strong>
To easily follow along this tutorial, please download installation script by clicking on the button below. It's FREE!
<div style="text-align: center;"><a class="button small loblue" style="text-decoration: none;" href="https://bigvisionllc.lpages.co/leadbox/143948b73f72a2%3A173c9390c346dc/5649050225344512/" target="_blank" rel="noopener">Download Installation Script </a></div>
</div>
<h2>Step 4: Download opencv and opencv_contrib</h2>

git clone https://github.com/opencv/opencv.git
cd opencv
git checkout $cvVersion
cd ..

git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout $cvVersion
cd ..
Download Code To easily follow along this tutorial, please download code by clicking on the button below. It’s FREE!

Download Code

Step 4: Download opencv and opencv_contrib

git clone https://github.com/opencv/opencv.git
cd opencv
git checkout $cvVersion
cd ..

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

Step 5: Compile and install OpenCV with contrib modules

First we navigate to the build directory

cd opencv
mkdir build
cd build

Next, we start the compilation and installation process.

cmake -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_PYTHON3_INSTALL_PATH=$cwd/OpenCV-$cvVersion-py3/lib/python3.5/site-packages \
-D WITH_QT=ON \
-D WITH_OPENGL=ON \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
For system wide installation of OpenCV, change CMAKE_INSTALL_PREFIX to CMAKE_INSTALL_PREFIX=/usr/local \.
make -j4
make install

2. How to use OpenCV in C++

Using CMakeLists.txt The basic structure of your CMakeLists.txt will be as follows:
cmake_minimum_required(VERSION 3.1)
# Enable C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

The basic stavructure of your CMakeLists.txt will be as follows:

SET(OpenCV_DIR /installation/OpenCV-master/lib/cmake/opencv4)

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-master/lib/cmake/opencv4)

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.

3. How to use OpenCV in Python

To use the OpenCV version installed using Python script, first we activate the correct Python Virtual Environment. For OpenCV-4 : Python 3

workon OpenCV-master-py3

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.

Subscribe & Download Code

If you liked this article and would like to download code (C++ and Python) and example images used in this post, please subscribe to our newsletter. You will also receive a free Computer Vision Resource Guide. In our newsletter, we share OpenCV tutorials and examples written in C++/Python, and Computer Vision and Machine Learning algorithms and news. Subscribe Now
Tags: Install OpenCV4 ubuntu

Filed Under: Install, OpenCV 4

About

AvatarI am an entrepreneur with a love for Computer Vision and Machine Learning with a dozen years of experience (and a Ph.D.) in the field.

In 2007, right after finishing my Ph.D., I co-founded TAAZ Inc. with my advisor Dr. David Kriegman and Kevin Barnes. The scalability, and robustness of our computer vision and machine learning algorithms have been put to rigorous test by more than 100M users who have tried our products. Read More…

Getting Started

  • Installation
  • PyTorch
  • Keras & Tensorflow
  • Resource Guide

Resources

Download Code (C++ / Python)

ENROLL IN OFFICIAL OPENCV COURSES

I've partnered with OpenCV.org to bring you official courses in Computer Vision, Machine Learning, and AI.
Learn More

Recent Posts

  • How to use OpenCV DNN Module with Nvidia GPU on Windows
  • How to use OpenCV DNN Module with NVIDIA GPUs
  • Code OpenCV in Visual Studio
  • Install OpenCV on Windows – C++ / Python
  • Face Recognition with ArcFace

Disclaimer

All views expressed on this site are my own and do not represent the opinions of OpenCV.org or any entity whatsoever with which I have been, am now, or will be affiliated.

GETTING STARTED

  • Installation
  • PyTorch
  • Keras & Tensorflow
  • Resource Guide

COURSES

  • Opencv Courses
  • CV4Faces (Old)

COPYRIGHT © 2021 - BIG VISION LLC

Privacy Policy | Terms & Conditions

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it. Privacy policyAccept