opencv自带的跟踪代码位置:opencv\samples\c\blobtrack_sample.cpp
- #include "cvaux.h"
- #include "highgui.h"
- #include <stdio.h>
- /* select the correct function for doing case insensitive string comparaison */
- #ifdef WIN32
- #define MY_STRNICMP strnicmp
- #define MY_STRICMP stricmp
- #else
- #define MY_STRNICMP strncasecmp
- #define MY_STRICMP strcasecmp
- #endif
- /* list of FG DETECTION modules */ /* FG(前景)检测模块的列表 */
- 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);}
- 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","Simplyfied version of FG_0"},
- {cvCreateFGDetector1,"FG_1","Adaptive background mixture models for real-time tracking. CVPR1999"},// 高斯混合模型运动目标检测
- {NULL,NULL,NULL}
- };
- /* list of BLOB DETECTION modules */ /* BLOB检测模块的列表 */
- 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 */ /* BLOB跟踪模块的列表 */
- 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 */ /* BLOB轨迹生成模块的列表 */
- 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 */ /* BLOB轨迹后处理模块的列表 */
- 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 */ /* BLOB轨迹分析模块的列表 */
- CvBlobTrackAnalysis* cvCreateModuleBlobTrackAnalysisDetector();
- typedef struct DefModule_BlobTrackAnalysis
- {
- CvBlobTrackAnalysis* (*create)();
- char* nickname;
- char* description;
- } DefModule_BlobTrackAnalysis;
- DefModule_BlobTrackAnalysis BlobTrackAnalysis_Modules[] =
- {
- {cvCreateModuleBlobTrackAnalysisHistPVS,"HistPVS","Histogramm of 5D feture vector analysis (x,y,vx,vy,state)"},
- {NULL,"None","No trajectory analiser"},
- {cvCreateModuleBlobTrackAnalysisHistP,"HistP","Histogramm of 2D feture vector analysis (x,y)"},
- {cvCreateModuleBlobTrackAnalysisHistPV,"HistPV","Histogramm of 4D feture vector analysis (x,y,vx,vy)"},
- {cvCreateModuleBlobTrackAnalysisHistSS,"HistSS","Histogramm of 4D feture 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 */ /* BLOB轨迹分析模块的列表 */
- /*================= END MODULES DECRIPTION ===================================*/
- /* 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 );
- /* main cicle */
- for( FrameNum=0; pCap && (key=cvWaitKey(OneFrameProcess?0:2))!=27;
- FrameNum++)
- { /* main cicle */
- IplImage* pImg = NULL;
- IplImage* pMask = NULL;
- if(key!=-1)
- {