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

📄 blobtrack.cpp

📁 Blob tracking with background subtraction
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "cvaux.h"
#include "highgui.h"
#include <stdio.h>
#include "Action.h"

/* Select appropriate case insensitive string comparison function: */
#if defined WIN32 || defined _MSC_VER
  #define MY_STRNICMP strnicmp
  #define MY_STRICMP stricmp
#else
  #define MY_STRNICMP strncasecmp
  #define MY_STRICMP strcasecmp
#endif

/* List of foreground (FG) DETECTION modules: */
static CvFGDetector* cvCreateFGDetector0      () { return cvCreateFGDetectorBase(CV_BG_MODEL_FGD,        NULL); }
static CvFGDetector* cvCreateFGDetector0Simple() { return cvCreateFGDetectorBase(CV_BG_MODEL_FGD_SIMPLE, NULL); }
static CvFGDetector* cvCreateFGDetector1      () { return cvCreateFGDetectorBase(CV_BG_MODEL_MOG,        NULL); }


DWORD hWnd=0;
double msg[100];


typedef struct DefModule_FGDetector
{
    CvFGDetector* (*create)();
    char* nickname;
    char* description;
} DefModule_FGDetector;

DefModule_FGDetector FGDetector_Modules[] =
{
    {cvCreateFGDetector0,"FG_0","Foreground Object Detection from Videos Containing Complex Background. ACM MM2003."},
    {cvCreateFGDetector0Simple,"FG_0S","Simplified version of FG_0"},
    {cvCreateFGDetector1,"FG_1","Adaptive background mixture models for real-time tracking. CVPR1999"},
    {NULL,NULL,NULL}
};

/* List of BLOB DETECTION modules: */
typedef struct DefModule_BlobDetector
{
    CvBlobDetector* (*create)();
    char* nickname;
    char* description;
} DefModule_BlobDetector;

DefModule_BlobDetector BlobDetector_Modules[] =
{
    {cvCreateBlobDetectorCC,"BD_CC","Detect new blob by tracking CC of FG mask"},
    {cvCreateBlobDetectorSimple,"BD_Simple","Detect new blob by uniform moving of connected components of FG mask"},
    {NULL,NULL,NULL}
};

/* List of BLOB TRACKING modules: */
typedef struct DefModule_BlobTracker
{
    CvBlobTracker* (*create)();
    char* nickname;
    char* description;
} DefModule_BlobTracker;

DefModule_BlobTracker BlobTracker_Modules[] =
{
    {cvCreateBlobTrackerCCMSPF,"CCMSPF","connected component tracking and MSPF resolver for collision"},
    {cvCreateBlobTrackerCC,"CC","Simple connected component tracking"},
    {cvCreateBlobTrackerMS,"MS","Mean shift algorithm "},
    {cvCreateBlobTrackerMSFG,"MSFG","Mean shift algorithm with FG mask using"},
    {cvCreateBlobTrackerMSPF,"MSPF","Particle filtering based on MS weight"},
    {NULL,NULL,NULL}
};

/* List of BLOB TRAJECTORY GENERATION modules: */
typedef struct DefModule_BlobTrackGen
{
    CvBlobTrackGen* (*create)();
    char* nickname;
    char* description;
} DefModule_BlobTrackGen;

DefModule_BlobTrackGen BlobTrackGen_Modules[] =
{
    {cvCreateModuleBlobTrackGenYML,"YML","Generate track record in YML format as synthetic video data"},
    {cvCreateModuleBlobTrackGen1,"RawTracks","Generate raw track record (x,y,sx,sy),()... in each line"},
    {NULL,NULL,NULL}
};

/* List of BLOB TRAJECTORY POST PROCESSING modules: */
typedef struct DefModule_BlobTrackPostProc
{
    CvBlobTrackPostProc* (*create)();
    char* nickname;
    char* description;
} DefModule_BlobTrackPostProc;

DefModule_BlobTrackPostProc BlobTrackPostProc_Modules[] =
{
    {cvCreateModuleBlobTrackPostProcKalman,"Kalman","Kalman filtering of blob position and size"},
    {NULL,"None","No post processing filter"},
//    {cvCreateModuleBlobTrackPostProcTimeAverRect,"TimeAverRect","Average by time using rectangle window"},
//    {cvCreateModuleBlobTrackPostProcTimeAverExp,"TimeAverExp","Average by time using exponential window"},
    {NULL,NULL,NULL}
};

/* List of BLOB TRAJECTORY ANALYSIS modules: */
CvBlobTrackAnalysis* cvCreateModuleBlobTrackAnalysisDetector();

typedef struct DefModule_BlobTrackAnalysis
{
    CvBlobTrackAnalysis* (*create)();
    char* nickname;
    char* description;
} DefModule_BlobTrackAnalysis;

DefModule_BlobTrackAnalysis BlobTrackAnalysis_Modules[] =
{
    {cvCreateModuleBlobTrackAnalysisHistPVS,"HistPVS","Histogram of 5D feature vector analysis (x,y,vx,vy,state)"},
    {NULL,"None","No trajectory analiser"},
    {cvCreateModuleBlobTrackAnalysisHistP,"HistP","Histogram of 2D feature vector analysis (x,y)"},
    {cvCreateModuleBlobTrackAnalysisHistPV,"HistPV","Histogram of 4D feature vector analysis (x,y,vx,vy)"},
    {cvCreateModuleBlobTrackAnalysisHistSS,"HistSS","Histogram of 4D feature vector analysis (startpos,endpos)"},
    {cvCreateModuleBlobTrackAnalysisTrackDist,"TrackDist","Compare tracks directly"},
    {cvCreateModuleBlobTrackAnalysisIOR,"IOR","Integrator (by OR operation) of several analysers "},
    {NULL,NULL,NULL}
};

/* List of Blob Trajectory ANALYSIS modules: */
/*================= END MODULES DECRIPTION ===================================*/

#include "fstream.h"
void WriteBlob(CvBlob *pB,CvBlobTrackerAuto* pTracker)
{

	return;
//	MessageBox(NULL,"Start","Here",IDOK);
    
	CvPoint p = cvPoint(cvRound(pB->x*256),cvRound(pB->y*256));
    CvSize  s = cvSize(MAX(1,cvRound(CV_BLOB_RX(pB)*256)), MAX(1,cvRound(CV_BLOB_RY(pB)*256)));
    int c = cvRound(255*pTracker->GetState(CV_BLOB_ID(pB)));

/*	cvEllipse( pI,
	p,
	s,
	0, 0, 360,
	CV_RGB(c,255-c,0), cvRound(1+(3*0)/255), CV_AA, 8 );

	//p.x >>= 8;
	//p.y >>= 8;
	//s.width >>= 8;
	//s.height >>= 8;
*/

	char str[200];
	sprintf(str,"%03d",CV_BLOB_ID(pB));

	p.y -= s.height;

	char* pS = pTracker->GetStateDesc(CV_BLOB_ID(pB));



	ofstream out;
	out.open("c:/blobtrackernew.txt",ios::out|ios::app);

//	m_gridCtrl.SetItemText(0,0,"Blob Number");
	out<<str<<"\r\n";

//	m_gridCtrl.SetItemText(0,1,"Time (msec)");
	out<<"0"<<"\r\n";


//	m_gridCtrl.SetItemText(0,2,"X Position");
	out<<p.x<<"\r\n";

//	m_gridCtrl.SetItemText(0,3,"Y Position");
	out<<p.y<<"\r\n";

//	m_gridCtrl.SetItemText(0,4,"Normal//Abnormal");
	if(pS)
		out<<pS<<"\r\n";
	else
		out<<" "<<"\r\n";

//	m_gridCtrl.SetItemText(0,5,"Area (Pixels)");
	out<<s.width*s.height<<"\r\n";

//	m_gridCtrl.SetItemText(0,6,"X Velocity (Pixels/Frame)");
	out<<"0"<<"\r\n";

//	m_gridCtrl.SetItemText(0,7,"Y Velocity (Pixels/Frame)");
	out<<"0"<<"\r\n";

//	m_gridCtrl.SetItemText(0,8,"Direction Angle");
	out<<"0"<<"\r\n";


	out.close();
//	MessageBox(NULL,"End","Here",IDOK);

}

void FastMV(IplImage *cur, IplImage *prev, int w, )




void GetEvenRows(IplImage * src, IplImage** dst)
{
	int	w=src->width;
	int h=src->height;
	CvMat *mat;

	mat=cvCreateMat(w,h/2+1,CV_8UC1);

//	m_cvResultImage=cvCreateImage(cvSize(w,h/2+1),IPL_DEPTH_8U,3);




	cvGetRows( src, mat, 0 , h, 2 );



//	cvReleaseImage(dst);


//	*dst=cvCreateImage(cvSize(w,h/2+1),IPL_DEPTH_8U,3);
//////////////////////////////////////////////////
//	Copy(mat, *dst);

	int x,y;
	for ( x=0; x<mat->width; x++) 
	{
		for ( y=0; y<mat->height; y++) 
		{
			CvScalar val = cvGet2D( mat, y, x);
			cvSet2D(*dst,y,x,cvScalarAll(val.val[0]));

		}
		for ( ; y<(*dst)->height; y++) 
		{
			cvSet2D(*dst,y,x,cvScalarAll(255));

		}


	}

//////////////////////////////////////////////////
	cvReleaseMat(&mat);

}
/* Run pipeline on all frames: */
static int RunBlobTrackingAuto( CvCapture* pCap, CvBlobTrackerAuto* pTracker,char* fgavi_name = NULL, char* btavi_name = NULL )
{
    int                     OneFrameProcess = 0;
    int                     key;
    int                     FrameNum = 0;
    CvVideoWriter*          pFGAvi = NULL;
    CvVideoWriter*          pBTAvi = NULL;

    //cvNamedWindow( "FG", 0 );
        IplImage*   pImg  = NULL;
      IplImage*   pImgRaw  = NULL;
		IplImage * pPrevImg=NULL;



	CAction action((int) cvGetCaptureProperty(pCap,CV_CAP_PROP_FRAME_WIDTH),(int) cvGetCaptureProperty(pCap,CV_CAP_PROP_FRAME_HEIGHT));

    /* Main loop: */
    for( FrameNum=0; pCap && (key=cvWaitKey(OneFrameProcess?0:1))!=27;
         FrameNum++)
    {   /* Main loop: */
        IplImage*   pMask = NULL;

        if(key!=-1)
        {
            OneFrameProcess = 1;
            if(key=='r')OneFrameProcess = 0;
        }


		

        pImgRaw	 = cvQueryFrame(pCap);
        if(pImgRaw == NULL) break;

		///////////////////////////////////////////

		//GetEvenRows(pImgRaw,&pImgRaw);//Taghi Taghi GHoli khaaaaaaan
		//pImg=pImgRaw;

		pImg=action.Action(pImgRaw);

			cvResizeWindow("Tracking",720,240);

		//cvSubRS(pImg, cvScalarAll(255),pImg);


//	MessageBox(NULL,"Here","Here",IDOK);

        /* Process: */
        pTracker->Process(pImg, pMask);

        if(fgavi_name)
        if(pTracker->GetFGMask())
        {   /* Debug FG: */
            IplImage*           pFG = pTracker->GetFGMask();
            CvSize              S = cvSize(pFG->width,pFG->height);
            static IplImage*    pI = NULL;

            if(pI==NULL)pI = cvCreateImage(S,pFG->depth,3);
            cvCvtColor( pFG, pI, CV_GRAY2BGR );

            if(fgavi_name)
            {   /* Save fg to avi file: */
                if(pFGAvi==NULL)
                {
                    pFGAvi=cvCreateVideoWriter(
                        fgavi_name,
                        CV_FOURCC('x','v','i','d'),
                        25,
                        S );
                }
                cvWriteFrame( pFGAvi, pI );
            }

            if(pTracker->GetBlobNum()>0)
            {   /* Draw detected blobs: */
                int i;
                for(i=pTracker->GetBlobNum();i>0;i--)
                {

                    CvBlob* pB = pTracker->GetBlob(i-1);
                    CvPoint p = cvPointFrom32f(CV_BLOB_CENTER(pB));
                    CvSize  s = cvSize(MAX(1,cvRound(CV_BLOB_RX(pB))), MAX(1,cvRound(CV_BLOB_RY(pB))));
                    int c = cvRound(255*pTracker->GetState(CV_BLOB_ID(pB)));
                    cvEllipse( pI,
                        p,
                        s,
                        0, 0, 360,
                        CV_RGB(c,255-c,0), cvRound(1+(3*c)/255) );



                }   /* Next blob: */;
            }

            cvNamedWindow( "FG",0);
            cvShowImage( "FG",pI);
        }   /* Debug FG. */


        /* Draw debug info: */
        if(pImg)
        {   /* Draw all information about test sequence: */
            char        str[1024];
            int         line_type = CV_AA;   // Change it to 8 to see non-antialiased graphics.
            CvFont      font;
            int         i;
            IplImage*   pI = cvCloneImage(pImg);

            cvInitFont( &font, CV_FONT_HERSHEY_PLAIN, 0.7, 0.7, 0, 1, line_type );

            for(i=pTracker->GetBlobNum(); i>0; i--)
            {
                CvSize  TextSize;
                CvBlob* pB = pTracker->GetBlob(i-1);
                CvPoint p = cvPoint(cvRound(pB->x*256),cvRound(pB->y*256));
                CvSize  s = cvSize(MAX(1,cvRound(CV_BLOB_RX(pB)*256)), MAX(1,cvRound(CV_BLOB_RY(pB)*256)));
                int c = cvRound(255*pTracker->GetState(CV_BLOB_ID(pB)));

                cvEllipse( pI,
                    p,
                    s,
                    0, 0, 360,
                    CV_RGB(c,255-c,0), cvRound(1+(3*0)/255), CV_AA, 8 );

                p.x >>= 8;
                p.y >>= 8;
                s.width >>= 8;
                s.height >>= 8;
                sprintf(str,"%03d",CV_BLOB_ID(pB));
                cvGetTextSize( str, &font, &TextSize, NULL );
                p.y -= s.height;
                cvPutText( pI, str, p, &font, CV_RGB(0,255,255));
                {
                    char* pS = pTracker->GetStateDesc(CV_BLOB_ID(pB));

                    if(pS)
                    {
                        char* pStr = strdup(pS);
                        char* pStrFree = pStr;

                        while (pStr && strlen(pStr) > 0)
                        {
                            char* str_next = strchr(pStr,'\n');

                            if(str_next)
                            {
                                str_next[0] = 0;
                                str_next++;
                            }

                            p.y += TextSize.height+1;
                            cvPutText( pI, pStr, p, &font, CV_RGB(0,255,255));
                            pStr = str_next;
                        }
                        free(pStrFree);
                    }
                }
						/////////////////////////////
						WriteBlob(pB,pTracker);
						/////////////////////////////

            }   /* Next blob. */;
            
            cvNamedWindow( "Tracking", 0);
            cvShowImage( "Tracking",pI );

            if(btavi_name && pI)
            {   /* Save to avi file: */
                CvSize      S = cvSize(pI->width,pI->height);
                if(pBTAvi==NULL)
                {
                    pBTAvi=cvCreateVideoWriter(
                        btavi_name,
                        CV_FOURCC('x','v','i','d'),
                        25,
                        S );
                }
                cvWriteFrame( pBTAvi, pI );
            }

            cvReleaseImage(&pI);
        }   /* Draw all information about test sequence. */
    }   /*  Main loop. */

    if(pFGAvi)cvReleaseVideoWriter( &pFGAvi );
    if(pBTAvi)cvReleaseVideoWriter( &pBTAvi );
    return 0;
}   /* RunBlobTrackingAuto */

/* Read parameters from command line
 * and transfer to specified module:
 */
static void set_params(int argc, char* argv[], CvVSModule* pM, char* prefix, char* module)
{
    int prefix_len = strlen(prefix);
    int i;
    for(i=0; i<argc; ++i)
    {
        int j;
        char* ptr_eq = NULL;
        int   cmd_param_len=0;
        char* cmd = argv[i];
        if(MY_STRNICMP(prefix,cmd,prefix_len)!=0) continue;
        cmd += prefix_len;
        if(cmd[0]!=':')continue;
        cmd++;

⌨️ 快捷键说明

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