How to install OpenCV for Python in Linux

OpenCV is the best open source solution for computer vision and image recognition applications.  It exists for all the major platforms. In this post, we are going to see how to set it up in Ubuntu Linux or Linux Mint.

We are looking at OpenCV 3.0.x which is the latest version as of now. Though you can obtain the source using apt-get , it is better to directly download it from the main site, as that is where the latest version is always available.

Step 1. Install Dependencies

We will need cmake to build the OpenCV as its in C++ so we need to update cmake first.

sudo apt-get install build-essential cmake

Next, we install the Qt library

sudo apt-get install qt5-default libvtk6-dev

Install image manipulation libraries

sudo apt-get install zlib1g-dev libjpeg-dev libwebp-dev libpng-dev libtiff5-dev libjasper-dev libopenexr-dev libgdal-dev

Install video manipulation libraries

sudo apt-get install libdc1394-22-dev libavcodec-dev libavformat-dev libswscale-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev yasm libopencore-amrnb-dev libopencore-amrwb-dev libv4l-dev libxine2-dev

Install supporting math libraries

sudo apt-get install libtbb-dev libeigen3-dev

Step 2. Update languages

We need to update python and Java.

sudo apt-get install python-dev python-tk python-numpy python3-dev python3-tk python3-numpy
sudo apt-get install python-matplotlib
sudo apt-get install ant default-jdk

Step 3: Compile OpenCV

Download the latest source zip from here . On this page click on OpenCV for Linux/Mac which will start the download of the zip file.

Unzip the zip into a directory of your choice. eg. /var/projects/opencv

Make that directory your current directory and type mkdir build so you end up with /var/projects/opencv/build . Go into the build directory cd build

Setup Cmake configuration and run the build.

cmake -DWITH_QT=ON -DWITH_OPENGL=ON -DFORCE_VTK=ON -DWITH_TBB=ON -DWITH_GDAL=ON -DWITH_XINE=ON -DBUILD_EXAMPLES=ON ..
make -j4
sudo make install

Run configure with

sudo ldconfig

This completes the setup of OpenCV.

Step 4: Running the Python Samples

Got into the samples folder eg. cd /var/projects/opencv/samples/python2

You will find a lot of samples here.

If you have a cam attached then do python video.py

To run a simple example, try python floodfill.py

You can get a list and explanation of all the samples by running python demo.py

 

Here is a video of me running the Canny Edge detection sample. python edge.py

 

1 Trackback / Pingback

  1. Installation OpenCV in LinuxMint (Rosa) | RPoernama

Leave a Reply

Your email address will not be published.


*