blobtrack.cpp
上传用户:banwdc
上传日期:2016-06-25
资源大小:2871k
文件大小:26k
源码类别:

OpenCV

开发平台:

Visual C++

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