Analytics Development

How to install OpenCV Ubuntu

OpenCV is an open source computer vision library available under the BSD license. So it is free for academic and commercial use. The library is written in C and C++. It runs on Linux, Windows, Mac OS, iOS, and Android. It has C, C++, Java, MATLAB and Python interfaces. OpenCV has more than 2,500 optimized algorithms for real-time computer vision.

The aim of the OpenCV community is to create a computer vision infrastructure that allows developers to create complex applications with relative ease. The library is designed for computational efficiency for real-time applications. So it has a wide array of uses in face recognition, gesture recognition, medical imaging, human-computer interaction, motion tracking, security monitoring, robotics, camera controls and more.

Besides the computer vision components, OpenCV also has support for general-purpose machine learning. Machine learning (ML) is an important technology for computer vision problems. So the ML library makes OpenCV more attractive to computer vision developers.

Computer Vision and OpenCV

Computer vision was created with the goal to replicate human vision capabilities. It uses algorithms to transform captured images into data and makes it easier to comprehend real-world vision problems.

In the case of human vision, our eyes work as input devices. Then our brains divide the image streams into multiple channels for processing. Besides visual data, the human brain also takes into account other sensory data and uses it to understand spacial depth. It gives human brains the ability to understand three-dimensional space.

When we collect data through cameras we get a two-dimensional view of the world. Computer vision algorithms take the two-dimensional images and use mathematical properties to figure out the three-dimensional representations. It is an extremely difficult problem to solve.

Also, computer vision often uses other contextual information to overcome the limitations of two-dimensional images. It takes into account information like color, brightness or contrast. For example, if an object recognition algorithm is looking for a wood table, it can safely eliminate any non-wood related colors from the input images. Also, computer vision algorithms eliminate noise in the input data.

The OpenCV library is designed to make the implementation of computer vision algorithms easier. It handles computational complexity so developers can concentrate on high-level tasks.

History of OpenCV

In 1999, OpenCV started at Intel as an initiative to advance CPU-Intensive applications. Gary Bradski who was working at Intel at the time noticed that students at MIT Media Lab were sharing libraries to get a head start in computer vision applications. This inspired the idea for building a computer vision infrastructure that can be used easily.

From Intel, the OpenCV project moved to Willow Garage, a robotics research lab and technology incubator based in Menlo Park, California. Currently, the OpenCV open source project is maintained by Itseez, a customized computer vision software development and consultancy company.

OpenCV version 1.0 was released in 2006. The next major version 2.0.0 came in 2009. The current major version 3.0.0 was released in 2015. The most recent version to-date is OpenCV 3.3.0.

Using OpenCV

The library has gained popularity among scientists and academics. It is often used as a teaching tool for computer vision. But OpenCV is robust enough to support real-world problems.

You can use OpenCV for non-commercial and commercial products. It is used by industry giants like Google, Yahoo, Microsoft, Intel, IBM, Sony, Honda, and Toyota. Research institutes in leading universities like MIT, CMU, Stanford, and Cambridge provide support for the library. The OpenCV Yahoo Group has 50,000 members worldwide.

Installing OpenCV

Warning: The whole installation is around 10 GB.

Step 1: Install OpenCV Dependencies

Use the following commands to update your Ubuntu libraries:

$ sudo apt-get update
$ sudo apt-get upgrade

Mkdir a working dir “OpenCV”:

$ mkdir OpenCV
$ cd OpenCV

Install OpenCV development dependencies:

$ sudo apt-get install libopencv-dev

Install build tool dependencies:

$ sudo apt-get install build-essential checkinstall cmake pkg-config

Install Image I/O dependencies:

$ sudo apt-get install libtiff5-dev libjpeg-dev libjasper-dev libpng12-dev libwebp-dev libopenexr-dev libgdal-dev

Install Video I/O dependencies:

$ sudo apt-get install libavcodec-dev libavformat-dev libmp3lame-dev
$ sudo apt-get install libswscale-dev libdc1394-22-dev libxine2-dev
$ sudo apt-get install libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev
$ sudo apt-get install libv4l-dev v4l-utils libfaac-dev libopencore-amrnb-dev
$ sudo apt-get install libopencore-amrwb-dev libtheora-dev libvorbis-dev
$ sudo apt-get install libxvidcore-dev libx264-dev x264 yasm

Install parallelism and linear algebra library dependencies:

$ sudo apt-get install libtbb-dev libeigen3-dev

Install GUI dependencies:

$ sudo apt-get install libqt4-dev libgtk2.0-dev qt5-default

Install Python dependencies:

$ sudo apt-get install python-dev python-tk python-numpy python3-dev python3-tk python3-numpy python-matplotlib
$ sudo apt-get install python-opencv

Step 2: Configure and Compile OpenCV on Ubuntu

Download the OpenCV code and unzip it into a directory and set up environment for compilation :

$ wget -O OpenCV-3.3.0.zip https://sourceforge.net/projects/opencvlibrary/files/opencv-unix/3.3.0/opencv-3.3.0.zip/download
$ unzip OpenCV-3.3.0.zip
$ cd opencv-3.3.0
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
	-D CMAKE_INSTALL_PREFIX=/usr/local \
	-D WITH_TBB=ON \
	-D BUILD_NEW_PYTHON_SUPPORT=ON \
	-D WITH_V4L=ON \
  	-D BUILD_opencv_java=ON \
	-D INSTALL_C_EXAMPLES=ON \
	-D INSTALL_PYTHON_EXAMPLES=ON \
	-D BUILD_DOCS=ON \
	-D BUILD_EXAMPLES=ON \
	-D WITH_QT=ON \
	-D WITH_OPENGL=ON \
	-D WITH_EIGEN=ON ..

Start compiling:

$ make -j4

Successful compilation output should look like this:

Scanning dependencies of target example_3calibration
[ 99%] Building CXX object samples/cpp/CMakeFiles/example_3calibration.dir/3calibration.cpp.o
[ 99%] Built target example_fitellipse
Scanning dependencies of target example_segment_objects
[ 99%] Building CXX object samples/cpp/CMakeFiles/example_segment_objects.dir/segment_objects.cpp.o
[ 99%] Linking CXX executable ../../bin/cpp-example-starter_imagelist
[ 99%] Linking CXX executable ../../bin/cpp-example-segment_objects
[ 99%] Built target example_starter_imagelist
Scanning dependencies of target tutorial_pointPolygonTest_demo
[ 99%] Building CXX object samples/cpp/CMakeFiles/tutorial_pointPolygonTest_demo.dir/tutorial_code/ShapeDescriptors/pointPolygonTest_demo.cpp.o
[ 99%] Linking CXX executable ../../bin/cpp-tutorial-Morphology_3
[ 99%] Built target example_segment_objects
Scanning dependencies of target example_fback
[ 99%] Building CXX object samples/cpp/CMakeFiles/example_fback.dir/fback.cpp.o
[ 99%] Built target tutorial_Morphology_3
Scanning dependencies of target tutorial_calcHist_Demo
[ 99%] Building CXX object samples/cpp/CMakeFiles/tutorial_calcHist_Demo.dir/tutorial_code/Histograms_Matching/calcHist_Demo.cpp.o
[ 99%] Linking CXX executable ../../bin/cpp-tutorial-pointPolygonTest_demo
[ 99%] Linking CXX executable ../../bin/cpp-example-fback
[ 99%] Built target tutorial_pointPolygonTest_demo
Scanning dependencies of target example_em
[ 99%] Building CXX object samples/cpp/CMakeFiles/example_em.dir/em.cpp.o
[ 99%] Linking CXX executable ../../bin/cpp-example-3calibration
[ 99%] Linking CXX executable ../../bin/cpp-tutorial-calcHist_Demo
[ 99%] Built target example_fback
Scanning dependencies of target example_filestorage_base64
[ 99%] Building CXX object samples/cpp/CMakeFiles/example_filestorage_base64.dir/filestorage_base64.cpp.o
[ 99%] Built target example_3calibration
[ 99%] Built target tutorial_calcHist_Demo
[ 99%] Linking CXX executable ../../bin/cpp-example-em
[100%] Linking CXX executable ../../bin/cpp-example-filestorage_base64
[100%] Built target example_em
[100%] Built target example_filestorage_base64
$

Step 3: Install the OpenCV library
Run the following commands:

$ sudo make install
$ echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf
$ sudo ldconfig

Step 4: Verify Installation

You verify installation by importing cv2 and using “cv2.__version__” command on the Python commandline:

$ python3
Python 3.5.2 (default, Sep 14 2017, 22:51:06) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2 
>>> cv2.__version__
'3.3.0'
>>> 

Next Steps

OpenCV Face Recognition

References:

About the author

Admin

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.

3 Comments

  • Thanks for these instruction! However, how can I adjust these command to install this inside a conda environment and create bindings to the python version from the conda environment?

    • Hi and thanks for comment. Haven’t played around with conda so hopefully someone might have and would share more insight on it.

      You can ask this on the github page “https://github.com/opencv/opencv” for faster response

  • Will this support ffmpeg. If I would like to have ffmpeg support, what should I modify in your instructions. Planning to install it on a python 3.6 virtual environment (linux mint 18.1).

Leave a Comment