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 apt–get install build–essential cmake pkg–config
- The pkg–config package is (very likely) already installed on your system, but be sure to include it in the above apt–get
- 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 apt–get install libjpeg8–dev libtiff5–dev libjasper–dev libpng12–dev
To make OpenCV work with video install packages used to process video streams and access frames from cameras:
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
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
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
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
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/python–2.7/site–packages/ . You can verify this using the ls command:
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:
- Open up a new terminal.
- Execute the workon command to access the cv Python virtual environment.
- Attempt to import the Python + OpenCV bindings.
Type like the image below and you will see the OpenCV installed on your cv environment