You are on page 1of 5

#include #include #include #include #include #include

"cv.h" "cvaux.h" "highgui.h" "cxcore.h" <ctype.h> <stdio.h>

#include "BlobResult.h" //int blobExtraction(); //int contourDrawing(); //IplImage* BlobIm = NULL; //IplImage* g_image = NULL; //IplImage* videoFrame = NULL; G int main(int argc, char* argv[]) { /* Start capturing */ CvCapture* capture = 0; /* Capture from CAM if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0 ]))) capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 ); else if( argc == 2 )*/ capture = cvCaptureFromAVI( "back.avi"); //capture = cvCaptureFromCAM(0); if( !capture ) { fprintf(stderr,"Could not initialize...\n"); return -1; } /* Capture 1 video frame for initialization */ //IplImage* videoFrame = NULL; videoFrame = cvQueryFrame(capture); if(!videoFrame) { printf("Bad frame \n"); exit(0); } // Create windows cvNamedWindow("RGB",1); //cvNamedWindow("BG", 1); cvNamedWindow("FG", 1); cvNamedWindow("Blobs", 1); cvNamedWindow( "Contours",1); // Select parameters for Gaussian model. CvGaussBGStatModelParams* params = new CvGaussBGStatModelParams; params->win_size=2; params->n_gauss=3;

params->bg_threshold=0.7; params->std_threshold=15; params->minArea=15; params->weight_init=0.05; params->variance_init=30; CvBGStatModel* bgModel = cvCreateGaussianBGModel(videoFrame ,params); //Write FG in a file CvVideoWriter *writer = 0; int isColor = 1; int fps = 25; // or 30 int frameW = 200; // 744 for firewire cameras int frameH = 200; // 480 for firewire cameras //writer=cvCreateVideoWriter("out.avi",CV_FOURCC('D', 'I', 'V', 'X'), // fps,cvSize(frameW,frameH),isColor); // CV_FOURCC, ( 'D', 'I', 'V', 'X') int key=-1; int count=0; while (count<500) //while(key != 'q') { // Grab a fram videoFrame = cvQueryFrame(capture); //cvFlip(videoFrame, 0); if( !videoFrame ) { break;} // Update model cvUpdateBGStatModel(videoFrame,bgModel); // Display results //cvShowImage("BG", bgModel->background); cvShowImage("FG", bgModel->foreground); // Write foreground in AVI formet cvWriteFrame(writer,bgModel->foreground); // Copy foreground to a veriable used for Blob Extraction BlobIm = bgModel->foreground; // Copy foreground to a veriable used for Contour Drawing g_image = bgModel->foreground; // function is called for Blob Extraction blobExtraction(); // function is called for Contour Drawing contourDrawing(); count++; key = cvWaitKey(10); } //cvDestroyWindow("BG"); cvDestroyWindow("FG");

cvDestroyWindow("Blobs"); cvDestroyWindow("Contours"); cvWaitKey(0); cvReleaseBGStatModel( &bgModel ); cvReleaseCapture(&capture); cvReleaseVideoWriter(&writer); return 0; } /* int blobExtraction() { // get blobs and filter them using its area CBlobResult blobs; int i; //int count; CBlob *currentBlob; int num_blobs; IplImage* originalThr = NULL; IplImage* displayNoBlob = NULL; if(!originalThr) { originalThr = cvCreateImage(cvGetSize(BlobIm), IPL_DEPTH_8U,1); } if(!displayNoBlob) { displayNoBlob = cvCreateImage(cvGetSize(BlobIm), IPL_DEPTH_8U,3) ; } // threshold input image for blob finding BlobIm cvThreshold( BlobIm, originalThr, 40,255 , CV_THRESH_BINARY ); cvDilate(BlobIm,BlobIm,0,30);

// for finding blobs and its area and filter those blobs blobs = CBlobResult(BlobIm, NULL, 0); blobs.Filter( blobs, B_INCLUDE , CBlobGetArea(),B_GREATER ,1); //cvMerge( BlobIm, BlobIm, BlobIm, NULL, displayNoBlob ); num_blobs= blobs.GetNumBlobs(); printf ("Count Number of Blobs in a Frame:%d\n",num_blobs); CvPoint rect_vertice_1, rect_vertice_2; for (i = 0; i < num_blobs; i++ ) { currentBlob = blobs.GetBlob(i); currentBlob->FillBlob( displayNoBlob, CV_RGB(255,0,0)); //CV_RGB (255,0,0) .. cvScalar(125), 0, 0

rect_vertice_1.x rect_vertice_1.y rect_vertice_2.x rect_vertice_2.y

= = = =

(int)currentBlob->MinX(); (int)currentBlob->MinY(); (int)currentBlob->MaxX(); (int)currentBlob->MaxY();

//cvRectangle( displayNoBlob, rect_vertice_1, rect_vertice_2, CV _RGB(0, 255, 0), 1, 8, 0); cvRectangle( videoFrame, rect_vertice_1, rect_vertice_2, CV_RGB( 0, 255, 0), 1, 8, 0); } cvShowImage("Blobs", displayNoBlob); cvShowImage("RGB",videoFrame); return 0; } int contourDrawing() { IplImage* g_gray = NULL; CvSeq* contours = 0; CvMemStorage* g_storage = NULL; if( g_storage == NULL ) { g_gray = cvCreateImage( cvGetSize( g_image ), IPL_DEPTH_8U, 1 ); g_storage = cvCreateMemStorage(0); } else { cvClearMemStorage( g_storage ); } // threshold input image for Contur drawing cvThreshold( g_image, g_gray, 100, 255, CV_THRESH_BINARY ); cvFindContours( g_gray, g_storage, &contours, sizeof(CvContour), CV_ RETR_LIST,CV_CHAIN_APPROX_SIMPLE ); cvZero( g_gray ); CvSeq* current_contour = contours; long area, largestArea=0; CvSeq* largest_contour=NULL; //if (contours == NULL){return NULL;} //for finding max contour and its area while (current_contour != NULL) { area = fabs(cvContourArea(current_contour)); //,CV_WHOLE_SEQ //printf("Area:%d\n",area); if(area > largestArea) { largestArea = area; //if(largestArea>40) {largest_contour = current_contour; // printf("LargestArea:%d\n",largestArea); }

} current_contour=current_contour->h_next; } if(largestArea>60) // draw contour cvDrawContours(g_gray,largest_contour, cvScalarAll(255),cvScalarAll( 255),100); cvShowImage( "Contours", g_gray ); return 0; }*/