OpenCV Setup in Ubuntu

Sardar Manmeeth Singh
Nov 4 · 4 min read

Hello there Readers!

OpenCV is a great library which can be used to work on detecting people and tracking them too!

This blog is about how to install OpenCV in Ubuntu for “Getting started with OpenCV for Human Detection”.

I am going to install OpenCV-3.4.4 in Ubuntu 16.04 LTS version. However, the latest stable build is 4.1.2. You can have a look at their releases on their official website.

Get ready to get good amount of breaks in between the installation, as the OpenCV takes time to get its packages unpacked during the process. Let’s get started….

Step-1: Choose your version of OpenCV. I am choosing OpenCV-3.4.4, you can choose further versions as well and follow the steps in this guide.

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"

Step-2: Store the current working directory in cwd variable, to avoid long path inclusions when we are working on the installation. We are also going to refer to this directory as OpenCV_Home_Dir throughout this blog.

# Save current working directory

cwd=$(pwd)

Update the Packages. We have to update the packages that we already have in our existing Ubuntu environment.

sudo apt -y update

sudo apt -y upgrade

Step-3: Install the 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 libjasper-dev libpng12-dev

sudo apt -y install libtiff5-dev

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 libgstreamer0.10-dev libgstreamer-plugins-base0.10-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

Step-4: Install the Python Libraries

You need to install python3, if you do (or) do not have Python2 version. Ubuntu comes with Python 2.7 (approx.) pre-installed. Following are the commands:

sudo apt -y install python3-dev python3-pip python3-venv

sudo -H pip3 install -U pip numpy

sudo apt -y install python3-testresources

Step-5: Create a virtual environment.

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

######################################

Step-6: Downloading “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-7: Compile and install OpenCV with contrib modules.

First we navigate to the build directory where we have opencv.

cd opencv

mkdir build

cd build

Step-8: Compilation and Installation of OpenCV.

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 ..

(Please enter the above lines of code as is, do not ignore the ‘-D’ part in each line of code).

make -j4

make install

Now that we have installed OpenCV on Ubuntu, let’s use it in Python.

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

Step-9: For OpenCV-3.4.4 : Python 3, activate the Python Virtual Environment using the following command in a new terminal:

workoncv-3.4.4

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

ipython

import cv2

print(cv2.__version__)

Congratulations! You have successfully installed OpenCV, i’s libraries and also created your Python Virtual Environment and configured it.

Hope this blog was helpful to get OpenCV Setup in Ubuntu. If you are interested in checking out the interesting story on human detection with the OpenCV that you have just setup, please visit here.

See you in my next one!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade