RunTrack.cpp
资源名称:VIS.zip [点击查看]
上传用户:sdgangtie
上传日期:2020-03-07
资源大小:7324k
文件大小:7k
源码类别:
数值算法/人工智能
开发平台:
Visual C++
- #include "StdAfx.h"
- #include "RunTrack.h"
- RunTrack::RunTrack(void)
- : m_pFilePath(NULL)
- , m_pCapture(NULL)
- , m_pImage(NULL)
- , m_pTracker(NULL)
- , m_FrameCount(0)
- , m_State(0)
- , m_Style(0)
- {
- m_Param.FGTrainFrames = 0; m_Param.pFG = NULL; m_Param.pBD = NULL; m_Param.pBT = NULL; m_Param.pBTGen = NULL; m_Param.pBTA = NULL; m_Param.pBTPP = NULL; m_Param.UsePPData = 0;
- }
- RunTrack::~RunTrack(void)
- {
- ReleaseParam();
- if(m_pImage) cvReleaseImage(&m_pImage);
- if(m_pCapture) cvReleaseCapture(&m_pCapture);
- if(m_pTracker) cvReleaseBlobTrackerAuto(&m_pTracker);
- }
- int RunTrack::Init(char* pFilePath,int _style, CvParamSet m_ParamSet)
- {
- m_pFilePath = pFilePath;
- if(m_pFilePath == NULL)
- return 0;
- if(m_pCapture)
- cvReleaseCapture(&m_pCapture);
- m_pCapture = cvCaptureFromFile(m_pFilePath);
- m_State = 0;
- m_Style = _style;
- if (m_Style > 0)
- {
- if (m_pTracker)
- {
- cvReleaseBlobTrackerAuto(&m_pTracker);
- }
- ResetParam(m_ParamSet);
- if (m_Param.pBTGen&&m_Param.UsePPData)
- {
- sprintf(m_GenFile,"%s.xml",m_pFilePath);
- m_Param.pBTGen->SetFileName(m_GenFile);
- }
- m_pTracker = new BlobTrackerAuto(&m_Param);
- }
- m_FrameCount = 0;
- return (int)cvGetCaptureProperty(m_pCapture,CV_CAP_PROP_FRAME_COUNT);
- }
- void RunTrack::Run(void)
- {
- m_State = 1;
- }
- void RunTrack::Pause(void)
- {
- m_State = 2;
- }
- void RunTrack::Stop(void)
- {
- m_State = 0;
- m_FrameCount = 0;
- }
- int RunTrack::GetState(void)
- {
- return m_State;
- }
- IplImage* RunTrack::GetNextFrame(void)
- {
- if(m_State == 0)
- {
- if (m_pImage) cvReleaseImage(&m_pImage);
- m_pImage = NULL;
- }
- if(m_State == 1)
- {
- if(m_pCapture)
- {
- IplImage* pImg = cvQueryFrame(m_pCapture);
- if(pImg != NULL) { m_FrameCount++;
- if (m_Style == 0)
- {
- if (m_pImage) cvReleaseImage(&m_pImage);
- m_pImage = cvCloneImage(pImg);
- }
- else if(m_Style == 1)
- {
- if(m_pTracker) m_pTracker->Process(pImg);
- if (m_pImage) cvReleaseImage(&m_pImage);
- m_pImage = cvCloneImage(m_pTracker->GetFGMask());
- }
- else if (m_Style == 2)
- {
- if(m_pTracker) m_pTracker->Process(pImg);
- if (m_pImage) cvReleaseImage(&m_pImage);
- m_pImage = ProcessResult(pImg, m_pTracker);
- }
- }
- else
- {
- m_State = 0;
- m_FrameCount = 0;
- }
- }
- }
- return m_pImage;
- }
- int RunTrack::GetFrameCount(void)
- {
- return m_FrameCount;
- }
- void RunTrack::SetPosition(int Pos)
- {
- m_FrameCount = Pos;
- cvSetCaptureProperty(m_pCapture,CV_CAP_PROP_POS_FRAMES,Pos);
- }
- CString RunTrack::GetTimeFre(void)
- {
- CString retval;
- if (m_State != 0)
- {
- double AllFrames = cvGetCaptureProperty(m_pCapture,CV_CAP_PROP_FRAME_COUNT);
- double NowFrames = cvGetCaptureProperty(m_pCapture,CV_CAP_PROP_POS_FRAMES);
- retval.Format(TEXT("%d / %d"),(int)NowFrames,(int)AllFrames);
- }
- return retval;
- }
- IplImage* RunTrack::ProcessResult(IplImage* pImage, CvBlobTrackerAuto* m_pTracker)
- {
- char str[1024];
- int line_type = CV_AA;
- CvFont font;
- static IplImage* pI;
- if (pI) cvReleaseImage(&pI);
- pI = cvCloneImage(pImage);
- cvInitFont(&font, CV_FONT_HERSHEY_PLAIN, 1.2, 1.2, 0, 2, line_type);
- for(int i=m_pTracker->GetBlobNum(); i>0; i--)
- {
- CvSize TextSize;
- CvBlob* pB = m_pTracker->GetBlob(i-1);
- CvPoint p = cvPoint(cvRound(pB->x*256),cvRound(pB->y*256));
- CvSize s = cvSize(MAX(1,cvRound(CV_BLOB_RX(pB)*256)), MAX(1,cvRound(CV_BLOB_RY(pB)*256)));
- int c = cvRound(255*m_pTracker->GetState(CV_BLOB_ID(pB)));
- cvEllipse(pI, p, s, 0, 0, 360, CV_RGB(c,255-c,0), 2, CV_AA, 8);
- p.x >>= 8; p.y >>= 8; s.width >>= 8; s.height >>= 8;
- sprintf(str,"%d",CV_BLOB_ID(pB));
- cvGetTextSize( str, &font, &TextSize, NULL );
- p.y -= s.height;
- cvPutText( pI, str, p, &font, CV_RGB(0,255,255));
- {
- const char* pS = m_pTracker->GetStateDesc(CV_BLOB_ID(pB));
- if(pS)
- {
- char* pStr = strdup(pS);
- char* pStrFree = pStr;
- while (pStr && strlen(pStr) > 0)
- {
- char* str_next = strchr(pStr,'n');
- if(str_next)
- {
- str_next[0] = 0; str_next++;
- }
- p.y += TextSize.height+1;
- cvPutText( pI, pStr, p, &font, CV_RGB(0,255,255));
- pStr = str_next;
- }
- free(pStrFree);
- }
- }
- }
- BlobTrackerAuto* pTracker = (BlobTrackerAuto*)m_pTracker;
- BlobTrackGen *pGen= (BlobTrackGen*)pTracker->GetTrackGen();
- if (pGen)
- {
- CvBlobSeq *m_TrackList = pGen->GetTrackList();
- for(int i=m_TrackList->GetBlobNum(); i>0; i--) { DefBlobTrack* pTrack = (DefBlobTrack*)m_TrackList->GetBlob(i-1); CvBlobSeq* pSeq = pTrack->pSeq; if (pTrack->FrameLast+1 < m_FrameCount)
- continue;
- for(int j=0; j<pSeq->GetBlobNum()-1; j++) { CvBlob* pB1 = pSeq->GetBlob(j); CvBlob* pB2 = pSeq->GetBlob(j+1); CvPoint p1 =cvPoint(cvRound(pB1->x*256),cvRound(pB1->y*256)); CvPoint p2 =cvPoint(cvRound(pB2->x*256),cvRound(pB2->y*256)); cvLine(pI, p1, p2, CV_RGB(0,255,255), 2, CV_AA, 8 ); }
- }
- }
- return cvCloneImage(pI);
- }
- void RunTrack::ReleaseParam(void)
- {
- if(m_Param.pFG) cvReleaseFGDetector(&m_Param.pFG);m_Param.pFG = NULL; if(m_Param.pBD) cvReleaseBlobDetector(&m_Param.pBD);m_Param.pBD = NULL; if(m_Param.pBT) cvReleaseBlobTracker(&m_Param.pBT);m_Param.pBT = NULL; if(m_Param.pBTGen) cvReleaseBlobTrackGen(&m_Param.pBTGen);m_Param.pBTGen = NULL; if(m_Param.pBTPP) cvReleaseBlobTrackPostProc(&m_Param.pBTPP);m_Param.pBTPP = NULL; if(m_Param.pBTA) cvReleaseBlobTrackAnalysis(&m_Param.pBTA);m_Param.pBTA = NULL;
- }
- void RunTrack::ResetParam(CvParamSet m_ParamSet) { ReleaseParam(); if (m_Style > 0)
- { m_Param.FGTrainFrames = 0; if (m_ParamSet.m_FG == 1)
- {
- m_Param.pFG = cvCreateFGDetectorBoa();
- if (m_Param.pFG)
- {
- m_Param.pFG->SetParam("Threshold",m_ParamSet.m_Threshold);
- }
- }
- else if (m_ParamSet.m_FG == 2)
- m_Param.pFG = cvCreateFGDetectorBase(CV_BG_MODEL_FGD, NULL);
- else if (m_ParamSet.m_FG == 3)
- m_Param.pFG = cvCreateFGDetectorBase(CV_BG_MODEL_MOG, NULL);
- else
- m_Param.pFG = NULL;
- if (m_Style > 1)
- {
- m_Param.pBD = cvCreateBlobDetectorBoa();
- if (m_ParamSet.m_BT == 1)
- m_Param.pBT = cvCreateBlobTrackerCC();
- else if (m_ParamSet.m_BT == 2)
- m_Param.pBT = cvCreateBlobTrackerMSFG();
- else if (m_ParamSet.m_BT == 3)
- m_Param.pBT = cvCreateBlobTrackerMSPF();
- else
- m_Param.pBT = NULL;
- if (m_ParamSet.m_Check)
- {
- m_Param.pBTGen = cvCreateModuleBlobTrackGenBoa();
- m_Param.UsePPData = 1;
- if (m_ParamSet.m_BPP == 1)
- m_Param.pBTPP = cvCreateModuleBlobTrackPostProcKalman();
- else if (m_ParamSet.m_BPP == 2)
- m_Param.pBTPP = cvCreateModuleBlobTrackPostProcTimeAverRect();
- else if (m_ParamSet.m_BPP == 3)
- m_Param.pBTPP = cvCreateModuleBlobTrackPostProcTimeAverExp();
- else
- m_Param.pBTPP = NULL;
- if (m_ParamSet.m_BTA == 1)
- m_Param.pBTA = cvCreateModuleBlobTrackAnalysisTrackDist();
- else if (m_ParamSet.m_BTA == 2)
- m_Param.pBTA = cvCreateModuleBlobTrackAnalysisHistP();
- else if (m_ParamSet.m_BTA == 3)
- m_Param.pBTA = cvCreateModuleBlobTrackAnalysisHistPV();
- else if (m_ParamSet.m_BTA == 4)
- m_Param.pBTA = cvCreateModuleBlobTrackAnalysisHistPVS();
- else
- m_Param.pBTA = NULL;
- }
- else
- {
- m_Param.pBTGen = NULL;
- m_Param.UsePPData = 0; m_Param.pBTPP = NULL; m_Param.pBTA = NULL;
- }
- }
- } }