⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 blobcentre.cpp

📁 opencv codes in C++ (linux and eclipse)
💻 CPP
字号:
//c++ opencv code for blob detection and marking blob centre #include "cv.h"#include "cxcore.h"#include "highgui.h"#include "BlobResult.h"#include "BlobExtraction.h"#include "Blob.h"#include "cvaux.h"#include <stdio.h>IplImage  *image, *gray, *dst;int main( int argc, char **argv){    CvFont dfont;    float hscale      = 0.5f;    float vscale      = 0.5f;    float italicscale = 0.0f;    int  thickness    = 2;    char text[255]    = "";	    cvInitFont (&dfont, CV_FONT_HERSHEY_SIMPLEX , hscale, vscale, italicscale, thickness, CV_AA);	//capture video	CvCapture* capture = cvCaptureFromFile("output.avi");	//created 4 window to display images    cvNamedWindow("Capture", 0);    cvNamedWindow("Result", 0);    cvNamedWindow("Foreground", 0);        //print error if it doesnt capture image     if(!capture)    {        fprintf( stderr, "ERROR: capture is NULL \n" );        getchar();        return -1;    }    // query the frame and save it in image    IplImage* image = cvQueryFrame(capture);    CvBGStatModel* bg_model;    //gaussian background subtraction	bg_model =cvCreateGaussianBGModel( image );		//loop through each frame    for( int fr = 1;image; image = cvQueryFrame(capture), fr++ )    {          if(!image)        {            fprintf( stderr, "ERROR: frame is null...\n" );            getchar();            break;        }				//created a copy of image in finalFrame        IplImage* finalFrame = cvCloneImage(image);		//update the image        cvUpdateBGStatModel( image, bg_model );                                //created a copy of foreground in frame        IplImage* frame = cvCloneImage(bg_model->foreground);         cvSmooth(frame, frame, CV_BLUR);		cvErode(frame,frame,NULL,5);		cvDilate(frame,frame,NULL,5);		cvSmooth(frame, frame, CV_BLUR, 3, 3);        cvPutText(bg_model->foreground, "foreground", cvPoint(20,400), &dfont, CV_RGB(255,255,255));                               IplImage* gsFrame = cvCreateImage(cvSize(image->width,image->height),IPL_DEPTH_8U,1);               //converted the image to gray scale and save it in gsFrame        cvCvtColor(image, gsFrame, CV_BGR2GRAY);               	dst = cvCreateImage( cvSize( image->width, image->height ), IPL_DEPTH_8U, 3 );			cvCopy( image, dst );	cvPutText(dst, "original frame", cvPoint(20,400), &dfont, CV_RGB(255,255,255));	//Blob detection------------------------------------------------	CBlobResult blobs;	blobs = CBlobResult( frame, NULL, 100, true );	blobs.Filter( blobs, B_INCLUDE, CBlobGetArea(), B_LESS,5000);	blobs.Filter( blobs, B_INCLUDE, CBlobGetArea(), B_GREATER,0);			int blobnum = blobs.GetNumBlobs();	CBlob blob;                                      	CvPoint p1, p2; 		for(int a = 0; a < blobnum; a++)	{		blob = blobs.GetBlob(a);		p1.x = (int)blob.MinX();		p1.y = (int)blob.MinY();		p2.x = (int)blob.MaxX();		p2.y = (int)blob.MaxY();				int Meanx=(p1.x+p2.x)/2;		int Meany=(p1.y+p2.y)/2;			    printf("%d (%d,%d)", a, Meanx, Meany);	    sprintf(text, "%d", (int)blob.area);						cvLine( finalFrame, cvPoint(Meanx, Meany), cvPoint(Meanx, Meany), CV_RGB(153, 0 , 0), 4, 8, 0 );				sprintf(text, "%d", (int)blob.area);		cvPutText(finalFrame, text, cvPoint(p1.x, p2.y + 15), &dfont, CV_RGB(0,255,255));		cvRectangle(finalFrame, p1, p2, CV_RGB( 204, 0, 0 ), 2, 8, 0 );	}	    		cvShowImage( "Capture", dst );	cvMoveWindow("Capture",0,0);        cvShowImage( "Result", finalFrame );        cvMoveWindow("Result", 500, 0);        cvShowImage("Foreground", bg_model->foreground);        cvMoveWindow("Foreground", 0, 400);        cvSaveImage("output.jpg",finalFrame);    	cvReleaseImage(&gsFrame);        cvReleaseImage(&finalFrame);		cvWaitKey(10);    }    		cvWaitKey(0);		cvReleaseCapture( &capture );  		cvReleaseBGStatModel(&bg_model);  	     cvDestroyWindow( "Capture" );    cvDestroyWindow( "Result" );    cvDestroyWindow( "Foreground" );   	cvReleaseImage( &image );	cvReleaseImage( &gray );	cvReleaseImage( &dst );	cvDestroyAllWindows(); 	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -