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

OpenCL Development Environment Setup

Windows 7, 8, and 8.1

To install on your system:
  1. Determine your processor type (Intel Core/Xeon or AMD) and system type (64-bit or 32-bit): if you don't already know, go to the Windows Control Panel, select "View by: Large Icons" or "View by: Small Icons", then click on "System". The "Processor" line will display the CPU type, and the "System Type" line will tell you whether you have a 64-bit or 32-bit system.
  2. Download the appropriate 64-bit or 32-bit AMD-APP-SDK-v2.9-1 installer from http://developer.amd.com/tools-and-sdks/heterogeneous-computing/amd-accelerated-parallel-processing-app-sdk/downloads/ (do this regardless of whether you have an Intel or AMD processor).
  3. Run the downloaded installer. When you reach the "Setup Type" screen, select "Custom" and click "Next". On the "Custom Setup" screen, you may, if you wish, deselect all optional features except OpenCL Samples -> Sample Source Code (see below). This saves several hundred MB of disk space.
    AMD_SDK_custom_setup.png
    If you have an Intel CPU, install the latest version of "OpenCL™ Runtime xx.x for Intel® Core™ and Intel® Xeon® Processors for Windows* OS (64-bit & 32-bit)" from https://software.intel.com/en-us/articles/opencl-drivers . If your computer has an Intel HD or Iris GPU, you may also optionally install the drivers for that device.
You'll find the OpenCL code sample files referred to below in subdirectory "AMD APP SDK\2.9\samples\opencl" under the directory where you installed the SDK.
A Visual Studio 2012 solution file (OpenCLSamplesVS12.sln) is available in the samples\opencl directory to build and run the samples (this solution file will work with Visual Studio 2013 also). Some of the samples also use OpenGL and may not build without AMD OpenGL support installed.
To build and run one of the samples (for example, "HelloWorld") using the VS12 solution file:
  • Open OpenCLSamplesVS12.sln in Visual Studio 2013
  • Right-click the desired project from the "Solution Explorer" pane: for example, "HelloWorld"
    • On the popup menu, select "Set as StartUp Project"
  • Press Ctrl-F5 to build and run the project
  • If the project fails to run, try specifying the command arguments (Links to an external site.)
         --device cpu

and run the program again
Mac OS X

OpenCL development support is already installed in Xcode 5 and later (see https://developer.apple.com/opencl (for details). You don't need to install anything.

Linux

Follow these steps to install the AMD APP SDK on your Linux computer (this also applies for machines with Intel CPUs):