BlobTracker.cpp
资源名称:VIS.zip [点击查看]
上传用户:sdgangtie
上传日期:2020-03-07
资源大小:7324k
文件大小:24k
源码类别:
数值算法/人工智能
开发平台:
Visual C++
- #include "StdAfx.h"
- #include "BlobTracker.h"
- /************************************************************************/
- /* 前景检测模块——背景差部分 */
- /************************************************************************/
- // 仿射变换矩阵,主要用于前景检测模块中计算全局运动和轨迹生成模块中轨迹校正
- static CvMat *T = NULL;
- FGDetector::FGDetector(void)
- : m_FrameCount(0)
- , m_Transform(1)
- , m_Threshold(20.0)
- , m_pFramePre(NULL)
- , m_pFGMask(NULL)
- , m_pTransPre(NULL)
- {
- if(m_Transform) T = cvCreateMat( 2, 3, CV_64FC1 );
- else T = cvCreateMat( 3, 3, CV_64FC1 );
- cvZero(T);
- AddParam("Threshold",&m_Threshold);
- AddParam("Transform",&m_Transform);
- SetModuleName("FGDetector");
- }
- FGDetector::~FGDetector(void)
- {
- Release();
- }
- IplImage* FGDetector::GetMask(void)
- {
- return m_pFGMask;
- }
- void FGDetector::Process(IplImage* pFrame)
- {
- if(m_FrameCount == 0)
- {
- m_pFGMask = cvCreateImage(cvSize( pFrame->width, pFrame->height ), IPL_DEPTH_8U, 1);
- m_pTransPre = cvCreateImage(cvSize( pFrame->width, pFrame->height ), IPL_DEPTH_8U, 3);
- m_pFramePre = cvCloneImage(pFrame);
- }
- else if(m_FrameCount == 1)
- {
- GetMaskBySIFT(m_pFramePre, pFrame, m_Transform, T);
- if(m_Transform) cvWarpAffine(m_pFramePre, m_pTransPre, T, CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS, cvScalarAll(0)); else cvWarpPerspective(m_pFramePre, m_pTransPre, T, CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS, cvScalarAll(0));
- if(m_pFramePre) cvCopyImage(pFrame, m_pFramePre);
- else m_pFramePre = cvCloneImage(pFrame);
- }
- else
- {
- GetMaskBySIFT(m_pFramePre, pFrame, m_Transform, T);
- IplImage* m_pTransAft = cvCreateImage(cvSize( pFrame->width, pFrame->height ), IPL_DEPTH_8U, 3);
- if(m_Transform) cvWarpAffine(pFrame, m_pTransAft, T, CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS + CV_WARP_INVERSE_MAP,cvScalarAll(0)); else cvWarpPerspective(pFrame, m_pTransAft, T, CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS + CV_WARP_INVERSE_MAP, cvScalarAll(0));
- IplImage* diff1 = cvCreateImage(cvGetSize(pFrame), IPL_DEPTH_8U, 3); IplImage* diff2 = cvCreateImage(cvGetSize(pFrame), IPL_DEPTH_8U, 3); cvAbsDiff(m_pTransPre, m_pFramePre, diff1); cvAbsDiff(m_pTransAft, m_pFramePre, diff2); cvSmooth(diff1, diff1, CV_GAUSSIAN, 5, 0, 0, 0); cvSmooth(diff2, diff2, CV_GAUSSIAN, 5, 0, 0, 0); IplImage* gray1 = cvCreateImage(cvGetSize(pFrame), IPL_DEPTH_8U, 1); IplImage* gray2 = cvCreateImage(cvGetSize(pFrame), IPL_DEPTH_8U, 1); cvCvtColor(diff1, gray1, CV_BGR2GRAY); cvCvtColor(diff2, gray2, CV_BGR2GRAY); IplImage* mask1 = cvCreateImage(cvGetSize(pFrame), IPL_DEPTH_8U, 1); IplImage* mask2 = cvCreateImage(cvGetSize(pFrame), IPL_DEPTH_8U, 1); cvThreshold(gray1, mask1, m_Threshold, 255.0, CV_THRESH_BINARY); cvThreshold(gray2, mask2, m_Threshold, 255.0, CV_THRESH_BINARY); cvErode(mask1, mask1, 0, 2); cvDilate(mask1, mask1, 0, 2); cvErode(mask2, mask2, 0, 2); cvDilate(mask2, mask2, 0, 2);
- cvAnd(mask1, mask2, m_pFGMask, 0); if(m_Transform) cvWarpAffine(m_pFramePre, m_pTransPre, T, CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS, cvScalarAll(0)); else cvWarpPerspective(m_pFramePre, m_pTransPre, T, CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS, cvScalarAll(0));
- if(m_pFramePre) cvCopyImage(pFrame, m_pFramePre);
- else m_pFramePre = cvCloneImage(pFrame); cvReleaseImage(&diff1); cvReleaseImage(&diff2); cvReleaseImage(&gray1); cvReleaseImage(&gray2); cvReleaseImage(&mask1); cvReleaseImage(&mask2); cvReleaseImage(&m_pTransAft);
- }
- m_FrameCount++;
- }
- void FGDetector::Release(void)
- {
- if(m_pFramePre) cvReleaseImage(&m_pFramePre);
- if(m_pFGMask) cvReleaseImage(&m_pFGMask);
- if(m_pTransPre) cvReleaseImage(&m_pTransPre);
- }
- /************************************************************************/
- /* 对象检测模块——连通分量方法 */
- /************************************************************************/
- static int CompareContour(const void* a, const void* b, void* ) { float dx,dy; float h,w,ht,wt; CvPoint2D32f pa,pb; CvRect ra,rb; CvSeq* pCA = *(CvSeq**)a; CvSeq* pCB = *(CvSeq**)b; ra = ((CvContour*)pCA)->rect; rb = ((CvContour*)pCB)->rect; pa.x = ra.x + ra.width*0.5f; pa.y = ra.y + ra.height*0.5f; pb.x = rb.x + rb.width*0.5f; pb.y = rb.y + rb.height*0.5f; w = (ra.width+rb.width)*0.5f; h = (ra.height+rb.height)*0.5f; dx = (float)(fabs(pa.x - pb.x)-w); dy = (float)(fabs(pa.y - pb.y)-h); wt = MAX(ra.width, rb.width)*0.3f; // 宽度距离限制 ht = MAX(ra.height, rb.height)*0.3f; // 高度距离限制 return (dx < wt && dy < ht); }
- BlobDetector::BlobDetector(void)
- : m_HMin(0.05)
- , m_HMax(0.95)
- , m_WMin(0.05)
- , m_WMax(0.95)
- {
- AddParam("HMin", &m_HMin); AddParam("HMax", &m_HMax); AddParam("WMin", &m_WMin); AddParam("WMax", &m_WMax); SetParam("DebugWnd",0); SetModuleName("BD Detector");
- }
- BlobDetector::~BlobDetector(void)
- {
- }
- void BlobDetector::cvFindBlobsByCCClasters(IplImage* pFG, CvBlobSeq* pBlobs, CvMemStorage* storage)
- {
- IplImage* pIB = NULL; CvSeq* cnt = NULL; CvSeq* cnt_list = cvCreateSeq(0, sizeof(CvSeq), sizeof(CvSeq*), storage); CvSeq* clasters = NULL; int claster_cur, claster_num; pIB = cvCloneImage(pFG); cvThreshold(pIB, pIB, 128, 255, CV_THRESH_BINARY); cvFindContours(pIB, storage, &cnt, sizeof(CvContour), CV_RETR_EXTERNAL); cvReleaseImage(&pIB); for(; cnt; cnt=cnt->h_next) { cvSeqPush(cnt_list, &cnt); } claster_num = cvSeqPartition(cnt_list, storage, &clasters, CompareContour, NULL); for(claster_cur=0; claster_cur<claster_num; claster_cur++) { CvBlob NewBlob; double M00,X,Y,XX,YY; CvMoments m; CvRect rect_res = cvRect(-1,-1,-1,-1); CvMat mat; for(int cnt_cur=0; cnt_cur<clasters->total; ++cnt_cur) { CvRect rect; CvSeq* cnt; int k = *(int*)cvGetSeqElem(clasters, cnt_cur); if(k!=claster_cur) continue; cnt = *(CvSeq**)cvGetSeqElem(cnt_list, cnt_cur); rect = ((CvContour*)cnt)->rect; if(rect_res.height<0) { rect_res = rect; } else { int x0,x1,y0,y1; x0 = MIN(rect_res.x, rect.x); y0 = MIN(rect_res.y, rect.y); x1 = MAX(rect_res.x+rect_res.width, rect.x+rect.width); y1 = MAX(rect_res.y+rect_res.height, rect.y+rect.height); rect_res.x = x0; rect_res.y = y0; rect_res.width = x1-x0; rect_res.height = y1-y0; } } if(rect_res.height < 1 || rect_res.width < 1) { X = 0; Y = 0; XX = 0; YY = 0; } else { cvMoments(cvGetSubRect(pFG,&mat,rect_res), &m, 0); M00 = cvGetSpatialMoment(&m, 0, 0); if(M00 <= 0 ) continue; X = cvGetSpatialMoment(&m, 1, 0)/M00; Y = cvGetSpatialMoment(&m, 0, 1)/M00; XX = (cvGetSpatialMoment(&m, 2, 0)/M00) - X*X; YY = (cvGetSpatialMoment(&m, 0, 2)/M00) - Y*Y; } NewBlob = cvBlob(rect_res.x+(float)X, rect_res.y+(float)Y, (float)(4*sqrt(XX)), (float)(4*sqrt(YY))); pBlobs->AddBlob(&NewBlob); }
- if(m_Wnd) { // Debug info: IplImage* pI = cvCreateImage(cvSize(pFG->width,pFG->height),IPL_DEPTH_8U,3); cvZero(pI); for(claster_cur=0; claster_cur<claster_num; ++claster_cur) { int cnt_cur; CvScalar color = CV_RGB(rand()%256,rand()%256,rand()%256); for(cnt_cur=0; cnt_cur<clasters->total; ++cnt_cur) { CvSeq* cnt; int k = *(int*)cvGetSeqElem( clasters, cnt_cur ); if(k!=claster_cur) continue; cnt = *(CvSeq**)cvGetSeqElem( cnt_list, cnt_cur ); cvDrawContours( pI, cnt, color, color, 0, CV_FILLED, 8); } CvBlob* pB = pBlobs->GetBlob(claster_cur); int x = cvRound(CV_BLOB_RX(pB)), y = cvRound(CV_BLOB_RY(pB)); cvEllipse( pI, cvPointFrom32f(CV_BLOB_CENTER(pB)), cvSize(MAX(1,x), MAX(1,y)), 0, 0, 360, color, 1 ); } cvNamedWindow( "Clusters", 0); cvResizeWindow( "Clusters", pI->width, pI->height) ; cvShowImage( "Clusters",pI ); cvReleaseImage(&pI); } /* Debug info. */
- }
- int BlobDetector::DetectNewBlob(IplImage* pImg, IplImage* pFGMask, CvBlobSeq* pNewBlobList, CvBlobSeq* pOldBlobList)
- {
- int result = 0; CvSize S = cvSize(pFGMask->width,pFGMask->height); CvBlobSeq Blobs; CvMemStorage* storage = cvCreateMemStorage(); cvFindBlobsByCCClasters(pFGMask, &Blobs, storage ); for(int i=Blobs.GetBlobNum(); i>0; i--) { CvBlob* pB = Blobs.GetBlob(i-1); if(pB->h < pFGMask->height*m_HMin || pB->w < pFGMask->width*m_WMin) { Blobs.DelBlob(i-1); continue; } if(pB->h > pFGMask->height*m_HMax || pB->w > pFGMask->width*m_WMax) { Blobs.DelBlob(i-1); continue; } if(MIN(pB->x,pFGMask->width-pB->x) < CV_BLOB_RX(pB) || MIN(pB->y,pFGMask->height-pB->y) < CV_BLOB_RY(pB)) { Blobs.DelBlob(i-1); continue; } if(pOldBlobList) { for(int j=pOldBlobList->GetBlobNum(); j>0; j--) { CvBlob* pBOld = pOldBlobList->GetBlob(j-1); if((fabs(pBOld->x-pB->x) < (CV_BLOB_RX(pBOld)+CV_BLOB_RX(pB))) && (fabs(pBOld->y-pB->y) < (CV_BLOB_RY(pBOld)+CV_BLOB_RY(pB)))) { Blobs.DelBlob(i-1); break; } } } } { int N = Blobs.GetBlobNum(); for(int i=1; i<N; i++) { for(int j=i; j>0; j--) { CvBlob temp; float AreaP, AreaN; CvBlob* pP = Blobs.GetBlob(j-1); CvBlob* pN = Blobs.GetBlob(j); AreaP = CV_BLOB_WX(pP)*CV_BLOB_WY(pP); AreaN = CV_BLOB_WX(pN)*CV_BLOB_WY(pN); if(AreaN < AreaP)break; temp = pN[0]; pN[0] = pP[0]; pP[0] = temp; } } for(int i=0; i<MIN(N,10); i++) { pNewBlobList->AddBlob(Blobs.GetBlob(i)); result = 1; } } #if 0 { // 嵌入的人检测模块 IplImage *pImage = cvCloneImage(pImg); CvBlobSeq NewBlobLists; PeopleDetector(pImage, &NewBlobLists); for( int i = NewBlobLists.GetBlobNum(); i > 0 ; i-- ) { CvBlob *pB = NewBlobLists.GetBlob(i-1); pB->x = pB->x+pB->w*0.5; pB->y = pB->y+pB->h*0.5; CvPoint p1 = cvPoint(cvRound(pB->x-pB->w*0.5), cvRound(pB->y-pB->h*0.5)); CvPoint p2 = cvPoint(cvRound(pB->x+pB->w*0.5), cvRound(pB->y+pB->h*0.5)); CvScalar color = CV_RGB(0,255,0); cvRectangle(pImage,p1,p2,color); if(pOldBlobList) { for(int j=pOldBlobList->GetBlobNum(); j>0; j--) { CvBlob* pBOld = pOldBlobList->GetBlob(j-1); if((fabs(pBOld->x-pB->x) < (CV_BLOB_RX(pBOld)+CV_BLOB_RX(pB))) && (fabs(pBOld->y-pB->y) < (CV_BLOB_RY(pBOld)+CV_BLOB_RY(pB)))) { NewBlobLists.DelBlob(i-1); break; } } } } for(int i=0; i<MIN(NewBlobLists.GetBlobNum(),10); i++) { CvBlob *pB = NewBlobLists.GetBlob(i-1); pNewBlobList->AddBlob(pB); result = 1; } cvNamedWindow("People", 0); cvResizeWindow( "People", pImage->width, pImage->height) ; cvShowImage("People", pImage); cvReleaseImage(&pImage); } #endif cvReleaseMemStorage(&storage);
- return result;
- }
- void BlobDetector::Release(void)
- {
- delete this;
- }
- /************************************************************************/
- /* 轨迹生成模块——对象特征方法 */
- /************************************************************************/
- BlobTrackGen::BlobTrackGen(void)
- : m_FrameCount(0)
- , m_Transform(1)
- , m_pFileName(NULL)
- , m_TrackList(sizeof(DefBlobTrack))
- {
- m_Size = cvSize(2,2); AddParam("Transform", &m_Transform); SetParam("DebugWnd",0); SetModuleName("BlobTrackGen");
- }
- BlobTrackGen::~BlobTrackGen(void)
- {
- for(int i=m_TrackList.GetBlobNum(); i>0; i--) { DefBlobTrack* pTrack = (DefBlobTrack*)m_TrackList.GetBlob(i-1); delete pTrack->pSeq; pTrack->pSeq = NULL; }
- }
- CvPoint2D64f BlobTrackGen::affine_xform_pt(CvPoint2D64f pt, CvMat* T)
- {
- CvMat XY, UV;
- double xy[3] = {pt.x, pt.y, 1.0}, uv[2] = {0};
- cvInitMatHeader(&XY, 3, 1, CV_64FC1, xy, CV_AUTOSTEP);
- cvInitMatHeader(&UV, 2, 1, CV_64FC1, uv, CV_AUTOSTEP);
- cvMatMul(T, &XY, &UV);
- return cvPoint2D64f(uv[0] , uv[1]);
- }
- CvPoint2D64f BlobTrackGen::persp_xform_pt(CvPoint2D64f pt, CvMat* T)
- {
- CvMat XY, UV;
- double xy[3] = {pt.x, pt.y, 1.0}, uv[3] = {0};
- cvInitMatHeader(&XY, 3, 1, CV_64FC1, xy, CV_AUTOSTEP);
- cvInitMatHeader(&UV, 3, 1, CV_64FC1, uv, CV_AUTOSTEP);
- cvMatMul(T, &XY, &UV);
- return cvPoint2D64f(uv[0] / uv[2], uv[1] / uv[2]);
- }
- void BlobTrackGen::SaveAll(void)
- { int ObjNum = m_TrackList.GetBlobNum(); int i; char video_name[1024]; char* struct_name = NULL; CvFileStorage* storage = cvOpenFileStorage(m_pFileName, NULL,CV_STORAGE_WRITE_TEXT); if(storage == NULL) { printf("WARNING!!! Cannot open %s file for trajectory output.", m_pFileName); } for(i=0; i<1024 && m_pFileName[i]!='.' && m_pFileName[i]!=0; i++) video_name[i]=m_pFileName[i]; video_name[i] = 0; for(; i>0; i--) { if(video_name[i-1] == '\') break; if(video_name[i-1] == '/') break; if(video_name[i-1] == ':') break; } struct_name = video_name + i; int tail_length = 3; for(i=0; i<ObjNum; i++) { char obj_name[1024]; DefBlobTrack* pTrack = (DefBlobTrack*)m_TrackList.GetBlob(i); if(pTrack == NULL || pTrack->Saved == 1) continue; if ( pTrack->FrameLast - pTrack->FrameLast > tail_length) continue; sprintf(obj_name, "%s_obj%d", struct_name, i+1); cvStartWriteStruct(storage, struct_name, CV_NODE_MAP); CvBlobSeq* pSeq = pTrack->pSeq; cvWriteString(storage, "VideoObj", obj_name); cvWriteInt(storage, "FrameBegin", pTrack->FrameBegin); cvWriteInt(storage, "FrameLast", pTrack->FrameLast - tail_length); { int j; CvPoint2D32f p; cvStartWriteStruct( storage, "Pos", CV_NODE_SEQ|CV_NODE_FLOW ); for(j=0; j<pSeq->GetBlobNum() - tail_length;j++) { CvBlob* pB = pSeq->GetBlob(j); p.x = pB->x/(m_Size.width-1); p.y = pB->y/(m_Size.height-1); cvWriteRawData(storage, &p, 1 ,"ff"); } cvEndWriteStruct(storage); } { int j; CvPoint2D32f p; cvStartWriteStruct( storage, "Size", CV_NODE_SEQ|CV_NODE_FLOW ); for(j=0; j<pSeq->GetBlobNum() - tail_length; j++) { CvBlob* pB = pSeq->GetBlob(j); p.x = pB->w/(m_Size.width-1); p.y = pB->h/(m_Size.height-1); cvWriteRawData(storage, &p, 1 ,"ff"); } cvEndWriteStruct(storage); } cvEndWriteStruct(storage); /* pTrack->Saved = 1;*/ } cvReleaseFileStorage(&storage);
- }
- void BlobTrackGen::SetFileName(char* pFileName)
- {
- m_pFileName = pFileName;
- }
- void BlobTrackGen::AddBlob(CvBlob* pBlob)
- {
- DefBlobTrack* pTrack = (DefBlobTrack*)m_TrackList.GetBlobByID(CV_BLOB_ID(pBlob)); if(pTrack == NULL) { DefBlobTrack Track; Track.blob = pBlob[0]; Track.FrameBegin = m_FrameCount; Track.pSeq = new CvBlobSeq; Track.Saved = 0; m_TrackList.AddBlob((CvBlob*)&Track); pTrack = (DefBlobTrack*)m_TrackList.GetBlobByID(CV_BLOB_ID(pBlob)); } assert(pTrack); pTrack->FrameLast = m_FrameCount; assert(pTrack->pSeq); pTrack->pSeq->AddBlob(pBlob);
- }
- void BlobTrackGen::Process(IplImage* pImg, IplImage* pFG)
- {
- m_Size = cvSize(pImg->width, pImg->height); if (T) { for(int i=m_TrackList.GetBlobNum(); i>0; i--) { DefBlobTrack* pTrack = (DefBlobTrack*)m_TrackList.GetBlob(i-1); CvBlobSeq* pSeq = pTrack->pSeq; for(int j=0; j<pSeq->GetBlobNum()-1; j++) { CvBlob* pB = pSeq->GetBlob(j); CvPoint2D64f pt = cvPoint2D64f(pB->x, pB->y); CvPoint2D64f ptT; if(m_Transform) ptT = affine_xform_pt(pt, T); else ptT = persp_xform_pt(pt, T); pB->x = ptT.x; pB->y = ptT.y; } } } SaveAll(); if (m_Wnd) { IplImage *pI = cvCloneImage(pImg); for(int i=m_TrackList.GetBlobNum(); i>0; i--) { DefBlobTrack* pTrack = (DefBlobTrack*)m_TrackList.GetBlob(i-1); CvBlobSeq* pSeq = pTrack->pSeq; if (pTrack->FrameLast < m_FrameCount || pTrack->FrameLast-pTrack->FrameBegin < 5) continue; int j; for(j=0; j<pSeq->GetBlobNum()-1; j++) { CvBlob* pB1 = pSeq->GetBlob(j); CvBlob* pB2 = pSeq->GetBlob(j+1); CvPoint p1 =cvPoint(cvRound(pB1->x),cvRound(pB1->y)); CvPoint p2 =cvPoint(cvRound(pB2->x),cvRound(pB2->y)); cvLine(pI, p1, p2, CV_RGB(0,255,0), 1); } if (pTrack->FrameLast == m_FrameCount) { CvBlob* pB = pSeq->GetBlob(j); CvPoint p = cvPoint(cvRound(pB->x),cvRound(pB->y)); CvSize s = cvSize(MAX(1,cvRound(CV_BLOB_RX(pB))), MAX(1,cvRound(CV_BLOB_RY(pB)))); cvEllipse( pI, p, s, 0, 0, 360, CV_RGB(0,255,0), 1); #if 0 { // 对象的颜色柱状图信息
- CvMat mat;
- CvRect r;
- r.width = cvRound(pB->w*1.5+0.5); r.height = cvRound(pB->h*1.5+0.5); r.x = MAX(1,cvRound(pB->x - 0.5*r.width)); r.y = MAX(1,cvRound(pB->y - 0.5*r.height)); IplImage* src = cvCloneImage(pImg);
- cvSetImageROI(src,r);
- if(src)
- {
- IplImage* h_plane = cvCreateImage( cvGetSize(src), 8, 1 );
- IplImage* s_plane = cvCreateImage( cvGetSize(src), 8, 1 );
- IplImage* v_plane = cvCreateImage( cvGetSize(src), 8, 1 );
- IplImage* planes[] = { h_plane, s_plane, v_plane };
- IplImage* hsv = cvCreateImage( cvGetSize(src), 8, 3 );
- int h_bins = 16, s_bins = 4, v_bins = 4;
- int hist_size[] = {h_bins, s_bins, v_bins};
- float h_ranges[] = { 0, 180 };
- float s_ranges[] = { 0, 256 };
- float v_ranges[] = { 0, 256 };
- float* ranges[] = { h_ranges, s_ranges, v_ranges };
- int scale = 20;
- IplImage* hist_img = cvCreateImage( cvSize(h_bins*v_bins*scale*0.25,h_bins*s_bins*scale*0.25), 8, 3 );
- CvHistogram* hist;
- float min_value = 0, max_value = 0, sum_all = 0;
- int h, s, v;
- cvCvtColor( src, hsv, CV_RGB2HSV );
- cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 );
- hist = cvCreateHist( 3, hist_size, CV_HIST_ARRAY, ranges, 1 );
- cvCalcHist( planes, hist, 0, 0 );
- cvGetMinMaxHistValue( hist, &min_value, &max_value, 0, 0 );
- cvZero( hist_img );
- for( h = 0; h < h_bins; h++ )
- {
- for( s = 0; s < s_bins; s++ )
- {
- for( v = 0; v < v_bins; v++ )
- {
- float bin_val = cvQueryHistValue_3D( hist, h, s ,v);
- sum_all += bin_val;
- int intensity = 255-cvRound(bin_val*255/max_value);
- cvRectangle( hist_img, cvPoint( (v+(h%4)*v_bins)*scale,(s+(h/4)*s_bins)*scale ),
- cvPoint( (v+(h%4)*v_bins+1)*scale - 2,(s+(h/4)*s_bins+1)*scale - 2),
- CV_RGB(intensity,intensity,intensity),CV_FILLED );
- }
- }
- }
- char str[1024];
- sprintf(str,"Object %d",pTrack->blob.ID);
- cvNamedWindow( str, 1 );
- //cvResizeWindow(str,src->width,src->height);
- cvShowImage( str, src );
- char str2[1024];
- sprintf(str2,"Hist %d",pTrack->blob.ID);
- cvNamedWindow( str2, 1 );
- //cvResizeWindow(str2,hist_img->width,hist_img->height);
- cvShowImage( str2, hist_img );
- cvReleaseImage(&h_plane);
- cvReleaseImage(&s_plane);
- cvReleaseImage(&v_plane);
- cvReleaseImage(&hist_img);
- } cvReleaseImage(&src); } #endif } } cvNamedWindow("TrackGen"); cvResizeWindow("TrackGen",pI->width,pI->height); cvShowImage("TrackGen",pI); cvReleaseImage(&pI); } m_FrameCount++;
- }
- void BlobTrackGen::Release(void)
- {
- delete this;
- }
- /************************************************************************/
- /* 主模块——各个子模块综合模块 */
- /************************************************************************/
- #define TIME_BEGIN() #define TIME_END(_name_)
- BlobTrackerAuto::BlobTrackerAuto(CvBlobTrackerAutoParam* param):m_BlobList(sizeof(CvBlobTrackAuto))
- {
- m_BlobList.AddFormat("i");
- m_NextBlobID = 1;
- m_pFGMask = NULL;
- m_FrameCount = 0;
- m_FGTrainFrames = param?param->FGTrainFrames:0;
- m_pFG = param?param->pFG:NULL;
- m_pBD = param?param->pBD:NULL;
- m_pBT = param?param->pBT:NULL;
- m_pBTGen = param?param->pBTGen:NULL;
- m_pBTA = param?param->pBTA:NULL;
- m_pBTPostProc = param?param->pBTPP:NULL;
- m_UsePPData = param?param->UsePPData:0;
- SetModuleName("BlobTrack Auto");
- }
- BlobTrackerAuto::~BlobTrackerAuto()
- {
- }
- void BlobTrackerAuto::Process(IplImage* pImg, IplImage* pMask)
- {
- int CurBlobNum = 0;
- int i;
- IplImage* pFG = pMask;
- m_FrameCount++;
- TIME_BEGIN();
- if(m_pFG)
- {
- m_pFG->Process(pImg);
- pFG = m_pFG->GetMask();
- }
- TIME_END("FGDetector");
- m_pFGMask = pFG;
- #if 0
- static int frame = 1;
- static IplImage* pITemp;
- static IplImage* pITempRes = cvCreateImage(cvSize(pImg->width,pImg->height),pImg->depth,pImg->nChannels);
- if (frame == 1)
- {
- pITemp = cvCloneImage(pImg);
- cvZero(pITemp);
- }
- else
- {
- double alpha = (double)(frame-1)/(double)frame;
- double belta = (double)1/(double)frame;
- cvAddWeighted(pITemp,alpha,pImg,belta,0,pITempRes);
- cvCopyImage(pITempRes, pITemp);
- }
- cvSaveImage("E:\BG.bmp",pITemp);
- frame++;
- #endif
- TIME_BEGIN();
- if(m_pBT)
- {
- int i;
- m_pBT->Process(pImg, pFG);
- for(i=m_BlobList.GetBlobNum(); i>0; --i)
- {
- CvBlob* pB = m_BlobList.GetBlob(i-1);
- int BlobID = CV_BLOB_ID(pB);
- int i = m_pBT->GetBlobIndexByID(BlobID);
- m_pBT->ProcessBlob(i, pB, pImg, pFG);
- pB->ID = BlobID;
- }
- CurBlobNum = m_pBT->GetBlobNum();
- }
- TIME_END("BlobTracker");
- TIME_BEGIN();
- if(m_pBTPostProc)
- {
- int i;
- for(i=m_BlobList.GetBlobNum(); i>0; --i)
- {
- CvBlob* pB = m_BlobList.GetBlob(i-1);
- m_pBTPostProc->AddBlob(pB);
- }
- m_pBTPostProc->Process();
- for(i=m_BlobList.GetBlobNum(); i>0; --i)
- {
- CvBlob* pB = m_BlobList.GetBlob(i-1);
- int BlobID = CV_BLOB_ID(pB);
- CvBlob* pBN = m_pBTPostProc->GetBlobByID(BlobID);
- if(pBN && m_UsePPData && pBN->w >= CV_BLOB_MINW && pBN->h >= CV_BLOB_MINH)
- {
- m_pBT->SetBlobByID(BlobID, pBN );
- }
- if(pBN)
- {
- pB[0] = pBN[0];
- }
- }
- }
- TIME_END("PostProcessing");
- TIME_BEGIN();
- if(pFG)
- {
- int i;
- for(i=m_BlobList.GetBlobNum();i>0;--i)
- {
- CvBlobTrackAuto* pB = (CvBlobTrackAuto*)(m_BlobList.GetBlob(i-1));
- int Good = 0;
- int w=pFG->width;
- int h=pFG->height;
- CvRect r = CV_BLOB_RECT(pB);
- CvMat mat;
- double aver = 0;
- double area = CV_BLOB_WX(pB)*CV_BLOB_WY(pB);
- if(r.x < 0){r.width += r.x;r.x = 0;}
- if(r.y < 0){r.height += r.y;r.y = 0;}
- if(r.x+r.width>=w){r.width = w-r.x-1;}
- if(r.y+r.height>=h){r.height = h-r.y-1;}
- if(r.width > 4 && r.height > 4 && r.x < w && r.y < h &&
- r.x >=0 && r.y >=0 &&
- r.x+r.width < w && r.y+r.height < h && area > 0)
- {
- aver = cvSum(cvGetSubRect(pFG,&mat,r)).val[0] / area;
- if(aver > 0.1*255)Good = 1;
- }
- else
- {
- pB->BadFrames+=2;
- }
- if(Good)
- {
- pB->BadFrames = 0;
- }
- else
- {
- pB->BadFrames++;
- }
- }
- for(i=0; i<m_BlobList.GetBlobNum(); ++i)
- {
- CvBlobTrackAuto* pB = (CvBlobTrackAuto*)m_BlobList.GetBlob(i);
- if(pB->BadFrames>3)
- {
- m_pBT->DelBlobByID(CV_BLOB_ID(pB));
- m_BlobList.DelBlob(i);
- i--;
- }
- }
- }
- TIME_END("BlobDeleter");
- TIME_BEGIN();
- if(m_pBT)
- m_pBT->Update(pImg, pFG);
- TIME_END("BlobTrackerUpdate");
- TIME_BEGIN();
- if(m_pBD && pFG && (m_FrameCount > m_FGTrainFrames) )
- {
- static CvBlobSeq NewBlobList;
- CvBlobTrackAuto NewB;
- NewBlobList.Clear();
- if(m_pBD->DetectNewBlob(pImg, pFG, &NewBlobList, &m_BlobList))
- {
- int i;
- IplImage* pMask = pFG;
- for(i=0; i<NewBlobList.GetBlobNum(); ++i)
- {
- CvBlob* pBN = NewBlobList.GetBlob(i);
- pBN->ID = m_NextBlobID;
- if(pBN && pBN->w >= CV_BLOB_MINW && pBN->h >= CV_BLOB_MINH)
- {
- CvBlob* pB = m_pBT->AddBlob(pBN, pImg, pMask );
- if(pB)
- {
- NewB.blob = pB[0];
- NewB.BadFrames = 0;
- m_BlobList.AddBlob((CvBlob*)&NewB);
- m_NextBlobID++;
- }
- }
- }
- if(pMask != pFG) cvReleaseImage(&pMask);
- }
- }
- TIME_END("BlobDetector");
- TIME_BEGIN();
- if(m_pBTGen)
- {
- for(i=m_BlobList.GetBlobNum(); i>0; --i)
- {
- CvBlob* pB = m_BlobList.GetBlob(i-1);
- m_pBTGen->AddBlob(pB);
- }
- m_pBTGen->Process(pImg, pFG);
- }
- TIME_END("TrajectoryGeneration");
- TIME_BEGIN();
- if(m_pBTA)
- {
- int i;
- for(i=m_BlobList.GetBlobNum(); i>0; i--)
- m_pBTA->AddBlob(m_BlobList.GetBlob(i-1));
- m_pBTA->Process(pImg, pFG);
- }
- TIME_END("TrackAnalysis");
- }