September 27, 2011

Install OpenCV on Linux (Ubuntu)


Unlike OpenCV in windows, you don't need visual studio to execute your OpenCV programs in Linux.

Following are the steps to install OpenCV on Linux-Ubuntu. Copy and execute following code lines in Ubuntu terminal one by one. Remember, you need internet connection for this installation.

1. You require a g++ compiler for programming.
$ sudo apt-get install g++

2. OpenCV requires GTK+ 2.0 or higher version to display images.To install GTK+,
$ sudo apt-get install libgtk2.0-dev

3. Go to Synaptic Package Manager (System> Administration> Synaptic Package Manager)

4. Search for “opencv” and install the main “opencv” package and the following lib files:
libcv
libcv-dev
libcvaux
libcvaux-dev
libhighgui
libhighgui-dev
opencv-doc
(‘python-opencv’ not required).
(you can also install opencv directly from the terminal by “sudo apt-get install” the above lib files

5. After installing all the packages, open a terminal & 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.

6. To check the path where opencv & other lib files are stored, do:

$ pkg-config --cflags opencv
(output will come as)
-I/usr/include/opencv
$ pkg-config --libs opencv
(output will come as)
-lcxcore -lcv -lhighgui -lcvaux -lml

These paths are needed to compile your opencv programs.
In the next post, I will show you how to compile OpenCV programs in Ubuntu.

September 21, 2011

Serial Communication in OpenCV

    To communicate the results of your program with the interfaced hardware, you need the serial communication.
First add the file serial.cpp to your project.
Syntax:
WriteComPort("COM2","f"); // COM2-COM port, f- character to be sent
Don't forget to check your COM port to which your hardware is connected.

September 15, 2011

Detection of Circles using OpenCV


    In the last post, I discussed how to install OpenCV on Windows and how to create and run a project. Now I will show some simple OpenCV programs.

First, consider the program below, used for detection of green circles in the video. It also prints the centers of green circles detected in each frame.

PROGRAM:

#include"highgui.h"
#include"cv.h"
void main()
{

cvNamedWindow( "Example2", CV_WINDOW_AUTOSIZE );
CvCapture* capture = cvCreateCameraCapture(0);
IplImage* frame;
while(1) {

frame = cvQueryFrame( capture );
if( !frame ) break;
IplImage* result1=cvCreateImage(cvGetSize(frame),8,1);
IplImage* result=cvCreateImage(cvGetSize(frame),8,3);
cvCvtColor(frame,result,CV_BGR2RGB);
for(int y=0;y<result->height;y++)
{
 uchar* ptr = (uchar*) (result->imageData + y * result->widthStep);
   for( int x=0; x<result->width; x++ )
   {
      if(ptr[3*x+1]<=155 && ptr[3*x]<=255 && ptr[3*x+2]<=255)
    {
  ptr[3*x] = 0;
  ptr[3*x+1] =0;
  ptr[3*x+2] = 0;
  }
  else
  {
  ptr[3*x] = 255;
  ptr[3*x+1] = 255;
  ptr[3*x+2] = 255;
 }
}
}
cvCvtColor(result,result1,CV_BGR2GRAY);
CvMemStorage* storage = cvCreateMemStorage(0);
cvSmooth(result1, result1, CV_GAUSSIAN, 5, 5 );
CvSeq* results = cvHoughCircles(
result1,
storage,
CV_HOUGH_GRADIENT,
2,
result1->width/10
);
for( int i = 0; i < results->total; i++ ) {
float* p = (float*) cvGetSeqElem( results, i );
CvPoint pt = cvPoint( cvRound( p[0] ), cvRound( p[1] ) );
printf("\t%d %d",pt.x,pt.y);
cvCircle(
result1,
pt,
cvRound( p[2] ),
CV_RGB(0xff,0xff,0xff)
);
}

cvShowImage( "Example2", frame );
cvShowImage( "result1", result1 );
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( "Example2" );

}


     You can easily modify this program to detect circles of other colors, to find number of circles, etc.
In the next post,  I will show how to install OpenCV on Linux platform and how to create and run a project on it. I will also show the serial communication in OpenCV.

September 06, 2011

C program for The Extended Euclidean Algorithm

     In the last post, i discussed about the extended Euclidean Algorithm. Now I will show the program in C language for this algorithm.