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

OpenCV

开发平台:

Visual C++

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