OpenCV is one of the most popular Computer Vision libraries amongst the developers. It is widely used for developing real time Computer Vision applications and can be easily integrated with Deep Learning algorithms for developing complex applications.
Back in my college days, I started working on Computer Vision via MATLAB, when my mentor suggested me to move on to OpenCV for better results. I took his advice and started looking for the posts instructing how to install OpenCV on the system. But to my ill luck, there was not much support available on the internet. Consequently, it took me quite some days before I was finally able to successfully complete the installation.To avoid such hassles for others going forward, I decided to write this blog where I can share all the steps required to sucessfully complete the OpenCV installation. .
Note: The following instructions are written to install OpenCV on Ubuntu. Please make sure that you have latest version of Ubuntu in your system. The same can be downloaded from https://www.ubuntu.com/download/desktop. If you are currently using Windows/MAC OS, then I would recommend you to dual boot your systems with Ubuntu as it will enable you to take the advantages of both the operating systems as and when required.
Without much ado, let us get started with the installation steps.
All the steps given below are to be executed via terminal and therefore launch your terminal from your Ubuntu and get ready for the installation.
Step1: Checking for correct version of Python language
Make sure that you are using Python3 as your development language because Python2 might loose the community support in the near future.
Step2: Installing dependencies for OpenCV
Before installing any dependencies let’s update the system.
Once you are done with the updates, let us move on and install some pre-requisite libararies for OpenCV. Since OpenCV is meant to work on different formats of videos and images, it is vital to install all the relevant image and video packages. The same can be done by executing the commands as given below.
Next we are going to install GTK library so that OpenCVs GUI operations can be carried out smoothly in the environment
sudo apt-get install libgtk-3-dev
We are left with only one more step before we can install OpenCV in the system.
sudo apt-get install python3-dev
The above statement makes sure that all Python3 headers and libraries are correctly installed.
Step3: Download OpenCV
OpenCV can be downloaded from their official page,https://opencv.org/releases.html
As of now the latest stable version of OpenCV is 3.4.3.
To make sure that you are in the Home folder of your system, issue the command as given below.
To download the opencv package issue the command as given below:
wget -0 opencv.zip https://github.com/opencv/opencv/archive/3.4.3.zip
To check for the latest version of opencv module please click here.
There are some patented modules in OpenCV that can’t be used for commercial use. Such modules are kept separately in opencv_contrib module. The module has to be downloaded separately and can be done as per the following commands.
Make sure that the version of opencv module and opencv_contrib module are matching with each other so to avoid any errors later.
To check for the latest versions of contrib modules please click here.
Next the modules are unzipped from their respective folders using the following the command.
Step4: Configuring environment for installation
Next step is to download and install the package manager for Python, named as pip. To do the needfull, execute the commands as given below:
While working on different projects, we might need different versions of OpenCV or Python or for that matter any other supporting library. To maintain such an isolation it is advised to use virtual environments. For analogy, it can be considered as a virtual box being created into Windows. While virtual box has limited resources available, virtual environment in Ubuntu has the access to all the available resources.
To ensure the correct installation of the same, follow the commands given below:
sudo pip install virtualenv virtualenvwrapper
Once the installation is done, ~/.bashrc file is to be edited to set up the environment variables. For same, open the ~/.bashrc file in Text Editor of Ubuntu and add the following lines at the end of the file and save it.
Now, we are all setup to create a virtual environment in which we will install our opencv and other libraries later on.
mkvirtualenv cv3.4.3 -p python3
Above command creates a Python3 virtual environment having name cv3.4.3. The name of the environment can be anything as per your choice but it is preferred to keep it short and simple. Also I have added the version of opencv in the name so as to avoid any confusions later if multiple versions of opencv are installed in different virtual environments.
To make sure that everything is upto the mark, please make sure that the folder has been created by listing down the contents of folder .virtualenvs in home folder of your Ubuntu.
As can be seen above, I have 4 diferent virtual environments in my system, namely, ml, cv3.4, cv3.3 and cv3.4.3.
Going forward, all the installations are to be done in this virtual environment and so make sure that you have chosen the correct environment before going ahead.
To switch to the virtual ensironment use the command below:
As soon as you execute the command above, your terminal will look as shown below:
Step5: Compile Open CV
Finally let us compile Opencv module along with opencv contrib module.
Make sure that the name of the virtual environment is same as created by you above.
Sit back a while and let the processing get finished. Once done check for “Python3” in the output. If somehow you are not able to find Python3 or find Python2 instead, then I would recommend you to start over with the whole process again.
Make sure the number 4 in command above is representing the number of cores in the system. Please check the number of cores in your system by writing ‘nproc’ .
Its interval time now!!!
Yes you heard me correctly... Make some coffee and have some snacks because this process will take atleast 30 mins to complete.
Step 6: Install OpenCV
To finally install opencv execute the commands given below:
sudo make install
Step 7: Finishing the OpenCV Installation
Last few commands to be executed are:
sudo mv cv2.cpython-36m-x86_64-linux-gnu.so cv2.so
ln -s /usr/local/lib/python3.6/site-packages/cv2.so cv2.so
Step 8: Verifying opencv installation
To verify that OpenCV has installed properly, please type the following commands:
If Opencv has been installed properly, you will be able to see the ‘3.4.3’ at the
output of the above command.
This blog provides step by step instructions to install OpenCV with Python on
Ubuntu. Though there are much simpler ways available to do the same, this
process ensures that all the dependencies and OpenCV modules are installed in
one go so that there are no problems in the future.
If you enjoyed reading this tutorial, please recommned the same to others .