Installing OpenCV 3 in Ubuntu 16.04

An image processing is a way to get information or data from image analytical or any process. Out there a lot of useful tools to do image processing the most popular tool for image processing as long far I know are Matlab (paid or you can get a discount if you are a student) and OpenCV. Based on their official website for OpenCV, OpenCV (Open Source Computer Vision Library) is released under a BSD license and hence it’s free for both academic and commercial use.  So far you still get the point? If yes let’s rocking our system with OpenCV. This section will explain step by step with a handful and light description about what we will do.

Note: Follow along with the instructions and perform the steps — by the end of this article you’ll have OpenCV installed on your system. From there, just ignore the Python bindings and proceed as usual.

For your info Ubuntu is installed with two version of Python by default there are Python 2.x and Python 3.x

  • Python 2.x (used by default when you type python in your terminal).
  • Python 3.x (can be accessed via the python3  command)

So which better version of Python I muse use? Well, I’ll not start to argue about which better Python 2 or Python 3? It depends on your ecosystem and for note Python 3 is future, but in my ecosystem I use Python 2 and as far as OpenCV goes, OpenCV 3 doesn’t care which version of Python you’re using: the bindings will work just the same.

 

STEP 1: Install OpenCV dependencies in Ubuntu 16.04

All step that I described below is used the terminal as the interface so you can just press Ctrl+Alt+T to open your terminal.

First, we will make sure that our system is fresh and update our pre-installed libraries.

sudo apt-get update
sudo apt-get upgrade

After the update and upgrade was complete, we will install some developers tools:

$ sudo aptget install buildessential cmake pkgconfig

  • The pkgconfig  package is (very likely) already installed on your system, but be sure to include it in the above aptget
  • The cmake  program is used to automatically configure our OpenCV build.

 

OpenCV is an image processing and computer vision library. Therefore, OpenCV needs to be able to load various image file formats from disk such as JPEG, PNG, TIFF, etc. So we need to install libraries to support OpenCV I/O images from the system

$ sudo aptget install libjpeg8dev libtiff5dev libjasperdev libpng12dev

To make OpenCV work with video  install packages used to process video streams and access frames from cameras:

$ sudo aptget install libavcodecdev libavformatdev libswscaledev libv4ldev
$ sudo aptget install libxvidcoredev libx264dev
OpenCV process like imshow (to figure out result of image) we need GUI. The name of the module that handles OpenCV GUI operations is highgui . The highgui  module relies on the GTK library we need to install GTK
$ sudo aptget install libgtk3dev
We install libraries that are used to optimize various functionalities inside OpenCV, such as matrix operations:
$ sudo aptget install libatlasbasedev gfortran
Wrap up all of depedencies in Step #1 by installing the Python development headers and libraries for both Python 2.7 and Python 3.5
$ sudo aptget install python2.7dev python3.5dev
Note: If you do not install the Python development headers and library, you’ll get issues during Step #4 where we run cmake  to configure our build. 

Step #2: Download the OpenCV source

Ok, let’s we download OpenCV. The latest version I used is OpenCV 3.3.1 which we download OpenCV .zip and unarchived

$ cd ~
$ wget O opencv.zip https://github.com/Itseez/opencv/archive/3.3.1.zip
$ unzip opencv.zip
To check the latest OpenCV version you can go and check their release version in here
we also need the opencv_contrib repository as well:
$ wget O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.3.1.zip
$ unzip opencv_contrib.zip
Note: the version of OpenCV source and opencv_contrib must same. If the versions numbers do not matchup, you could very easily run into compile time errors (or worse, runtime errors that are near impossible to debug).

Step #3: Setup your Python environment — Python 2.7 or Python 3

Install pip as python package manager. Pip allow us to install python package we need easily.

$ sudo apt-get install python-pip && pip install –upgrade pip

We will use virtualenv and virtualenvwrapper. These Python packages allow you to create separate, independent Python environments for eachproject that you are working on. Bot ot the is a standard  practice of python community to laverage their environement without distrubing other env.

 

$ sudo pip install virtualenv virtualenvwrapper
$ sudo rm -rf ~/.cache/pip

Then we need to add some line to .bashrc file:

$ cd ~
$ nano .bashrc

add the following lines to the bottom of the content of the file

# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

Then save the file, go back to terminal to refresh .bashrc with command:

$ source ~/.bashrc

Now that we have installed virtualenv  and virtualenvwrapper , the next step is to actually create the Python virtual environment — we do this using the mkvirtualenv  command.

 

Next, let’s create a virtual environment for OpenCV, called cv:

For python 2:

$ mkvirtualenv cv p python2

 

For  Python 3:

$ mkvirtualenv cv p python3

 

Verifying that you are in the “cv” virtual environment

$ workon cv

 If you see the text (cv)  preceding your prompt, then you are in the cv  virtual environment:

 

Install NumPy into your Python virtual environment

Make sure you’re still on cv virtual environment like I described before. NumPy is a Python package used for numerical processing.

$ pip install numpy

 

Step #4: Configuring and compiling OpenCV on Ubuntu 16.04

Until this step, if you follow along the step i hope you will successful compiling OpenCV, if you find an error in compiling just make sure you’re not missing the step.  We are now ready to compile and OpenCV!

 

Make sure we are in cv environment

$ workon cv

 

Setup configure build using cmake

$ cd ~/opencv3.1.0/
$ mkdir build
$ cd build
$ cmake D CMAKE_BUILD_TYPE=RELEASE \
    D CMAKE_INSTALL_PREFIX=/usr/local \
    D INSTALL_PYTHON_EXAMPLES=ON \
    D INSTALL_C_EXAMPLES=OFF \
    D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib3.3.1/modules \
    D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python \
    D BUILD_EXAMPLES=ON ..
The above commands change directory to ~/opencv3.3.1 , which if you have been following this tutorial is where you downloaded and unarchived the .zip  files.
Finally, we execute cmake  to configure our build.

And compile OpenCV while you are in in the build folder:

$ make j4

The j  switch controls the number of processes to be used when compiling OpenCV. You can check the number of process you can use by checking with this command nproc

Just wait the process until 100% it will take some time you can leave to get some coffee or maybe plaing NBA in XBOX at one play time.

Last step: install openCV (remember that you still in the build folder)

$ sudo make install
$ sudo ldconfig

 

Step #5: Finish your OpenCV install

 

For Python 2.7:

After running sudo make install , your Python 2.7 bindings for OpenCV 3 should now be located in /usr/local/lib/python2.7/sitepackages/ . You can verify this using the ls  command:

$ ls l /usr/local/lib/python2.7/sitepackages/
You will find output like this
The path i used is in site-packages, if your system not found in site-packages you can assume in dist-packages
The final step is to sym-link our OpenCV cv2.so  bindings into our cv  virtual environment.
$ cd ~/.virtualenvs/cv/lib/python2.7/sitepackages/
$ ln s /usr/local/lib/python2.7/sitepackages/cv2.so cv2.so
Change the python version with version of Python you used for development.

Step #6: Testing your OpenCV install

You now have OpenCV 3 installed on your Ubuntu 16.04 system!

To verify that your installation is working:

  1. Open up a new terminal.
  2. Execute the workon  command to access the cv  Python virtual environment.
  3. Attempt to import the Python + OpenCV bindings.

Type like the image below and you will see the OpenCV installed on your cv environment

 

 

 

 

  • Share post

Leave a Reply

Your email address will not be published. Required fields are marked *