目标跟踪之:blob_tracking 跟踪代码中文注释

opencv自带的跟踪代码位置:opencv\samples\c\blobtrack_sample.cpp
[cpp]  view plain copy
  1. #include "cvaux.h"  
  2. #include "highgui.h"  
  3. #include   
  4.   
  5. /* select the correct function for doing case insensitive string comparaison */  
  6. #ifdef WIN32  
  7.   #define MY_STRNICMP strnicmp  
  8.   #define MY_STRICMP stricmp  
  9. #else  
  10.   #define MY_STRNICMP strncasecmp  
  11.   #define MY_STRICMP strcasecmp  
  12. #endif  
  13.   
  14. /* list of FG DETECTION modules */ /* FG(前景)检测模块的列表 */  
  15. static CvFGDetector* cvCreateFGDetector0(){return cvCreateFGDetectorBase(CV_BG_MODEL_FGD, NULL);}  
  16. static CvFGDetector* cvCreateFGDetector0Simple(){return cvCreateFGDetectorBase(CV_BG_MODEL_FGD_SIMPLE, NULL);}  
  17. static CvFGDetector* cvCreateFGDetector1(){return cvCreateFGDetectorBase(CV_BG_MODEL_MOG, NULL);}  
  18. typedef struct DefModule_FGDetector  
  19. {  
  20.     CvFGDetector* (*create)();  
  21.     char* nickname;  
  22.     char* description;  
  23. } DefModule_FGDetector;  
  24. DefModule_FGDetector FGDetector_Modules[] =  
  25. {  
  26.     {cvCreateFGDetector0,"FG_0","Foreground Object Detection from Videos Containing Complex Background. ACM MM2003."},  
  27.     {cvCreateFGDetector0Simple,"FG_0S","Simplyfied version of FG_0"},  
  28.     {cvCreateFGDetector1,"FG_1","Adaptive background mixture models for real-time tracking. CVPR1999"},// 高斯混合模型运动目标检测  
  29.     {NULL,NULL,NULL}  
  30. };  
  31.   
  32. /* list of BLOB DETECTION modules */ /* BLOB检测模块的列表 */  
  33. typedef struct DefModule_BlobDetector  
  34. {  
  35.     CvBlobDetector* (*create)();  
  36.     char* nickname;  
  37.     char* description;  
  38. } DefModule_BlobDetector;  
  39. DefModule_BlobDetector BlobDetector_Modules[] =  
  40. {  
  41.     //{cvCreateBlobDetectorCC,"BD_CC","Detect new blob by tracking CC of FG mask"},  
  42.     {cvCreateBlobDetectorSimple,"BD_Simple","Detect new blob by uniform moving of connected components of FG mask"},  
  43.     {NULL,NULL,NULL}  
  44. };  
  45.   
  46. /* list of BLOB TRACKING modules */ /* BLOB跟踪模块的列表 */  
  47. typedef struct DefModule_BlobTracker  
  48. {  
  49.     CvBlobTracker* (*create)();  
  50.     char* nickname;  
  51.     char* description;  
  52. } DefModule_BlobTracker;  
  53. DefModule_BlobTracker BlobTracker_Modules[] =  
  54. {  
  55.     //{cvCreateBlobTrackerCCMSPF,"CCMSPF","connected component tracking and MSPF resolver for collision"},  
  56.     //{cvCreateBlobTrackerCC,"CC","Simple connected component tracking"},  
  57.     //{cvCreateBlobTrackerMS,"MS","Mean shift algorithm "},  
  58.     //{cvCreateBlobTrackerMSFG,"MSFG","Mean shift algorithm with FG mask using"},  
  59.     {cvCreateBlobTrackerMSPF,"MSPF","Particle filtering based on MS weight"},  
  60.     {NULL,NULL,NULL}  
  61. };  
  62.   
  63. /* list of BLOB TRAJECTORY GENERATION modules */ /* BLOB轨迹生成模块的列表 */  
  64. typedef struct DefModule_BlobTrackGen  
  65. {  
  66.     CvBlobTrackGen* (*create)();  
  67.     char* nickname;  
  68.     char* description;  
  69. } DefModule_BlobTrackGen;  
  70. DefModule_BlobTrackGen BlobTrackGen_Modules[] =  
  71. {  
  72.     {cvCreateModuleBlobTrackGenYML,"YML","Generate track record in YML format as synthetic video data"},  
  73.     {cvCreateModuleBlobTrackGen1,"RawTracks","Generate raw track record (x,y,sx,sy),()... in each line"},  
  74.     {NULL,NULL,NULL}  
  75. };  
  76.   
  77. /* list of BLOB TRAJECTORY POST PROCESSING modules */ /* BLOB轨迹后处理模块的列表 */  
  78. typedef struct DefModule_BlobTrackPostProc  
  79. {  
  80.     CvBlobTrackPostProc* (*create)();  
  81.     char* nickname;  
  82.     char* description;  
  83. } DefModule_BlobTrackPostProc;  
  84. DefModule_BlobTrackPostProc BlobTrackPostProc_Modules[] =  
  85. {  
  86.     {cvCreateModuleBlobTrackPostProcKalman,"Kalman","Kalman filtering of blob position and size"},  
  87.     {NULL,"None","No post processing filter"},  
  88. //    {cvCreateModuleBlobTrackPostProcTimeAverRect,"TimeAverRect","Average by time using rectangle window"},  
  89. //    {cvCreateModuleBlobTrackPostProcTimeAverExp,"TimeAverExp","Average by time using exponential window"},  
  90.     {NULL,NULL,NULL}  
  91. };  
  92.   
  93. /* list of BLOB TRAJECTORY ANALYSIS modules */ /* BLOB轨迹分析模块的列表 */  
  94. CvBlobTrackAnalysis* cvCreateModuleBlobTrackAnalysisDetector();  
  95.   
  96. typedef struct DefModule_BlobTrackAnalysis  
  97. {  
  98.     CvBlobTrackAnalysis* (*create)();  
  99.     char* nickname;  
  100.     char* description;  
  101. } DefModule_BlobTrackAnalysis;  
  102. DefModule_BlobTrackAnalysis BlobTrackAnalysis_Modules[] =  
  103. {  
  104.     {cvCreateModuleBlobTrackAnalysisHistPVS,"HistPVS","Histogramm of 5D feture vector analysis (x,y,vx,vy,state)"},  
  105.     {NULL,"None","No trajectory analiser"},  
  106.     {cvCreateModuleBlobTrackAnalysisHistP,"HistP","Histogramm of 2D feture vector analysis (x,y)"},  
  107.     {cvCreateModuleBlobTrackAnalysisHistPV,"HistPV","Histogramm of 4D feture vector analysis (x,y,vx,vy)"},  
  108.     {cvCreateModuleBlobTrackAnalysisHistSS,"HistSS","Histogramm of 4D feture vector analysis (startpos,endpos)"},  
  109.     {cvCreateModuleBlobTrackAnalysisTrackDist,"TrackDist","Compare tracks directly"},  
  110.     {cvCreateModuleBlobTrackAnalysisIOR,"IOR","Integrator (by OR operation) of several analysers "},  
  111.     {NULL,NULL,NULL}  
  112. };  
  113. /* list of Blob Trajectory ANALYSIS modules */ /* BLOB轨迹分析模块的列表 */  
  114. /*================= END MODULES DECRIPTION ===================================*/  
  115.   
  116. /* run pipeline on all frames */  
  117. /* 运行传递途径上的所有帧 */  
  118. static int RunBlobTrackingAuto( CvCapture* pCap, CvBlobTrackerAuto* pTracker,char* fgavi_name = NULL, char* btavi_name = NULL )  
  119. {  
  120.     int                     OneFrameProcess = 0;  
  121.     int                     key;  
  122.     int                     FrameNum = 0;  
  123.     CvVideoWriter*          pFGAvi = NULL;  
  124.     CvVideoWriter*          pBTAvi = NULL;  
  125.   
  126.     //cvNamedWindow( "FG", 0 );  
  127.   
  128.     /* main cicle */  
  129.     for( FrameNum=0; pCap && (key=cvWaitKey(OneFrameProcess?0:2))!=27;  
  130.          FrameNum++)  
  131.     {/* main cicle */  
  132.         IplImage*   pImg = NULL;  
  133.         IplImage*   pMask = NULL;  
  134.   
  135.         if(key!=-1)  
  136.         {  
  137.             OneFrameProcess = 1;  
  138.             if(key=='r')OneFrameProcess = 0;  
  139.         }  
  140.   
  141.         pImg = cvQueryFrame(pCap);  
  142.         if(pImg == NULL) break;  
  143.   
  144.   
  145.         /* Process */  
  146.         pTracker->Process(pImg, pMask);  
  147.   
  148.         if(fgavi_name)  
  149.         if(pTracker->GetFGMask())  
  150.         {/* debug FG */  
  151.             IplImage*           pFG = pTracker->GetFGMask();  
  152.             CvSize              S = cvSize(pFG->width,pFG->height);  
  153.             static IplImage*    pI = NULL;  
  154.   
  155.             if(pI==NULL)pI = cvCreateImage(S,pFG->depth,3);  
  156.             cvCvtColor( pFG, pI, CV_GRAY2BGR );  
  157.   
  158.             if(fgavi_name)  
  159.             {/* save fg to avi file */  
  160.                 if(pFGAvi==NULL)  
  161.                 {  
  162.                     pFGAvi=cvCreateVideoWriter(  
  163.                         fgavi_name,  
  164.                         CV_FOURCC('x','v','i','d'),  
  165.                         25,  
  166.                         S );  
  167.                 }  
  168.                 cvWriteFrame( pFGAvi, pI );  
  169.             }  
  170.   
  171.             if(pTracker->GetBlobNum()>0)  
  172.             {/* draw detected blobs */  
  173.                 int i;  
  174.                 for(i=pTracker->GetBlobNum();i>0;i--)  
  175.                 {  
  176.                     CvBlob* pB = pTracker->GetBlob(i-1);  
  177.                     CvPoint p = cvPointFrom32f(CV_BLOB_CENTER(pB));  
  178.                     CvSize  s = cvSize(MAX(1,cvRound(CV_BLOB_RX(pB))), MAX(1,cvRound(CV_BLOB_RY(pB))));  
  179.                     int c = cvRound(255*pTracker->GetState(CV_BLOB_ID(pB)));  
  180.                     cvEllipse( pI,  
  181.                         p,  
  182.                         s,  
  183.                         0, 0, 360,  
  184.                         CV_RGB(c,255-c,0), cvRound(1+(3*c)/255) );  
  185.                 }/* next blob */;  
  186.             }  
  187.   
  188.             cvNamedWindow( "FG",0);  
  189.             cvShowImage( "FG",pI);  
  190.         }/* debug FG*/  
  191.   
  192.   
  193.         /* draw debug info */  
  194.         if(pImg)  
  195.         {/* draw all inforamtion about tets sequence */  
  196.             char        str[1024];  
  197.             int         line_type = CV_AA; // change it to 8 to see non-antialiased graphics  
  198.             CvFont      font;  
  199.             int         i;  
  200.             IplImage*   pI = cvCloneImage(pImg);  
  201.   
  202.             cvInitFont( &font, CV_FONT_HERSHEY_PLAIN, 0.7, 0.7, 0, 1, line_type );  
  203.   
  204.             for(i=pTracker->GetBlobNum();i>0;i--)  
  205.             {  
  206.                 CvSize  TextSize;  
  207.                 CvBlob* pB = pTracker->GetBlob(i-1);  
  208.                 CvPoint p = cvPoint(cvRound(pB->x*256),cvRound(pB->y*256));  
  209.                 CvSize  s = cvSize(MAX(1,cvRound(CV_BLOB_RX(pB)*256)), MAX(1,cvRound(CV_BLOB_RY(pB)*256)));  
  210.                 int c = cvRound(255*pTracker->GetState(CV_BLOB_ID(pB)));  
  211.   
  212.                 cvEllipse( pI,  
  213.                     p,  
  214.                     s,  
  215.                     0, 0, 360,  
  216.                     CV_RGB(c,255-c,0), cvRound(1+(3*0)/255), CV_AA, 8 );  
  217.                 p.x >>= 8;  
  218.                 p.y >>= 8;  
  219.                 s.width >>= 8;  
  220.                 s.height >>= 8;  
  221.                 sprintf(str,"%03d",CV_BLOB_ID(pB));  
  222.                 cvGetTextSize( str, &font, &TextSize, NULL );  
  223.                 p.y -= s.height;  
  224.                 cvPutText( pI, str, p, &font, CV_RGB(0,255,255));  
  225.                 {  
  226.                     const char* pS = pTracker->GetStateDesc(CV_BLOB_ID(pB));  
  227.                     if(pS)  
  228.                     {  
  229.                         char* pStr = strdup(pS);  
  230.                         char* pStrFree = pStr;  
  231.                         for(;pStr && strlen(pStr)>0;)  
  232.                         {  
  233.                             char* str_next = strchr(pStr,'\n');  
  234.                             if(str_next)  
  235.                             {  
  236.                                 str_next[0] = 0;  
  237.                                 str_next++;  
  238.                             }  
  239.                             p.y += TextSize.height+1;  
  240.                             cvPutText( pI, pStr, p, &font, CV_RGB(0,255,255));  
  241.                             pStr = str_next;  
  242.                         }  
  243.                         free(pStrFree);  
  244.                     }  
  245.                 }  
  246.   
  247.             }/* next blob */;  
  248.               
  249.             cvNamedWindow( "Tracking", 0);  
  250.             cvShowImage( "Tracking",pI );  
  251.   
  252.             if(btavi_name && pI)  
  253.             {/* save to avi file */  
  254.                 CvSize      S = cvSize(pI->width,pI->height);  
  255.                 if(pBTAvi==NULL)  
  256.                 {  
  257.                     pBTAvi=cvCreateVideoWriter(  
  258.                         btavi_name,  
  259.                         CV_FOURCC('x','v','i','d'),  
  260.                         25,  
  261.                         S );  
  262.                 }  
  263.                 cvWriteFrame( pBTAvi, pI );  
  264.             }  
  265.   
  266.             cvReleaseImage(&pI);  
  267.         }/* draw all inforamtion about tets sequence */  
  268.     }/* main cicle */  
  269.   
  270.     if(pFGAvi)cvReleaseVideoWriter( &pFGAvi );  
  271.     if(pBTAvi)cvReleaseVideoWriter( &pBTAvi );  
  272.     return 0;  
  273. }/* RunBlobTrackingAuto */  
  274.   
  275. /* read parameters from command line and transfer to specified module */  
  276. /* 读取命令行参数并转移到指定的模块 */  
  277. static void set_params(int argc, char* argv[], CvVSModule* pM, char* prefix, char* module)  
  278. {  
  279.     int prefix_len = strlen(prefix);  
  280.     int i;  
  281.     for(i=0;i
  282.     {  
  283.         int j;  
  284.         char* ptr_eq = NULL;  
  285.         int   cmd_param_len=0;  
  286.         char* cmd = argv[i];  
  287.         if(MY_STRNICMP(prefix,cmd,prefix_len)!=0) continue;  
  288.         cmd += prefix_len;  
  289.         if(cmd[0]!=':')continue;  
  290.         cmd++;  
  291.   
  292.         ptr_eq = strchr(cmd,'=');  
  293.         if(ptr_eq)cmd_param_len = ptr_eq-cmd;  
  294.         for(j=0;;++j)  
  295.         {  
  296.             int     param_len;  
  297.             const char*   param = pM->GetParamName(j);  
  298.             if(param==NULL) break;  
  299.             param_len = strlen(param);  
  300.             if(cmd_param_len!=param_len) continue;  
  301.             if(MY_STRNICMP(param,cmd,param_len)!=0) continue;  
  302.             cmd+=param_len;  
  303.             if(cmd[0]!='=')continue;  
  304.             cmd++;  
  305.             pM->SetParamStr(param,cmd);  
  306.             printf("%s:%s param set to %g\n",module,param,pM->GetParam(param));  
  307.         }  
  308.     }  
  309.     pM->ParamUpdate();  
  310. }/* set_params */  
  311.   
  312. /* print all parameters value for given module */ /* 对于给定的模块,打印所有参数的值 */  
  313. static void print_params(CvVSModule* pM, char* module, char* log_name)  
  314. {  
  315.     FILE* log = log_name?fopen(log_name,"at"):NULL;  
  316.     int i;  
  317.     if(pM->GetParamName(0) == NULL ) return;  
  318.   
  319.   
  320.     printf("%s(%s) module parameters:\n",module,pM->GetNickName());  
  321.     if(log)  
  322.         fprintf(log,"%s(%s) module parameters:\n",module,pM->GetNickName());  
  323.     for(i=0;;++i)  
  324.     {  
  325.         const char*   param = pM->GetParamName(i);  
  326.         const char*   str = param?pM->GetParamStr(param):NULL;  
  327.         if(param == NULL)break;  
  328.         if(str)  
  329.         {  
  330.             printf("  %s: %s\n",param,str);  
  331.             if(log)  
  332.                 fprintf(log,"  %s: %s\n",param,str);  
  333.         }  
  334.         else  
  335.         {  
  336.             printf("  %s: %g\n",param,pM->GetParam(param));  
  337.             if(log)  
  338.                 fprintf(log,"  %s: %g\n",param,pM->GetParam(param));  
  339.         }  
  340.     }  
  341.     if(log)fclose(log);  
  342. }/* print_params */  
  343.   
  344. int main(int argc, char* argv[])  
  345. {/* main function */  
  346.     CvCapture*                  pCap = NULL;  
  347.     CvBlobTrackerAutoParam1     param = {0};  
  348.     CvBlobTrackerAuto*          pTracker = NULL;  
  349.   
  350.     float       scale = 1;  
  351.     char*       scale_name = NULL;  
  352.     char*       yml_name = NULL;  
  353.     char**      yml_video_names = NULL;  
  354.     int         yml_video_num = 0;  
  355.     char*       avi_name = NULL;  
  356.     char*       fg_name = NULL;  
  357.     char*       fgavi_name = NULL;  
  358.     char*       btavi_name = NULL;  
  359.     char*       bd_name = NULL;  
  360.     char*       bt_name = NULL;  
  361.     char*       btgen_name = NULL;  
  362.     char*       btpp_name = NULL;  
  363.     char*       bta_name = NULL;  
  364.     char*       bta_data_name = NULL;  
  365.     char*       track_name = NULL;  
  366.     char*       comment_name = NULL;  
  367.     char*       FGTrainFrames = NULL;  
  368.     char*       log_name = NULL;  
  369.     char*       savestate_name = NULL;  
  370.     char*       loadstate_name = NULL;  
  371.     char*       bt_corr = NULL;  
  372.     DefModule_FGDetector*           pFGModule = NULL;  
  373.     DefModule_BlobDetector*         pBDModule = NULL;  
  374.     DefModule_BlobTracker*          pBTModule = NULL;  
  375.     DefModule_BlobTrackPostProc*    pBTPostProcModule = NULL;  
  376.     DefModule_BlobTrackGen*         pBTGenModule = NULL;  
  377.     DefModule_BlobTrackAnalysis*    pBTAnalysisModule = NULL;  
  378.   
  379.     cvInitSystem(argc, argv);  
  380.   
  381.     if(argc < 2)  
  382.     {/* print help *//* 打印帮助 */  
  383.         int i;  
  384.         printf("blobtrack [fg=] [bd=]\n"  
  385.             "          [bt=] [btpp=]\n"  
  386.             "          [bta=\n"  
  387.             "          [bta_data=\n"  
  388.             "          [bt_corr=]\n"  
  389.             "          [btgen=]\n"  
  390.             "          [track=]\n"  
  391.             "          [scale=] [noise=] [IVar=]\n"  
  392.             "          [FGTrainFrames=]\n"  
  393.             "          [btavi=] [fgavi=]\n"  
  394.             "          \n");  
  395.         printf("   is way of blob position corrrection for \"Blob Tracking\" module\n"  
  396.             "     =none,PostProcRes\n"  
  397.             "   is number of frames for FG training\n"  
  398.             "   is file name for save tracked trajectories\n"  
  399.             "   is file name for data base of trajectory analysis module\n"  
  400.             "   is file name of avi to process by BlobTrackerAuto\n");  
  401.   
  402.         puts("\nModules:");  
  403. #define PR(_name,_m,_mt)\  
  404.         printf("<%s> is \"%s\" module name and can be:\n",_name,_mt);\  
  405.         for(i=0;_m[i].nickname;++i)\  
  406.         {\  
  407.             printf("  %d. %s",i+1,_m[i].nickname);\  
  408.             if(_m[i].description)printf(" - %s",_m[i].description);\  
  409.             printf("\n");\  
  410.         }  
  411.   
  412.         PR("fg_name",FGDetector_Modules,"FG/BG Detection");  
  413.         PR("bd_name",BlobDetector_Modules,"Blob Entrance Detection");  
  414.         PR("bt_name",BlobTracker_Modules,"Blob Tracking");  
  415.         PR("btpp_name",BlobTrackPostProc_Modules, "Blob Trajectory Post Processing");  
  416.         PR("btgen_name",BlobTrackGen_Modules, "Blob Trajectory Generation");  
  417.         PR("bta_name",BlobTrackAnalysis_Modules, "Blob Trajectory Analysis");  
  418. #undef PR  
  419.         return 0;  
  420.     }/* print help *//* 打印帮助 */  
  421.   
  422.     {/* parse arguments *//* 解析传入参数 */  
  423.         int i;  
  424.         for(i=1;i
  425.         {  
  426.             int bParsed = 0;  
  427.             size_t len = strlen(argv[i]);  
  428. #define RO(_n1,_n2) if(strncmp(argv[i],_n1,strlen(_n1))==0) {_n2 = argv[i]+strlen(_n1);bParsed=1;};  
  429.             RO("fg=",fg_name);  
  430.             RO("fgavi=",fgavi_name);  
  431.             RO("btavi=",btavi_name);  
  432.             RO("bd=",bd_name);  
  433.             RO("bt=",bt_name);  
  434.             RO("bt_corr=",bt_corr);  
  435.             RO("btpp=",btpp_name);  
  436.             RO("bta=",bta_name);  
  437.             RO("bta_data=",bta_data_name);  
  438.             RO("btgen=",btgen_name);  
  439.             RO("track=",track_name);  
  440.             RO("comment=",comment_name);  
  441.             RO("FGTrainFrames=",FGTrainFrames);  
  442.             RO("log=",log_name);  
  443.             RO("savestate=",savestate_name);  
  444.             RO("loadstate=",loadstate_name);  
  445. #undef RO  
  446.             {  
  447.                 char* ext = argv[i] + len-4;  
  448.                 if( strrchr(argv[i],'=') == NULL &&  
  449.                     !bParsed &&  
  450.                     (len>3 && (MY_STRICMP(ext,".avi") == 0 )))  
  451.                 {  
  452.                     avi_name = argv[i];  
  453.                     break;  
  454.                 }  
  455.             }/* next argument */  
  456.         }  
  457.     }/* parse arguments *//* 解析传入参数 */  
  458.   
  459.     if(track_name)  
  460.     {/* set Trajectory Generator module */ /* 设置轨迹生成模块 */  
  461.         int i;  
  462.         if(!btgen_name)btgen_name=BlobTrackGen_Modules[0].nickname;  
  463.         for(i=0;BlobTrackGen_Modules[i].nickname;++i)  
  464.         {  
  465.             if(MY_STRICMP(BlobTrackGen_Modules[i].nickname,btgen_name)==0)  
  466.                 pBTGenModule = BlobTrackGen_Modules + i;  
  467.         }  
  468.     }/* set Trajectory Generator modulke */ /* 设置轨迹生成模块 */  
  469.   
  470.     /* init postprocessing module if tracker correction by postporcessing is reqierd */  
  471.     /* 后处理模块初始化,如果需要跟踪校正后处理 */  
  472.     if(bt_corr && MY_STRICMP(bt_corr,"PostProcRes")!=0 && !btpp_name)  
  473.     {  
  474.         btpp_name = bt_corr;  
  475.         if(MY_STRICMP(btpp_name,"none")!=0)bt_corr = "PostProcRes";  
  476.     }  
  477.   
  478.     {/* set default parameters for one processing */ /* 设置默认参数为一个处理 */  
  479.         if(!bt_corr) bt_corr = "none";  
  480.         if(!fg_name) fg_name = FGDetector_Modules[0].nickname;  
  481.         if(!bd_name) bd_name = BlobDetector_Modules[0].nickname;  
  482.         if(!bt_name) bt_name = BlobTracker_Modules[0].nickname;  
  483.         if(!btpp_name) btpp_name = BlobTrackPostProc_Modules[0].nickname;  
  484.         if(!bta_name) bta_name = BlobTrackAnalysis_Modules[0].nickname;  
  485.         if(!scale_name) scale_name = "1";  
  486.     }  
  487.   
  488.     if(scale_name)   
  489.         scale = (float)atof(scale_name);  
  490.     for(pFGModule=FGDetector_Modules;pFGModule->nickname;++pFGModule)  
  491.         if( fg_name && MY_STRICMP(fg_name,pFGModule->nickname)==0 ) break;  
  492.     for(pBDModule=BlobDetector_Modules;pBDModule->nickname;++pBDModule)  
  493.         if( bd_name && MY_STRICMP(bd_name,pBDModule->nickname)==0 ) break;  
  494.     for(pBTModule=BlobTracker_Modules;pBTModule->nickname;++pBTModule)  
  495.         if( bt_name && MY_STRICMP(bt_name,pBTModule->nickname)==0 ) break;  
  496.     for(pBTPostProcModule=BlobTrackPostProc_Modules;pBTPostProcModule->nickname;++pBTPostProcModule)  
  497.         if( btpp_name && MY_STRICMP(btpp_name,pBTPostProcModule->nickname)==0 ) break;  
  498.     for(pBTAnalysisModule=BlobTrackAnalysis_Modules;pBTAnalysisModule->nickname;++pBTAnalysisModule)  
  499.         if( bta_name && MY_STRICMP(bta_name,pBTAnalysisModule->nickname)==0 ) break;  
  500.   
  501.     /* create source video */ /* 创建视频源 */  
  502.     if(avi_name)   
  503.       pCap = cvCaptureFromFile(avi_name);  
  504.       //pCap = cvCreateCameraCapture(-1);  
  505.   
  506.     if(pCap==NULL)  
  507.     {  
  508.         printf("Can't open %s file\n",avi_name);  
  509.         return -1;  
  510.     }  
  511.   
  512.   
  513.     {/* display parameters */ /* 显示参数 */  
  514.         int i;  
  515.         FILE* log = log_name?fopen(log_name,"at"):NULL;  
  516.         if(log)  
  517.         {/* print to log file */ /* 打印到log文件 */  
  518.             fprintf(log,"\n=== Blob Tracking pipline in processing mode===\n");  
  519.             if(avi_name)  
  520.             {  
  521.                 fprintf(log,"AVIFile: %s\n",avi_name);  
  522.             }  
  523.             fprintf(log,"FGDetector:   %s\n", pFGModule->nickname);  
  524.             fprintf(log,"BlobDetector: %s\n", pBDModule->nickname);  
  525.             fprintf(log,"BlobTracker:  %s\n", pBTModule->nickname);  
  526.             fprintf(log,"BlobTrackPostProc:  %s\n", pBTPostProcModule->nickname);  
  527.             fprintf(log,"BlobCorrection:  %s\n", bt_corr);  
  528.             fprintf(log,"Blob Trajectory Generator:  %s (%s)\n",  
  529.                 pBTGenModule?pBTGenModule->nickname:"None",  
  530.                 track_name?track_name:"none");  
  531.             fprintf(log,"BlobTrackAnalysis:  %s\n", pBTAnalysisModule->nickname);  
  532.             fclose(log);  
  533.         }  
  534.   
  535.         printf("\n=== Blob Tracking pipline in %s mode===\n","processing");  
  536.         if(yml_name)  
  537.         {  
  538.             printf("ConfigFile: %s\n",yml_name);  
  539.             printf("BG: %s\n",yml_video_names[0]);  
  540.             printf("FG: ");  
  541.             for(i=1;i<(yml_video_num);++i){printf(yml_video_names[i]);if((i+1)"|");};  
  542.             printf("\n");  
  543.         }  
  544.         if(avi_name)  
  545.         {  
  546.             printf("AVIFile: %s\n",avi_name);  
  547.         }  
  548.         printf("FGDetector:   %s\n", pFGModule->nickname);  
  549.         printf("BlobDetector: %s\n", pBDModule->nickname);  
  550.         printf("BlobTracker:  %s\n", pBTModule->nickname);  
  551.         printf("BlobTrackPostProc:  %s\n", pBTPostProcModule->nickname);  
  552.         printf("BlobCorrection:  %s\n", bt_corr);  
  553.         printf("Blob Trajectory Generator:  %s (%s)\n",  
  554.             pBTGenModule?pBTGenModule->nickname:"None",  
  555.             track_name?track_name:"none");  
  556.         printf("BlobTrackAnalysis:  %s\n", pBTAnalysisModule->nickname);  
  557.     }/* display parameters */ /* 显示参数 */  
  558.   
  559.     {   /* create autotracker module and its components*/ /* 创建自动跟踪模块及其组件 */  
  560.         param.FGTrainFrames = FGTrainFrames?atoi(FGTrainFrames):0;  
  561.   
  562.         /* Create FG Detection module */ /* 创建FG(前景)检测模块 */  
  563.         param.pFG = pFGModule->create();  
  564.         if(!param.pFG)  
  565.             puts("Can not create FGDetector module");  
  566.         param.pFG->SetNickName(pFGModule->nickname);  
  567.         set_params(argc, argv, param.pFG, "fg", pFGModule->nickname);  
  568.   
  569.         /* Create Blob Entrance Detection module */ /* 创建输入块(Blob Entrance)检测模块 */  
  570.         param.pBD = pBDModule->create();  
  571.         if(!param.pBD)  
  572.             puts("Can not create BlobDetector module");  
  573.         param.pBD->SetNickName(pBDModule->nickname);  
  574.         set_params(argc, argv, param.pBD, "bd", pBDModule->nickname);  
  575.   
  576.         /* Create blob tracker module */ /* 创建块跟踪(blob tracker)模块 */  
  577.         param.pBT = pBTModule->create();  
  578.         if(!param.pBT)  
  579.             puts("Can not create BlobTracker module");  
  580.         param.pBT->SetNickName(pBTModule->nickname);  
  581.         set_params(argc, argv, param.pBT, "bt", pBTModule->nickname);  
  582.   
  583.         /* create blob trajectory generation module */ /* 创建块轨迹生成模块 */  
  584.         param.pBTGen = NULL;  
  585.         if(pBTGenModule && track_name && pBTGenModule->create)  
  586.         {  
  587.             param.pBTGen = pBTGenModule->create();  
  588.             param.pBTGen->SetFileName(track_name);  
  589.         }  
  590.         if(param.pBTGen)  
  591.         {  
  592.             param.pBTGen->SetNickName(pBTGenModule->nickname);  
  593.             set_params(argc, argv, param.pBTGen, "btgen", pBTGenModule->nickname);  
  594.         }  
  595.   
  596.         /* create blob trajectory post processing module */ /* 创建BLOB轨迹后处理模块 */  
  597.         param.pBTPP = NULL;  
  598.         if(pBTPostProcModule && pBTPostProcModule->create)  
  599.         {  
  600.             param.pBTPP = pBTPostProcModule->create();  
  601.         }  
  602.         if(param.pBTPP)  
  603.         {  
  604.             param.pBTPP->SetNickName(pBTPostProcModule->nickname);  
  605.             set_params(argc, argv, param.pBTPP, "btpp", pBTPostProcModule->nickname);  
  606.         }  
  607.   
  608.         param.UsePPData = (bt_corr && MY_STRICMP(bt_corr,"PostProcRes")==0);  
  609.   
  610.         /* create blob trajectory analysis module */ /* 创建BLOB轨迹分析模块 */  
  611.         param.pBTA = NULL;  
  612.         if(pBTAnalysisModule && pBTAnalysisModule->create)  
  613.         {  
  614.             param.pBTA = pBTAnalysisModule->create();  
  615.             param.pBTA->SetFileName(bta_data_name);  
  616.         }  
  617.         if(param.pBTA)  
  618.         {  
  619.             param.pBTA->SetNickName(pBTAnalysisModule->nickname);  
  620.             set_params(argc, argv, param.pBTA, "bta", pBTAnalysisModule->nickname);  
  621.         }  
  622.   
  623.         /* create whole pipline */ /* 创建整个pipline */  
  624.         pTracker = cvCreateBlobTrackerAuto1(¶m);  
  625.         if(!pTracker)  
  626.             puts("Can not create BlobTrackerAuto");  
  627.     }  
  628.       
  629.     { /* load states of each module from state file */ /* 从状态文件中加载每个模块的状态 */  
  630.         CvFileStorage* fs = NULL;  
  631.         if(loadstate_name)   
  632.             fs=cvOpenFileStorage(loadstate_name,NULL,CV_STORAGE_READ);  
  633.         if(fs)  
  634.         {  
  635.             printf("Load states for modules...\n");  
  636.             if(param.pBT)  
  637.             {  
  638.                 CvFileNode* fn = cvGetFileNodeByName(fs,NULL,"BlobTracker");  
  639.                 param.pBT->LoadState(fs,fn);  
  640.             }  
  641.               
  642.             if(param.pBTA)  
  643.             {  
  644.                 CvFileNode* fn = cvGetFileNodeByName(fs,NULL,"BlobTrackAnalyser");  
  645.                 param.pBTA->LoadState(fs,fn);  
  646.             }  
  647.   
  648.             if(pTracker)  
  649.             {  
  650.                 CvFileNode* fn = cvGetFileNodeByName(fs,NULL,"BlobTrackerAuto");  
  651.                 pTracker->LoadState(fs,fn);  
  652.             }  
  653.   
  654.             cvReleaseFileStorage(&fs);  
  655.             printf("... Modules states loaded\n");  
  656.         }  
  657.     }/* load states of each module */ /* 加载每个模块的状态 */  
  658.   
  659.     {/* print modules parameters */ /* 打印模块参数 */  
  660.         struct DefMMM  
  661.         {  
  662.             CvVSModule* pM;  
  663.             char* name;  
  664.         } Modules[] = {  
  665.             {(CvVSModule*)param.pFG,"FGdetector"},  
  666.             {(CvVSModule*)param.pBD,"BlobDetector"},  
  667.             {(CvVSModule*)param.pBT,"BlobTracker"},  
  668.             {(CvVSModule*)param.pBTGen,"TrackGen"},  
  669.             {(CvVSModule*)param.pBTPP,"PostProcessing"},  
  670.             {(CvVSModule*)param.pBTA,"TrackAnalysis"},  
  671.             {NULL,NULL}  
  672.         };  
  673.         int     i;  
  674.         for(i=0;Modules[i].name;++i)  
  675.         {  
  676.             if(Modules[i].pM)  
  677.                 print_params(Modules[i].pM,Modules[i].name,log_name);  
  678.         }  
  679.     }/* print modules parameters */ /* 打印模块参数 */  
  680.   
  681.     /* run pipeline */ /* 运行传递途径 */  
  682.     RunBlobTrackingAuto( pCap, pTracker, fgavi_name, btavi_name );  
  683.           
  684.     {/* save state and release modulesSD */ /* 保存状态和释放modulesSD */  
  685.         CvFileStorage* fs = NULL;  
  686.         if(savestate_name)  
  687.         {  
  688.             fs=cvOpenFileStorage(savestate_name,NULL,CV_STORAGE_WRITE);  
  689.         }  
  690.         if(fs)  
  691.         {  
  692.             cvStartWriteStruct(fs,"BlobTracker",CV_NODE_MAP);  
  693.             if(param.pBT)param.pBT->SaveState(fs);  
  694.             cvEndWriteStruct(fs);  
  695.             cvStartWriteStruct(fs,"BlobTrackerAuto",CV_NODE_MAP);  
  696.             if(pTracker)pTracker->SaveState(fs);  
  697.             cvEndWriteStruct(fs);  
  698.             cvStartWriteStruct(fs,"BlobTrackAnalyser",CV_NODE_MAP);  
  699.             if(param.pBTA)param.pBTA->SaveState(fs);  
  700.             cvEndWriteStruct(fs);  
  701.             cvReleaseFileStorage(&fs);  
  702.         }  
  703.         if(param.pBT)cvReleaseBlobTracker(¶m.pBT);  
  704.         if(param.pBD)cvReleaseBlobDetector(¶m.pBD);  
  705.         if(param.pBTGen)cvReleaseBlobTrackGen(¶m.pBTGen);  
  706.         if(param.pBTA)cvReleaseBlobTrackAnalysis(¶m.pBTA);  
  707.         if(param.pFG)cvReleaseFGDetector(¶m.pFG);  
  708.         if(pTracker)cvReleaseBlobTrackerAuto(&pTracker);  
  709.     }/* save state and release modules*/ /* 保存状态和释放模块 */  
  710.   
  711.     if(pCap)  
  712.         cvReleaseCapture(&pCap);  
  713.   
  714.     return 0;  
  715. }/* main */  

你可能感兴趣的:(目标跟踪之:blob_tracking 跟踪代码中文注释)