November 08, 2011

OpenCV on Friendly ARM

      In the next few posts, I will discuss how to use OpenCV on ARM processor for image processing. I will discuss with reference to FriendlyARM Mini6410 (ARM11). The processor is Samsung S3C6410 with 667MHz, 128 MB RAM, 2 USB ports and a serial port. For implementing OpenCV on ARM, I will recommend a processor with speed greater than 500 MHz.
      OpenCV can be implemented on FriendlyARM with any of the operating systems like Linux, Windows CE, Android, etc. I have used Linux for my project. Just connect a USB camera to your board, run the executable file on your board and your system starts working independently on just 5V supply. That is the advantage of image processing on ARM.
      The first step is to install Linux on FriendlyARM which I will explain in my next post.

October 11, 2011

Execute OpenCV Program in Linux (Ubuntu)



      In my last post, I discussed how to install OpenCV on Linux - Ubuntu. Today I will show you how to execute OpenCV programs on Linux - Ubuntu. Remember, you don't need Microsoft Visual Studio unlike OpenCV in Windows. After compiling the program, you get the executible file of your program.
      Remember to complete every step of installation procedure. Then only start to compile your program.

1. 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.

2. For testing:
Create a new folder & a file “hello.cpp” in it using any text editor & type this code in it:
#include < cv.h > /* required to use OpenCV */
#include < highgui.h > /* required to use OpenCV's highgui */
#include < stdio.h >

int main(int argc, char *argv[]) {
IplImage* img=0; /* pointer to an image */
printf("Hello\n");
if(argv[1] != 0)
img = cvLoadImage(argv[1], 0); // 1 for color
else
printf("Enter filename\n");
if(img != 0) {
cvNamedWindow("Display", CV_WINDOW_AUTOSIZE); // create a window
cvShowImage("Display", img); // show image in window
cvWaitKey(0); // wait until user hits a key
cvDestroyWindow("Display");
}
else
printf("File not found\n");
return 0;
}

3. 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, as given in the next step.

4. To compile & run:
$ g++ -I/usr/include/opencv -lcxcore -lhighgui -lm hello.cpp
./a.out img

where “img” is the name of any image within the same folder with extension.
You should be able to see “Hello” and the image in a different window.

-> If this runs, Congrats! now you can run any C/C++ program with opencv lib.
Else, try

$ export PATH=$HOME/usr/bin/:$PATH

and go to step 1 again.

5. Now lets simplify the above big command by making a shortcut for it:
go to your local home directory(cd /home/) and open the .bashrc file using gedit(the file will be hidden). Append the following to the file:

alias gcv="g++ -I/usr/include/opencv -lcv -lcxcore -lcvaux -lhighgui -lm"
and save. Close the terminal and open it again.(as this process requires relogin of the terminal)

6.Now, go to directory containing a sample program & do
$ gcv filename.c && ./a.out
or
$ gcv filename.c
$ ./a.out input_img.jpg
As you can see the commands now become similar to $cc filename.c, $./a.out which are used normally for compiling and executing C programs.

****************************
Some ways to check whether all lib files are installed-
1. $ apt-cache search opencv
returns:
libcv-dev - development files for libcv
libcv0.9-0c2 - computer vision library
libcvaux-dev - development files for libcvaux
libcvaux0.9-0c2a - computer vision extension library
libhighgui-dev - development files for libhighgui
libhighgui0.9-0c2 - computer vision GUI library
opencv-doc - OpenCV documentation and examples

      In this way, you can compile and execute your OpenCV programs on Linux - Ubuntu

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.

August 31, 2011

The Extended Euclidean Algorithm

Try this!! You will definitely find it interesting.
Consider two integers m and n. Find two integers a and b such that m*a + n*b = gcd(m,n)

Example 1: m = 65; n = 40

Step 1: The (usual) Euclidean algorithm:
(1) 65 = 1*40 + 25
(2) 40 = 1*25 + 15
(3) 25 = 1*15 + 10
(4) 15 = 1*10 + 5
     10 = 2*5  + 0    (Do this until remainder = 0)
Therefore: gcd(65; 40) = 5.

Step 2: Using the method of back-substitution:
5 (4) = 15 - 10
   (3) = 15 - (25 - 15) = 2*15 - 25
   (2) = 2(40 - 25) - 25 = 2*40 - 3*25
   (1) = 2*40 - 3(65 - 40) = 5*40 - 3*65

Conclusion: 65(-3) + 40(5) = 5.



Example 2: m = 1239; n = 735
Step 1: The (usual) Euclidean algorithm:
(1) 1239 = 1*735 + 504
(2)  735  = 1*504 + 231
(3)  504  = 2*231 + 42
(4)   23   = 5*42 + 21
       42   = 2*21 + 0
Therefore: gcd(1239; 735) = 21.

Step 2: Using the method of back-substitution:
21 (4) = 231 - 5*42
     (3) = 231 - 5(504 - 2*231) = 11*231 - 5*504
     (2) = 11(735 - 504) - 5*504 = 11*735 - 16*504
     (1) = 11*735 - 16(1239 - 735) = 27*735 - 16*1239

Conclusion: 1239(-16) + 735(27) = 21.

August 25, 2011

Install OpenCV 2.0 on Windows

          To install OpenCv on your Windows, you need Microsoft Visual Studio 2008 and CMake. Remember, this process is little bit complicated and lengthy. But, it's worth of it.

The process is explained in detail in this document.

Install OpenCV and run the sample code. In the next post, I will show some simple OpenCV programs and also
how to install OpenCV on Linux platform.

August 19, 2011

Image Processing: Matlab vs OpenCV

File:OpenCV Logo with text.png                     Do you get bored by the speed of Matlab while processing images or videos? Then try OpenCV. It is easy to use and more importantly, almost 20 times faster than Matlab Image Processing Toolbox. It is mainly used for real time computer vision. It is an open source software and you can download OpenCv for free here .

      OpenCV stands for Open Source Computer Vision Library. It is a library of programming functions. This library was developed by Intel and is now supported by Willow Garage. It is free for use under the open source BSD license. The library is cross-platform. It focuses mainly on real-time image processing.
      
        The library is written in C and C++ and runs under Linux, Windows and Mac OS X. There is active development on interfaces for Python, Ruby, Matlab, and other languages. OpenCV was designed for computational efficiency and with a strong focus on real time applications. OpenCV is written in optimized C and can take advantage of multi-core processors.
       
        The OpenCV library contains over 500 functions that span many areas in vision, including factory medical imaging, security, user interface, camera calibration, stereo vision, and robotics. Because computer vision and machine learning often go hand-in- hand, OpenCV also contains a full, general-purpose Machine Learning Library (MLL). This sub-library is focused on statistical pattern recognition and clustering.

         In the future posts, I will share my experience of using OpenCV, both on Windows and Linux platforms.