Installing OpenCV and Using its Python Wrapper (For Linux)

If you are new to OpenCV, here is a quick introduction taken verbatim from opencv.org. OpenCV is released under a BSD license and hence it’s free for both academic and commercial use. It has C++, C, Python and Java interfaces and supports Windows, Linux, Mac OS, iOS and Android. OpenCV was designed for computational efficiency and with a strong focus on real-time applications. Written in optimized C/C++, the library can take advantage of multi-core processing. Enabled with OpenCL, it can take advantage of the hardware acceleration of the underlying heterogeneous compute platform.

In this tutorial,  we will learn how to install OpenCV and make it available to your python installation so you can use it in your Python application. The best way to install OpenCV is to download the source code and compile it yourself using ‘make’ . Here are the steps you need to follow:

    1.  Download the source code from opencv.org to your computer.
    2. Unzip the folder and change the directory to the folder that was downloaded
    3. Make a new directory in this folder called ‘build’ and change the directory to this directory.
    4. Now install all the dependencies that you will need to build the source code.
      sudo apt-get install build-essential
      sudo apt-get install cmake libgtk2.0-dev pkg-config
      sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
      [Optional] sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
      The last command is optional.
    5. Configure make by running the following command. The following command specifies that build type is “Release Mode” and installation path is /usr/local. Observe the -D before each option and .. at the end. This is the format of CMake.
      cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
      You can also add a lot more configurations depending on what features you want in your build. For example, the following command will enable TBB and Eigen.
      cmake -D WITH_TBB=ON -D WITH_EIGEN=ON ..
      To enable documentation and disable tests and samples, you would run the following command:
      cmake -D BUILD_DOCS=ON -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF ..
      To disable all GPU related features, you would run the following:
      cmake -D WITH_OPENCL=OFF -D WITH_CUDA=OFF -D BUILD_opencv_gpu=OFF -D BUILD_opencv_gpuarithm=OFF -D BUILD_opencv_gpubgsegm=OFF -D BUILD_opencv_gpucodec=OFF -D BUILD_opencv_gpufeatures2d=OFF -D BUILD_opencv_gpufilters=OFF -D BUILD_opencv_gpuimgproc=OFF -D BUILD_opencv_gpulegacy=OFF -D BUILD_opencv_gpuoptflow=OFF -D BUILD_opencv_gpustereo=OFF -D BUILD_opencv_gpuwarping=OFF ..
    6. When you are happy with your make configuration, you are ready to build the source code by running the following command.
      sudo make install

Upon successful build, your installation will be in the folder /usr/local/lib. Depending on what version of Python you already had, you will see ‘cv2.so’ in the site-packages of that python folder. In my case, I had Anaconda distribution of Python 2.7 and therefore, I had ‘cv2.so’ file in /usr/local/lib/python2.7/site-packages
screen1

Now we need to make ‘cv2.so’ file available to our Python. We can do this by adding PYTHONPATH environment variable that will point to  /usr/local/lib/python2.7/site-packages. This can be done by running the following command which will add the line to your .bashrc file.
export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages

Now to test whether it works. Just go to python shell and type 'import cv2'. If you don't see any error, there you have it. Now you can use the power of OpenCV in your python application.

References:

OpenCV-Python Tutorials

OpenCV Documentation

Books