OpenCV installation on Linux and Windows

ubuntu_opencv-1

How hard is to install OpenCV?

This was the question that I asked myself lately when I needed to use OpenCV for a project. I thought it must be simpler on Ubuntu than on Windows. But I was wrong. The goal of this tutorial is to provide working guidelines for OpenCV installation. I’ll cover installation instructions for OpenCV with following configurations:

Windows 7/ 10 

  • OpenCV 3.x.x with Python 2.7
  • OpenCV 3.x.x with Python 3.5

Ubuntu 16.04 

  • OpenCV 3.x.x with Python 3.5

Installation on Windows 7/ 10

OpenCV 3.x.x with Python 2.7 on Windows 32 bit

To have all the dependencies that are related to Python it is useful to install Anaconda.

  • Install Anaconda 2 for Python 2.7 (32 or 64 bit)
  • Install Anaconda 3 for Python 3.5 (32 or 64 bit)
  • Now we can install OpenCV by using pre-built libraries by downloading them from here.
  • For the sake of this tutorial I used OpenCV version 3.2.0
    • opencv-3.2.0-vc14.exe
  • After you’ve installed downloaded OpenCV version there is a need to move cv2.pyd file to a Python installation library.

Look for the cv2.pyd at the opencv installation folder

C:\Users\You\Downloads\opencv\build\python\2.7\x64\cv2.pyd

And move the cv2.pyd file to Python 2.7 installation folder

C:\Users\You\Anaconda2\Lib\site-packages\cv2.pyd

python_2.7.png

Example application

  • To test that opencv installed correctly
  • Open command line and run python. Then type the commands below to figure out what is the current opencv version.
C:\Users\You>python
Python 2.7.13 |Anaconda 4.3.0 (32-bit)| (default, Dec 19 2016, 13:36:02) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import cv2
>>> print(cv2.__version__)
3.2.0
>>>

OpenCV 3.x.x with Python 3.5 using Wheel on Windows 7 64 bit

  • There is no library for Python 3.5 support in OpenCV out of the box that is why we can use  unofficial Windows binaries for Python extension packages from here to be able to use it.

Note: I downloaded this one because I have Windows 7 64 bit

  • opencv_python-3.2.0-cp35-cp35m-win_amd64.whl

Pay attention that 3.2.0 means opencv version i.e. opencv-3.2.0

cp35 means Python version i.e. Python 3.5

  • After you downloaded this file open the command line and open the directory this file located in. For example, let’s say it was downloaded to Downloads folder.
  • Change current folder to Downloads 
C:\>cd C:\Users\You\Downloads
  • Install wheel with pip install command
C:\Users\You\Downloads>pip install opencv_python-3.2.0-cp35-cp35m-win_amd64.whl
Processing c:\users\andrei\downloads\opencv_python-3.2.0-cp35-cp35m-win_amd64.whl
Installing collected packages: opencv-python
Successfully installed opencv-python-3.2.0
You are using pip version 8.1.2, however version 9.0.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

C:\Users\You\Downloads>
  • Pay attention that you saw this line ‘Successfully installed opencv-python-3.2.0’

Example application

  • To test that opencv installed correctly
  • Open command line and run python. Then type the commands below to figure out what is the current opencv version.
C:\Users\You\Downloads>python
Python 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul 5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print(cv2.__version__)
3.2.0
>>>

Additional resources

Installation on Ubuntu 16.04

To install OpenCV on Ubuntu follow the steps in the guides below. The first one is the best and it worked for me.

  • Simply run this command for basic opencv3 installation.
conda install -c menpo opencv3
  • If Anaconda is not installed then run this one to install it.
sudo apt-get install python-opencv

Additional resources

What’s next?

Now that you have a working OpenCV you may watch this nice tutorial by Siraj Raval that is funny and hands on with OpenCV. It will teach you How to do Object Detection with OpenCV. It will also teach you that there is a need to run a code at least once before filming a YouTube video.

In addition if you are interested in object detection with OpenCV then definitely look at Satya Mallick tutorial on the subject.

 Java Code Geeks