Monday, 21 March 2011

Installtion guide for Opencv in Linux using the Terminal


This article is basically for installing Opencv 2.1 in Ubuntu 10.10. Opencv basically helps in image processing using c++ . For reading or writing images in c++ , we require Opencv. The steps below are the commands to be given in the terminal.

1.This installation needs a C++ compiler like “g++”. So install g++  by  typing the following command at the terminal–

$ sudo apt-get install g++


2. OpenCV requires GTK+ 2.0 or higher for displaying images. GTK is graphical user interface library. Type out the following code to know the version of GTK+ installed on ur pc.

    $ dpkg -l | grep libgtk

This command should give o/p something like this if its installed on your system:

ii  libgtk-vnc-1.0-0  0.4.1-3ubuntu2  A VNC viewer widget for GTK+ (runtime libraries)
ii  libgtk2-perl  2:1.222-1  Perl interface to the 2.x series of the Gimp Toolkit library
ii  libgtk2.0-0  2.22.0-0ubuntu1  The GTK+ graphical user interface library
ii  libgtk2.0-bin  2.22.0-0ubuntu1  The programs for the GTK+ graphical user interface library
ii  libgtk2.0-cil  2.12.10-1  CLI binding for the GTK+ toolkit 2.12
ii  libgtk2.0-common  2.22.0-0ubuntu1 Common files for the GTK+ graphical user interface library
ii  libgtk2.0-dev  2.22.0-0ubuntu1  Development files for the GTK+ library
ii  libgtkhtml-editor-common  1:3.30.3-1ubuntu1  HTML rendering/editing library - editor widget data
ii  libgtkhtml-editor0  1:3.30.3-1ubuntu1  HTML rendering/editing library - editor widget
ii  libgtkhtml3.14-19  1:3.30.3-1ubuntu1  HTML rendering/editing library - runtime files
ii  libgtkmm-2.4-1c2a  1:2.20.3-1  C++ wrappers for GTK+ (shared libraries)
ii  libgtksourceview2.0-0  2.10.5-0ubuntu1  shared libraries for the GTK+ syntax highlighting widget
ii  libgtksourceview2.0-common  2.10.5-0ubuntu1  common files for the GTK+ syntax highlighting widget
ii  libgtkspell0  2.0.16-1  a spell-checking addon for GTK's TextView widget


If not, then you need to install the libraries for GTK+ 2.0 first, then start with this opencv installation.
To install GTK+ 2.0 libraries, type the following:

    $ sudo apt-get install libgtk2.0-dev



3. After installing all the packages, type this code:

$ export LD_LIBRARY_PATH=/home/opencv/lib      
$ export PKG_CONFIG_PATH=/home/opencv/lib/pkgconfig

The above ones are default paths for the opencv libraries.
4. To check the path where opencv & other lib files are stored, do:

    $ pkg-config --cflags opencv

This output shows the path of the header file as:
-I/usr/include/opencv

   $ pkg-config --libs opencv
This output shows the libraries required to be included.
-lcxcore -lcv -lhighgui -lcvaux -lml

An Example code:

This code reads an image an displays it.It is named as img.cc

#include <stdlib.h>
#include <iostream>
#include <math.h>
#include </usr/include/opencv/cv.h>
#include </usr/include/opencv/highgui.h>
using namespace std;

int main(int argc, char *argv[])
{
  IplImage* img = 0;
  IplImage* img1=cvCreateImage(cvSize(256,256),IPL_DEPTH_8U,1);
  int height,width,step,channels;
  unsigned char *data;
  int i,j,k;

  if(argc<2){
    cout<<"Usage: main <image-file-name>";
    exit(0);
  }

  // load an image 
  img=cvLoadImage(argv[1],-1);    //-1 is a flag (flag<0 the loaded image is loaded as is (with number of channels in the file)
  if(!img){
    cout<<"Could not load image file:"<<argv[1]<<endl;
    exit(0);
  }

  // get the image data
  height    = img->height;
  width     = img->width;
  step      = img->widthStep;
  channels  = img->nChannels;
  data      = (unsigned char *)img->imageData;

  cout<<"Height - "<<height<<endl;
  cout<<"Width - "<<width<<endl;
  cout<<"WidthStep - "<<step<<endl;
  cout<<"Channels - "<<channels<<endl;
  cout<<endl;

  cvNamedWindow("Read Image", CV_WINDOW_AUTOSIZE);


  // creating the image
  for(i=0;i<height;i++) for(j=0;j<width;j++) for(k=0;k<channels;k++)
    data[i*step+j*channels+k]=data[i*step+j*channels+k];

  img1->imageData=(char*)data;

  // show the image
  cvShowImage("Read Image", img1 );

  // wait for a key
  cvWaitKey(0);

  // release the image
  cvReleaseImage(&img );
  return 0;
}

These paths are needed to compile your opencv programs as shown below:
5. To compile & run:

    $ g++ -I/usr/include/opencv -lcxcore -lhighgui -lm img.cc
 
    where img.cc is c++ code for reading an image and displaying it.
  
    $ ./a.out lena.bmp
   

6. For simplyfying the above command create an alias of the command in the home directory as
$ alias ocv="g++ -I/usr/include/opencv -lcv -lcxcore -lcvaux -lhighgui -lm" 
$ ocv img.cc
$./a.out lena.bmp

Following steps 5 and 6 we get the output as shown below.


Reading an image in C++ using OpenCv

   
   





No comments:

Post a Comment