Download the Project folder and the source code.
//______________________________________________________________________________________
// OpenCV Blobs detection program
// 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;
int frame_count= 0;
int i;
/* create a window for the video */
//cvNamedWindow( "Webcam",CV_WINDOW_AUTOSIZE);
cvNamedWindow( "Blobs",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 200) */
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);
/* Color the blobs one after the other */
for (i = 0; i < blobs.GetNumBlobs(); i++ )
{
currentBlob = blobs.GetBlob(i);
currentBlob->FillBlob( blobs_img, CV_RGB(255,0,0));
}
//cvShowImage( "Webcam",gray_img);
cvShowImage( "Blobs",blobs_img);
/* Clear image for next iteration */
cvZero(blobs_img);
/* Quit execution if 'ESC' key is pressed */
key = cvWaitKey(1);
}
/* Clean up memory */
//cvDestroyWindow( "Webcam" );
cvDestroyWindow( "Blobs" );
cvReleaseCapture( &capture );
return 0;
}
THanks for sharing, but this link doesn't work. Can you solve this issue?
ReplyDelete