Blob pointer using OpenCV & cvblobslib

Well... i had time to waste on this :P....


Download the source here:
Blob pointer [Source code] 4.61 Mb


Source:


//______________________________________________________________________________________
// OpenCV cursor movent using Blobs
// Author: Bharath Prabhuswamy
//______________________________________________________________________________________
//______________________________________________________________________________________
#include <stdio.h>
#include "cv.h"
#include "highgui.h"
#include "BlobResult.h"

int main()
{
    CvCapture *capture = 0;
    IplImage  *img = 0;
 IplImage  *gray_img = 0;
 IplImage  *thres_img = 0;
    IplImage  *blobs_img = 0;
 int key = 0;
 
    /* Initialize the webcam */
    capture = cvCaptureFromCAM( 0 );
 
    /* Always check if the program can find a device */
    if ( !capture ) 
        return -1;
 
 CBlobResult blobs;
 CBlob *currentBlob;
 CvRect rect;

 int frame_count = 0;
 int i = 0;

 int screen_x = GetSystemMetrics(SM_CXSCREEN);
 int screen_y = GetSystemMetrics(SM_CYSCREEN);
 int mouse_x,mouse_y;
 double x=0;
 double y=0;
 
    /* create a window for the video */
 cvNamedWindow( "Blobs",CV_WINDOW_AUTOSIZE);
 //cvNamedWindow( "Thres",CV_WINDOW_AUTOSIZE);
 
 /* Create required images once */
 if( frame_count == 0 )
    {
        /* Obtain a frame from the device */
        img = cvQueryFrame( capture );

  /* Always check if the device returns a frame */
  if( !img ) 
   return -1;

  gray_img  = cvCreateImage( cvGetSize(img), img->depth, 1);
  thres_img = cvCreateImage( cvGetSize(img), img->depth, 1);
  blobs_img = cvCreateImage( cvGetSize(img), img->depth, 3);
 }
 
    while((char) key != 27 )
    {
        /* Obtain a frame from the device */
        img = cvQueryFrame( capture );

  /* Always check if the device returns a frame */
  if( !img ) 
   return -1;
  
  frame_count = frame_count + 1;
  
  /* Flip image once, after blob processing it is flipped back */
  cvFlip(img,img,NULL);

  /* Convert image from Color to grayscale and 
                   then to binary (thresholded at 180) */
  cvCvtColor(img,gray_img,CV_RGB2GRAY);
  cvThreshold(gray_img,thres_img,200,255,CV_THRESH_BINARY);
  
  /* Find Blobs that are White, Hence 'uchar backgroundColor = 0' (Black) */
  blobs = CBlobResult(thres_img, NULL,0);

  /* Remove blobs if it does not cover minimum area specified below */
  blobs.Filter( blobs, B_EXCLUDE, CBlobGetArea(),B_LESS,5,50);

  /* Number of blobs */
  int j = blobs.GetNumBlobs();
  printf("%d\n",j);
  
  /* 'i' points to blob 0, i.e., first blob */
  /* If some blobs are detected then find the first blob */
  if(i==0 && blobs.GetNumBlobs()>i)
  {
   currentBlob = blobs.GetBlob(i);
   rect = currentBlob->GetBoundingBox();
   x = currentBlob->MinX();
   y = currentBlob->MinY();
   //printf("%d    %d\n",x,y);

   /* Scale the capture size to the Screen 
                        resolution and set the cursor location */
   mouse_x = (int) x*screen_x/img->width;
   mouse_y = (int) y*screen_y/img->height;
   SetCursorPos(mouse_x,mouse_y);

   /* Color the blob*/
   currentBlob->FillBlob( blobs_img, CV_RGB(255,0,0));
  }

  cvShowImage( "Blobs",blobs_img);
  //cvShowImage( "Thres",thres_img);
  
  /* Clear image for next iteration */
  cvZero(blobs_img);
  
  /* Quit execution if 'ESC' key is pressed */
  key = cvWaitKey(1);
 }

 /* Clean up memory */
 cvDestroyWindow( "Blobs" );
    cvReleaseCapture( &capture );
    return 0;
}
Video:

Image: