Hello, Readers! I am very pumped up by the response my previous articles on OpenCV got. Thanks a lot for the support. I will continue to share more contents on the techs I learn every day. Chien asked me how to install OpenCV along with an IDE. First of all, I searched for relevant tutorials available on the internet.
I found out that there are no updated guides to follow. So I thought I should provide you guys with one. So here I am going to show you how to install OpenCV with ease.
Ready? Let’s get started.
Requirements – Things we will need before we install OpenCV
Chien asked me how to set up OpenCV with Python. Python is an excellent choice to use OpenCV library. I chose Python to start my learning journey of OpenCV because it is straightforward to understand and follow. So I am going to show you how you can set up and install the OpenCV library and use it with Python. I prefer using Python 2.7.
You do not need to install Python additionally if you have Ubuntu 17.04 installed. You can confirm if it is already installed by opening up your terminal. Now type the following commands.
As you can see in the picture above Python is already installed. If you don’t see a similar output, you can get Python from here: Download Python 2.7
Also, I have got Ubuntu 17.04 installed. It is the latest version as in the time of writing this tutorial. You can follow this guide for other Ubuntu version too like Ubuntu 16.04 or Ubuntu 16.10.
You can download the latest version of Ubuntu from here: Download Ubuntu 17.04
For writing Python programs, I usually use Sublime Text. It is very lightweight and straightforward to use. It is suited to write small scripts to get work done in little time. But it can’t match the power of an Integrated Development Environment(IDE).
I am a fan of IDE’s offered by JetBrains. They are just so powerful. Using an IDE cuts the development time. I use PyCharm for coding applications in Python. I will show you how easy it is to set up. Also, I will configure it to use the OpenCV library.
Download the latest version of PyCharm from here: Download PyCharm. You can choose between the Professional and Community Editions.
Download OpenCV with extra modules
I found it complicated when I tried to install OpenCV on my Ubuntu 16.04. I failed and met some errors the first few times I tried. I was wondering why it is too hard to install OpenCV. My friend Atlantis guided me and suggested me some helpful tutorials. You can find those resources below this tutorial.
So now let us download the OpenCV source files. OpenCV modules are divided into two separate repositories.
The OpenCV repo contains the non-patented modules that you can use even for commercial purposes.
Besides the free modules, some extra modules are present in the ‘OpenCV_contrib’ repo. You will have to pay to use the patented modules if you use it for commercial purpose. Using these modules for educational purposes is free. Also, it contains some modules that are currently in development.
Let us now download the source files from both these repositories.
Download OpenCV here: OpenCV
Download OpenCV extra modules here: OpenCV_contrib
Optional: Make sure you download these two zip files into the ‘home’ directory. So that, you will find it easier to follow the commands in this tutorial. Otherwise, make sure you enter the right locations of these source folders in the commands you will see later in this tutorial.
So now that we have all the files and Softwares that we needed let us download and install the dependencies before we install OpenCV.
Installing dependencies before we install OpenCV
OpenCV needs additional libraries to perform basic actions like loading images of the different formats from the disk.
We also need to install the ‘cmake‘ program. It configures the OpenCV build. We are also going to need the libraries that enable OpenCV to work with video streams from the webcam of your computer.
In addition, we need some libraries that allow basic GUI operations and some other libraries for optimization of OpenCV functions.
Here are the commands that will install all the above-described libraries.
sudo apt-get update sudo apt-get upgrade sudo apt-get install build-essential cmake pkg-config sudo apt-get install libjpeg8-dev libtiff5-dev libpng-dev echo "deb http://us.archive.ubuntu.com/ubuntu/ yakkety universe" | sudo tee -a /etc/apt/sources.list sudo apt-get update sudo apt-get install libjasper-dev sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev sudo apt-get install libxvidcore-dev libx264-dev sudo apt-get install libgtk-3-dev sudo apt-get install libatlas-base-dev gfortran
Some libraries are no longer available for Ubuntu from the 17.04 version. So, I have added those libraries from the Ubuntu 16.10 universe.
These libraries should do all the necessary functions that OpenCV requires. These worked for me. In case, you run into any error at this point, drop a comment. We will fix it together.
Installing Python headers
So now, that we have our dependencies installed, there is one more thing we must do. The cmake command needs to know what version of Python we have on our computer and where is the Python interpreter. For that, we need to install the appropriate Python development header. Run this command.
sudo apt-get install python2.7-dev python3.5-dev
This will download the Python’s development headers and static library. So now, we are done with installing the dependencies. Just one more step, before we install OpenCV. We need to configure and setup our Python environment. Let me show you how it’s done.
Set up a virtual environment in Python
For creating separate working environments for each project, we need to install the virtualenv and virtualenvwrapper packages. We can install these Python packages using the Python’s package manager – pip.
Let us now quickly install pip, virtualenv, and virualenvwrapper. We will also need to configure the environment variable before we could use virtualenv through the terminal. Run these commands.
sudo apt-get install python-pip sudo pip install virtualenv virtualenvwrapper sudo rm -rf ~/get-pip.py ~/.cache/pip export WORKON_HOME=$HOME/.virtualenvs source /usr/local/bin/virtualenvwrapper.sh echo -e "\n# virtualenv and virtualenvwrapper" >> ~/.bashrc echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrc echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc source ~/.bashrc
The last command should save our changes that we made to the environment variables. So, at this point, our virtualenv package has been installed. Now let us create a virtual environment. After that, we will begin to compile and install OpenCV.
Creating a virtual environment
We will now create a virtual environment inside of which we are going to work with OpenCV. A virtual environment is specific to the version of Python we are going to work with. So you will have to choose between Python version 2.7 and Python version 3 to make a virtual environment.
Here I have given my virtual environment a name – ‘opencv.’ You can choose any name. Let me show you the commands.
Creating a Python version 2.7 virtual environment
mkvirtualenv opencv -p python2
Creating a Python version 3 virtual environment
mkvirtualenv opencv -p python3
Once you have successfully created your virtual environment, you should see something like this.
Note that we are now in the newly created ‘opencv’ virtual environment. How do we know that? See below.
You can confirm if you are inside a virtual environment or not. If you don’t see your virtual environment’s name on the left, run this command.
Also, we will need to install the numpy package inside our virtual environment.
pip install numpy
After we have installed numpy in the OpenCV virtual environment, we can now proceed to install OpenCV.
Building, Configuring and Installing OpenCV
Before you start, make sure that you are inside the ‘opencv’ virtual environment that we created in the previous step. Therefore, if you are not, just type the command ‘ workon opencv’.
Now go to the home directory, where you have downloaded the OpenCV and OpenCV_contrib zip files in earlier in this Step
Now unzip those files. I have renamed those two folders for simplicity, as you may see here.
Once you unzip those files, you should see different sub-directories.
Let us now configure the build using cmake. Run the following commands.
cd ~ cd opencv-3.2.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=/home/syed/opencv_contrib-3.2.0/modules \ -D PYTHON_EXECUTABLE=/home/syed/.virtualenvs/opencv/bin/python \ -D BUILD_EXAMPLES=ON ..
If you an output like this, then you probably have executed the right commands. But do confirm the values for your system, for the rows highlighted in the picture.
Confirm if the interpreter of your virtual environment’s Python is pointing to the correct path. Also, make sure that the cmake correctly read the numpy package inside your virtual environment.
Compiling and Installing
So now after the build is correctly configured, let us compile the source. Use this command.
-j switch allows you specify the number of processors or cores you want to use for compiling. I have a quad core processor, so I specified it as -j4. You can set the accordingly, or just run the command without any switch as shown below.
Once the compiling process completes, you should see something like this.
Now we can run this install command so that OpenCV is ready to use. To install OpenCV, run the following commands.
sudo make install sudo ldconfig
It would take a few minutes for the installation to complete. Once it is done, we are going to need to do some minor configurations. A successful installation looks like this.
Symlinking the OpenCV binding to our Virtual environment.
We need to add a reference to the cv2.so file into our ‘opencv’ virtual environment. The cv2.so file is usually generated at this path
To add the reference/sym-link run the following commands
cd ~/.virtualenvs/cv/lib/python2.7/site-packages/ ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so
Now we have successfully sym-linked the cv2.so file. So we have installed OpenCV 3.2.0. It is a good idea to test it before we configure it to use with the PyCharm IDE.
Testing the OpenCV installation
Open the terminal
Go inside the virtual environment ‘opencv.’
Import the OpenCV bindings for Python
Check the version of OpenCV
You should see an output like this.
If you see an error, make sure that you went into the correct virtual environment(The one we created here).
So now, we have a working installation of OpenCV, and the Python-OpenCV bindings are also functioning correctly. Let us now configure the PyCharm IDE to use the OpenCV library.
Configuring the PyCharm IDE for using OpenCV library
First of all, launch your PyCharm IDE.
As we have already downloaded the PyCharm IDE, we just need to run and configure it.
On Ubuntu, you can find the pycharm.sh file inside the bin folder of the PyCharm directory. Right click on pycharm.sh and select ‘properties’ option. Now under the Permissions tab, check the box that says ‘Allow executing file as program.’
Now close it and open a terminal window.
Go inside the bin directory of PyCharm and launch the IDE with this command.
Once PyCharm launches, we can create a new project.
You should see the screen below. Choose the ‘Create New Project’ option.
I selected the Pure Python category from the left panel. You can give a name to your project. I have named it ‘ShahsparxCV.’
You have to specify the location of the Python interpreter of your Virtual environment.
So just select the ‘Add local’ option from the Settings icon on the right. See the image below if you don’t find it.
Finally, make sure that the Interpreter path looks like this picture below.
If you don’t specify the Interpreter to be the one in your virtual environment where you have installed OpenCV, all imports to the cv2 module will fail. So, once it is done, hit the Create button.
And it is done! We have successfully configured PyCharm to work with the OpenCV library. Let us now test if works. Just create a Python file and name it whatever you want. Enter these two lines in the ‘test.py.’ File and run it. You should see the output in the Bottom console.
I hope the steps above work for you, as they did for me. We saw how to download, install and configure OpenCV. Finally, we saw how to set up the PyCharm IDE to work with OpenCV. If you meet with any error at any step, please describe it in the comments. I will try my best to fix it.
So now that you have the OpenCV library working on your computer, why not try to recognize some faces in your favorite photos? Here is a really simple guide that you can follow and step into the exciting world of Computer Vision. Also, share this article with your friends, so that they can get started too.
So that’s all for now. Take good care 🙂