1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
12 // Copyright (C) 2000, Intel Corporation, all rights reserved.
13 // Third party copyrights are property of their respective owners.
15 // Redistribution and use in source and binary forms, with or without modification,
16 // are permitted provided that the following conditions are met:
18 // * Redistribution's of source code must retain the above copyright notice,
19 // this list of conditions and the following disclaimer.
21 // * Redistribution's in binary form must reproduce the above copyright notice,
22 // this list of conditions and the following disclaimer in the documentation
23 // and/or other materials provided with the distribution.
25 // * The name of Intel Corporation may not be used to endorse or promote products
26 // derived from this software without specific prior written permission.
28 // This software is provided by the copyright holders and contributors "as is" and
29 // any express or implied warranties, including, but not limited to, the implied
30 // warranties of merchantability and fitness for a particular purpose are disclaimed.
31 // In no event shall the Intel Corporation or contributors be liable for any direct,
32 // indirect, incidental, special, exemplary, or consequential damages
33 // (including, but not limited to, procurement of substitute goods or services;
34 // loss of use, data, or profits; or business interruption) however caused
35 // and on any theory of liability, whether in contract, strict liability,
36 // or tort (including negligence or otherwise) arising in any way out of
37 // the use of this software, even if advised of the possibility of such damage.
43 // Uncomment to trade flexibility for speed
44 //#define CONST_HIST_SIZE
46 // Uncomment to get some performance stats in stderr
47 //#define REPORT_TICKS
49 #ifdef CONST_HIST_SIZE
54 typedef float DefHistType;
55 #define DefHistTypeMat CV_32F
56 #define HIST_INDEX(_pData) (((_pData)[0]>>m_ByteShift) + (((_pData)[1]>>(m_ByteShift))<<m_BinBit)+((pImgData[2]>>m_ByteShift)<<(m_BinBit*2)))
62 DefHistType m_HistVolume;
72 if(m_pHist)cvReleaseMat(&m_pHist);
75 void Resize(int BinNum)
77 if(m_pHist)cvReleaseMat(&m_pHist);
80 m_pHist = cvCreateMat(1, BinNum, DefHistTypeMat);
86 void Update(DefHist* pH, float W)
87 { /* Update histogram: */
89 Vol = 0.5*(m_HistVolume + pH->m_HistVolume);
90 WM = Vol*(1-W)/m_HistVolume;
91 WC = Vol*(W)/pH->m_HistVolume;
92 cvAddWeighted(m_pHist, WM, pH->m_pHist,WC,0,m_pHist);
93 m_HistVolume = (float)cvSum(m_pHist).val[0];
94 } /* Update histogram: */
97 class CvBlobTrackerOneMSFG:public CvBlobTrackerOne
100 int m_BinNumTotal; /* protected only for parralel MSPF */
103 void ReAllocKernel(int w, int h)
106 float x0 = 0.5f*(w-1);
107 float y0 = 0.5f*(h-1);
110 m_ObjSize = cvSize(w,h);
112 if(m_KernelHist) cvReleaseMat(&m_KernelHist);
113 if(m_KernelMeanShift) cvReleaseMat(&m_KernelMeanShift);
114 m_KernelHist = cvCreateMat(h, w, DefHistTypeMat);
115 m_KernelMeanShift = cvCreateMat(h, w, DefHistTypeMat);
117 for(y=0; y<h; ++y) for(x=0; x<w; ++x)
119 double r2 = ((x-x0)*(x-x0)/(x0*x0)+(y-y0)*(y-y0)/(y0*y0));
120 // double r2 = ((x-x0)*(x-x0)+(y-y0)*(y-y0))/((y0*y0)+(x0*x0));
121 CV_MAT_ELEM(m_KernelHist[0],DefHistType, y, x) = (DefHistType)GetKernelHist(r2);
122 CV_MAT_ELEM(m_KernelMeanShift[0],DefHistType, y, x) = (DefHistType)GetKernelMeanShift(r2);
132 CvMat* m_KernelMeanShift;
133 #ifndef CONST_HIST_SIZE
141 float m_HistModelVolume;
142 CvMat* m_HistCandidate;
143 float m_HistCandidateVolume;
147 DefHist m_HistCandidate;
153 void ReAllocHist(int Dim, int BinBit)
155 #ifndef CONST_HIST_SIZE
157 m_ByteShift = 8-BinBit;
160 m_BinNum = (1<<BinBit);
161 m_BinNumTotal = cvRound(pow((double)m_BinNum,(double)m_Dim));
163 if(m_HistModel) cvReleaseMat(&m_HistModel);
164 if(m_HistCandidate) cvReleaseMat(&m_HistCandidate);
165 if(m_HistTemp) cvReleaseMat(&m_HistTemp);
166 m_HistCandidate = cvCreateMat(1, m_BinNumTotal, DefHistTypeMat);
167 m_HistModel = cvCreateMat(1, m_BinNumTotal, DefHistTypeMat);
168 m_HistTemp = cvCreateMat(1, m_BinNumTotal, DefHistTypeMat);
169 cvZero(m_HistCandidate);
171 m_HistModelVolume = 0.0f;
172 m_HistCandidateVolume = 0.0f;
174 m_HistCandidate.Resize(m_BinNumTotal);
175 m_HistModel.Resize(m_BinNumTotal);
176 m_HistTemp.Resize(m_BinNumTotal);
179 double GetKernelHist(double r2)
181 return (r2 < 1) ? 1 - r2 : 0;
184 double GetKernelMeanShift(double r2)
186 return (r2<1) ? 1 : 0;
189 // void CollectHist(IplImage* pImg, IplImage* pMask, CvPoint Center, CvMat* pHist, DefHistType* pHistVolume)
190 // void CollectHist(IplImage* pImg, IplImage* pMask, CvPoint Center, DefHist* pHist)
192 void CollectHist(IplImage* pImg, IplImage* pMask, CvBlob* pBlob, DefHist* pHist)
194 int UsePrecalculatedKernel = 0;
195 int BW = cvRound(pBlob->w);
196 int BH = cvRound(pBlob->h);
197 DefHistType Volume = 0;
198 int x0 = cvRound(pBlob->x - BW*0.5);
199 int y0 = cvRound(pBlob->y - BH*0.5);
202 UsePrecalculatedKernel = (BW == m_ObjSize.width && BH == m_ObjSize.height ) ;
205 cvSet(pHist->m_pHist,cvScalar(1.0/m_BinNumTotal)); /* no zero bins, all bins have very small value*/
208 assert(BW < pImg->width);
209 assert(BH < pImg->height);
210 if((x0+BW)>=pImg->width) BW=pImg->width-x0-1;
211 if((y0+BH)>=pImg->height) BH=pImg->height-y0-1;
219 unsigned char* pImgData = &CV_IMAGE_ELEM(pImg,unsigned char,y+y0,x0*3);
220 unsigned char* pMaskData = pMask?(&CV_IMAGE_ELEM(pMask,unsigned char,y+y0,x0)):NULL;
221 DefHistType* pKernelData = NULL;
223 if(UsePrecalculatedKernel)
225 pKernelData = ((DefHistType*)CV_MAT_ELEM_PTR_FAST(m_KernelHist[0],y,0,sizeof(DefHistType)));
228 for(x=0; x<BW; ++x, pImgData+=3)
231 int index = HIST_INDEX(pImgData);
232 assert(index >= 0 && index < pHist->m_pHist->cols);
234 if(UsePrecalculatedKernel)
240 float dx = (x+x0-pBlob->x)/(pBlob->w*0.5f);
241 float dy = (y+y0-pBlob->y)/(pBlob->h*0.5f);
242 double r2 = dx*dx+dy*dy;
243 K = (float)GetKernelHist(r2);
248 K *= pMaskData[x]*0.003921568627450980392156862745098f;
251 ((DefHistType*)(pHist->m_pHist->data.ptr))[index] += K;
255 } /* if m_Dim == 3. */
257 pHist->m_HistVolume = Volume;
261 double calcBhattacharyya(DefHist* pHM = NULL, DefHist* pHC = NULL, DefHist* pHT = NULL)
263 if(pHM==NULL) pHM = &m_HistModel;
264 if(pHC==NULL) pHC = &m_HistCandidate;
265 if(pHT==NULL) pHT = &m_HistTemp;
266 if(pHC->m_HistVolume*pHM->m_HistVolume > 0)
270 cvMul(pHM->m_pHist,pHC->m_pHist,pHT->m_pHist);
271 cvPow(pHT->m_pHist,pHT->m_pHist,0.5);
272 return cvSum(pHT->m_pHist).val[0] / sqrt(pHC->m_HistVolume*pHM->m_HistVolume);
274 // Do computations manually and let autovectorizer do the job:
275 DefHistType *hm, *hc, *ht;
278 hm=(DefHistType *)(pHM->m_pHist->data.ptr);
279 hc=(DefHistType *)(pHC->m_pHist->data.ptr);
280 ht=(DefHistType *)(pHT->m_pHist->data.ptr);
281 size = pHM->m_pHist->width*pHM->m_pHist->height;
283 for(int i = 0; i < size; i++ )
285 sum += sqrt(hm[i]*hc[i]);
287 return sum / sqrt(pHC->m_HistVolume*pHM->m_HistVolume);
291 } /* calcBhattacharyyaCoefficient. */
294 // double GetBhattacharyya(IplImage* pImg, IplImage* pImgFG, float x, float y, DefHist* pHist=NULL)
295 double GetBhattacharyya(IplImage* pImg, IplImage* pImgFG, CvBlob* pBlob, DefHist* pHist=NULL, int /*thread_number*/ = 0)
297 if(pHist==NULL)pHist = &m_HistTemp;
298 CollectHist(pImg, pImgFG, pBlob, pHist);
299 return calcBhattacharyya(&m_HistModel, pHist, pHist);
302 void UpdateModelHist(IplImage* pImg, IplImage* pImgFG, CvBlob* pBlob)
304 if(m_Alpha>0 && !m_Collision)
305 { /* Update histogram: */
306 CollectHist(pImg, pImgFG, pBlob, &m_HistCandidate);
307 m_HistModel.Update(&m_HistCandidate, m_Alpha);
308 } /* Update histogram. */
310 } /* UpdateModelHist */
313 CvBlobTrackerOneMSFG()
316 /* Add several parameters for external use: */
318 AddParam("FGWeight", &m_FGWeight);
319 CommentParam("FGWeight","Weight of FG mask using (0 - mask will not be used for tracking)");
322 AddParam("Alpha", &m_Alpha);
323 CommentParam("Alpha","Coefficient for model histogram updating (0 - hist is not upated)");
326 AddParam("IterNum", &m_IterNum);
327 CommentParam("IterNum","Maximal number of iteration in meanshift operation");
329 /* Initialize internal data: */
335 m_HistCandidate = NULL;
339 m_KernelMeanShift = NULL;
340 ReAllocHist(3,5); /* 3D hist, each dim has 2^5 bins*/
342 SetModuleName("MSFG");
345 ~CvBlobTrackerOneMSFG()
348 if(m_HistModel) cvReleaseMat(&m_HistModel);
349 if(m_HistCandidate) cvReleaseMat(&m_HistCandidate);
350 if(m_HistTemp) cvReleaseMat(&m_HistTemp);
352 if(m_KernelHist) cvReleaseMat(&m_KernelHist);
353 if(m_KernelMeanShift) cvReleaseMat(&m_KernelMeanShift);
357 virtual void Init(CvBlob* pBlobInit, IplImage* pImg, IplImage* pImgFG = NULL)
359 int w = cvRound(CV_BLOB_WX(pBlobInit));
360 int h = cvRound(CV_BLOB_WY(pBlobInit));
361 if(w<CV_BLOB_MINW)w=CV_BLOB_MINW;
362 if(h<CV_BLOB_MINH)h=CV_BLOB_MINH;
365 if(w>pImg->width)w=pImg->width;
366 if(h>pImg->height)h=pImg->height;
370 CollectHist(pImg, pImgFG, pBlobInit, &m_HistModel);
371 m_Blob = pBlobInit[0];
374 virtual CvBlob* Process(CvBlob* pBlobPrev, IplImage* pImg, IplImage* pImgFG = NULL)
380 m_Blob = pBlobPrev[0];
383 { /* Check blob size and realloc kernels if it is necessary: */
384 int w = cvRound(m_Blob.w);
385 int h = cvRound(m_Blob.h);
386 if( w != m_ObjSize.width || h!=m_ObjSize.height)
389 /* after this ( w != m_ObjSize.width || h!=m_ObjSize.height) shoiuld be false */
391 } /* Check blob size and realloc kernels if it is necessary: */
394 for(iter=0; iter<m_IterNum; ++iter)
396 float newx=0,newy=0,sum=0;
400 //CvPoint Center = cvPoint(cvRound(m_Blob.x),cvRound(m_Blob.y));
401 CollectHist(pImg, NULL, &m_Blob, &m_HistCandidate);
402 B0 = calcBhattacharyya();
404 if(m_Wnd)if(CV_BLOB_ID(pBlobPrev)==0 && iter == 0)
406 IplImage* pW = cvCloneImage(pImgFG);
407 IplImage* pWFG = cvCloneImage(pImgFG);
413 assert(m_ObjSize.width < pImg->width);
414 assert(m_ObjSize.height < pImg->height);
416 /* Calculate shift vector: */
417 for(y=0; y<pImg->height; ++y)
419 unsigned char* pImgData = &CV_IMAGE_ELEM(pImg,unsigned char,y,0);
420 unsigned char* pMaskData = pImgFG?(&CV_IMAGE_ELEM(pImgFG,unsigned char,y,0)):NULL;
422 for(x=0; x<pImg->width; ++x, pImgData+=3)
424 int xk = cvRound(x-(m_Blob.x-m_Blob.w*0.5));
425 int yk = cvRound(y-(m_Blob.y-m_Blob.h*0.5));
429 int index = HIST_INDEX(pImgData);
430 assert(index >= 0 && index < m_BinNumTotal);
432 if(fabs(x-m_Blob.x)>m_Blob.w*0.6) continue;
433 if(fabs(y-m_Blob.y)>m_Blob.h*0.6) continue;
435 if(xk < 0 || xk >= m_KernelMeanShift->cols) continue;
436 if(yk < 0 || yk >= m_KernelMeanShift->rows) continue;
438 if(m_HistModel.m_HistVolume>0)
439 HM = ((DefHistType*)m_HistModel.m_pHist->data.ptr)[index]/m_HistModel.m_HistVolume;
441 if(m_HistCandidate.m_HistVolume>0)
442 HC = ((DefHistType*)m_HistCandidate.m_pHist->data.ptr)[index]/m_HistCandidate.m_HistVolume;
444 K = *(DefHistType*)CV_MAT_ELEM_PTR_FAST(m_KernelMeanShift[0],yk,xk,sizeof(DefHistType));
448 double V = sqrt(HM / HC);
449 int Vi = cvRound(V * 64);
451 if(Vi > 255) Vi = 255;
452 CV_IMAGE_ELEM(pW,uchar,y,x) = (uchar)Vi;
454 V += m_FGWeight*(pMaskData?(pMaskData[x]/255.0f):0);
456 Vi = cvRound(V * 64);
458 if(Vi > 255) Vi = 255;
459 CV_IMAGE_ELEM(pWFG,uchar,y,x) = (uchar)Vi;
465 //cvNamedWindow("MSFG_W",0);
466 //cvShowImage("MSFG_W",pW);
467 //cvNamedWindow("MSFG_WFG",0);
468 //cvShowImage("MSFG_WFG",pWFG);
469 //cvNamedWindow("MSFG_FG",0);
470 //cvShowImage("MSFG_FG",pImgFG);
472 //cvSaveImage("MSFG_W.bmp",pW);
473 //cvSaveImage("MSFG_WFG.bmp",pWFG);
474 //cvSaveImage("MSFG_FG.bmp",pImgFG);
479 /* Calculate new position by meanshift: */
481 /* Calculate new position: */
484 int x0 = cvRound(m_Blob.x - m_ObjSize.width*0.5);
485 int y0 = cvRound(m_Blob.y - m_ObjSize.height*0.5);
488 assert(m_ObjSize.width < pImg->width);
489 assert(m_ObjSize.height < pImg->height);
491 /* Crop blob bounds: */
492 if((x0+m_ObjSize.width)>=pImg->width) x0=pImg->width-m_ObjSize.width-1;
493 if((y0+m_ObjSize.height)>=pImg->height) y0=pImg->height-m_ObjSize.height-1;
497 /* Calculate shift vector: */
498 for(y=0; y<m_ObjSize.height; ++y)
500 unsigned char* pImgData = &CV_IMAGE_ELEM(pImg,unsigned char,y+y0,x0*3);
501 unsigned char* pMaskData = pImgFG?(&CV_IMAGE_ELEM(pImgFG,unsigned char,y+y0,x0)):NULL;
502 DefHistType* pKernelData = (DefHistType*)CV_MAT_ELEM_PTR_FAST(m_KernelMeanShift[0],y,0,sizeof(DefHistType));
504 for(x=0; x<m_ObjSize.width; ++x, pImgData+=3)
506 DefHistType K = pKernelData[x];
509 int index = HIST_INDEX(pImgData);
510 assert(index >= 0 && index < m_BinNumTotal);
512 if(m_HistModel.m_HistVolume>0)
513 HM = ((DefHistType*)m_HistModel.m_pHist->data.ptr)[index]/m_HistModel.m_HistVolume;
515 if(m_HistCandidate.m_HistVolume>0)
516 HC = ((DefHistType*)m_HistCandidate.m_pHist->data.ptr)[index]/m_HistCandidate.m_HistVolume;
520 double V = sqrt(HM / HC);
521 if(!m_Collision && m_FGWeight>0 && pMaskData)
523 V += m_FGWeight*(pMaskData[x]/255.0f);
525 K *= (float)MIN(V,100000.);
542 } /* if m_Dim == 3. */
544 /* Calculate new position by meanshift: */
547 { /* Iterate using bahattcharrya coefficient: */
550 // CvPoint NewCenter = cvPoint(cvRound(newx),cvRound(newy));
553 CollectHist(pImg, NULL, &B, &m_HistCandidate);
554 B1 = calcBhattacharyya();
556 newx = 0.5f*(newx+m_Blob.x);
557 newy = 0.5f*(newy+m_Blob.y);
558 if(fabs(newx-m_Blob.x)<0.1 && fabs(newy-m_Blob.y)<0.1) break;
559 } /* Iterate using bahattcharrya coefficient. */
561 if(fabs(newx-m_Blob.x)<0.5 && fabs(newy-m_Blob.y)<0.5) break;
564 } /* Next iteration. */
566 while(!m_Collision && m_FGWeight>0)
567 { /* Update size if no collision. */
570 double M00,X,Y,XX,YY;
575 r.width = cvRound(m_Blob.w*1.5+0.5);
576 r.height = cvRound(m_Blob.h*1.5+0.5);
577 r.x = cvRound(m_Blob.x - 0.5*r.width);
578 r.y = cvRound(m_Blob.y - 0.5*r.height);
581 if(r.x+r.width >= pImgFG->width) break;
582 if(r.y+r.height >= pImgFG->height) break;
583 if(r.height < 5 || r.width < 5) break;
585 cvMoments( cvGetSubRect(pImgFG,&mat,r), &m, 0 );
586 M00 = cvGetSpatialMoment( &m, 0, 0 );
588 X = cvGetSpatialMoment( &m, 1, 0 )/M00;
589 Y = cvGetSpatialMoment( &m, 0, 1 )/M00;
590 XX = (cvGetSpatialMoment( &m, 2, 0 )/M00) - X*X;
591 YY = (cvGetSpatialMoment( &m, 0, 2 )/M00) - Y*Y;
592 NewBlob = cvBlob(r.x+(float)X,r.y+(float)Y,(float)(4*sqrt(XX)),(float)(4*sqrt(YY)));
594 NewBlob.w = Alpha*NewBlob.w+m_Blob.w*(1-Alpha);
595 NewBlob.h = Alpha*NewBlob.h+m_Blob.h*(1-Alpha);
597 m_Blob.w = MAX(NewBlob.w,5);
598 m_Blob.h = MAX(NewBlob.h,5);
601 } /* Update size if no collision. */
605 }; /* CvBlobTrackerOneMSFG::Process */
607 virtual double GetConfidence(CvBlob* pBlob, IplImage* pImg, IplImage* /*pImgFG*/ = NULL, IplImage* pImgUnusedReg = NULL)
610 double B = GetBhattacharyya(pImg, pImgUnusedReg, pBlob, &m_HistTemp);
611 return exp((B-1)/(2*S));
613 }; /*CvBlobTrackerOneMSFG::*/
615 virtual void Update(CvBlob* pBlob, IplImage* pImg, IplImage* pImgFG = NULL)
616 { /* Update histogram: */
617 UpdateModelHist(pImg, pImgFG, pBlob?pBlob:&m_Blob);
618 } /*CvBlobTrackerOneMSFG::*/
620 virtual void Release(){delete this;};
621 virtual void SetCollision(int CollisionFlag)
623 m_Collision = CollisionFlag;
625 virtual void SaveState(CvFileStorage* fs)
627 cvWriteStruct(fs, "Blob", &m_Blob, "ffffi");
628 cvWriteInt(fs,"Collision", m_Collision);
629 cvWriteInt(fs,"HistVolume", cvRound(m_HistModel.m_HistVolume));
630 cvWrite(fs,"Hist", m_HistModel.m_pHist);
632 virtual void LoadState(CvFileStorage* fs, CvFileNode* node)
635 cvReadStructByName(fs, node, "Blob",&m_Blob, "ffffi");
636 m_Collision = cvReadIntByName(fs,node,"Collision",m_Collision);
637 pM = (CvMat*)cvRead(fs,cvGetFileNodeByName(fs,node,"Hist"));
640 m_HistModel.m_pHist = pM;
641 m_HistModel.m_HistVolume = (float)cvSum(pM).val[0];
645 }; /*CvBlobTrackerOneMSFG*/
648 void CvBlobTrackerOneMSFG::CollectHist(IplImage* pImg, IplImage* pMask, CvBlob* pBlob, DefHist* pHist)
650 int UsePrecalculatedKernel = 0;
651 int BW = cvRound(pBlob->w);
652 int BH = cvRound(pBlob->h);
653 DefHistType Volume = 0;
654 int x0 = cvRound(pBlob->x - BW*0.5);
655 int y0 = cvRound(pBlob->y - BH*0.5);
658 UsePrecalculatedKernel = (BW == m_ObjSize.width && BH == m_ObjSize.height ) ;
661 cvSet(pHist->m_pHist,cvScalar(1.0/m_BinNumTotal)); /* no zero bins, all bins have very small value*/
664 assert(BW < pImg->width);
665 assert(BH < pImg->height);
666 if((x0+BW)>=pImg->width) BW=pImg->width-x0-1;
667 if((y0+BH)>=pImg->height) BH=pImg->height-y0-1;
675 unsigned char* pImgData = &CV_IMAGE_ELEM(pImg,unsigned char,y+y0,x0*3);
676 unsigned char* pMaskData = pMask?(&CV_IMAGE_ELEM(pMask,unsigned char,y+y0,x0)):NULL;
677 DefHistType* pKernelData = NULL;
679 if(UsePrecalculatedKernel)
681 pKernelData = ((DefHistType*)CV_MAT_ELEM_PTR_FAST(m_KernelHist[0],y,0,sizeof(DefHistType)));
684 for(x=0; x<BW; ++x, pImgData+=3)
687 int index = HIST_INDEX(pImgData);
688 assert(index >= 0 && index < pHist->m_pHist->cols);
690 if(UsePrecalculatedKernel)
696 float dx = (x+x0-pBlob->x)/(pBlob->w*0.5);
697 float dy = (y+y0-pBlob->y)/(pBlob->h*0.5);
698 double r2 = dx*dx+dy*dy;
699 K = GetKernelHist(r2);
704 K *= pMaskData[x]*0.003921568627450980392156862745098;
707 ((DefHistType*)(pHist->m_pHist->data.ptr))[index] += K;
711 } /* if m_Dim == 3. */
713 pHist->m_HistVolume = Volume;
718 CvBlobTrackerOne* cvCreateBlobTrackerOneMSFG()
720 return (CvBlobTrackerOne*) new CvBlobTrackerOneMSFG;
723 CvBlobTracker* cvCreateBlobTrackerMSFG()
725 return cvCreateBlobTrackerList(cvCreateBlobTrackerOneMSFG);
728 /* Create specific tracker without FG
729 * usin - just simple mean-shift tracker: */
730 class CvBlobTrackerOneMS:public CvBlobTrackerOneMSFG
735 SetParam("FGWeight",0);
736 DelParam("FGWeight");
741 CvBlobTrackerOne* cvCreateBlobTrackerOneMS()
743 return (CvBlobTrackerOne*) new CvBlobTrackerOneMS;
746 CvBlobTracker* cvCreateBlobTrackerMS()
748 return cvCreateBlobTrackerList(cvCreateBlobTrackerOneMS);
751 typedef struct DefParticle
758 class CvBlobTrackerOneMSPF:public CvBlobTrackerOneMS
769 DefParticle* m_pParticlesPredicted;
770 DefParticle* m_pParticlesResampled;
774 DefHist* m_HistForParalel;
778 virtual void SaveState(CvFileStorage* fs)
780 CvBlobTrackerOneMS::SaveState(fs);
781 cvWriteInt(fs,"ParticleNum",m_ParticleNum);
782 cvWriteStruct(fs,"ParticlesPredicted",m_pParticlesPredicted,"ffffiffd",m_ParticleNum);
783 cvWriteStruct(fs,"ParticlesResampled",m_pParticlesResampled,"ffffiffd",m_ParticleNum);
786 virtual void LoadState(CvFileStorage* fs, CvFileNode* node)
789 CvBlobTrackerOneMS::LoadState(fs,node);
790 m_ParticleNum = cvReadIntByName(fs,node,"ParticleNum",m_ParticleNum);
794 printf("sizeof(DefParticle) is %d\n", (int)sizeof(DefParticle));
795 cvReadStructByName(fs,node,"ParticlesPredicted",m_pParticlesPredicted,"ffffiffd");
796 cvReadStructByName(fs,node,"ParticlesResampled",m_pParticlesResampled,"ffffiffd");
799 CvBlobTrackerOneMSPF()
801 m_pParticlesPredicted = NULL;
802 m_pParticlesResampled = NULL;
805 AddParam("ParticleNum",&m_ParticleNum);
806 CommentParam("ParticleNum","Number of particles");
810 AddParam("UseVel",&m_UseVel);
811 CommentParam("UseVel","Percent of particles which use velocity feature");
814 AddParam("SizeVar",&m_SizeVar);
815 CommentParam("SizeVar","Size variation (in object size)");
818 AddParam("PosVar",&m_PosVar);
819 CommentParam("PosVar","Position variation (in object size)");
823 SetModuleName("MSPF");
827 m_ThreadNum = omp_get_num_procs();
828 m_HistForParalel = new DefHist[m_ThreadNum];
833 ~CvBlobTrackerOneMSPF()
835 if(m_pParticlesResampled)cvFree(&m_pParticlesResampled);
836 if(m_pParticlesPredicted)cvFree(&m_pParticlesPredicted);
838 if(m_HistForParalel) delete[] m_HistForParalel;
845 if(m_pParticlesResampled)cvFree(&m_pParticlesResampled);
846 if(m_pParticlesPredicted)cvFree(&m_pParticlesPredicted);
847 m_pParticlesPredicted = (DefParticle*)cvAlloc(sizeof(DefParticle)*m_ParticleNum);
848 m_pParticlesResampled = (DefParticle*)cvAlloc(sizeof(DefParticle)*m_ParticleNum);
851 void DrawDebug(IplImage* pImg, IplImage* /*pImgFG*/)
856 DefParticle* pBP = k?m_pParticlesResampled:m_pParticlesPredicted;
857 //const char* name = k?"MSPF resampled particle":"MSPF Predicted particle";
858 IplImage* pI = cvCloneImage(pImg);
859 int h,hN = m_ParticleNum;
860 CvBlob C = cvBlob(0,0,0,0);
864 CvBlob B = pBP[h].blob;
865 int CW = cvRound(255*pBP[h].W);
867 int x = cvRound(CV_BLOB_RX(pB)), y = cvRound(CV_BLOB_RY(pB));
868 CvSize s = cvSize(MAX(1,x), MAX(1,y));
878 cvPointFrom32f(CV_BLOB_CENTER(pB)),
883 } /* Next hypothesis. */
891 cvPointFrom32f(CV_BLOB_CENTER(&C)),
892 cvSize(cvRound(C.w*0.5),cvRound(C.h*0.5)),
894 CV_RGB(0,0,255), 1 );
897 cvPointFrom32f(CV_BLOB_CENTER(&m_Blob)),
898 cvSize(cvRound(m_Blob.w*0.5),cvRound(m_Blob.h*0.5)),
900 CV_RGB(0,255,0), 1 );
902 //cvNamedWindow(name,0);
903 //cvShowImage(name,pI);
907 //printf("Blob %d, point (%.1f,%.1f) size (%.1f,%.1f)\n",m_Blob.ID,m_Blob.x,m_Blob.y,m_Blob.w,m_Blob.h);
914 for(p=0; p<m_ParticleNum; ++p)
915 { /* "Prediction" of particle: */
918 CvMat rm = cvMat(1,5,CV_32F,r);
919 cvRandArr(&m_RNG,&rm,CV_RAND_NORMAL,cvScalar(0),cvScalar(1));
921 m_pParticlesPredicted[p] = m_pParticlesResampled[p];
923 if(cvRandReal(&m_RNG)<0.5)
924 { /* Half of particles will predict based on external blob: */
925 m_pParticlesPredicted[p].blob = m_Blob;
928 if(cvRandReal(&m_RNG)<m_UseVel)
929 { /* Predict moving particle by usual way by using speed: */
930 m_pParticlesPredicted[p].blob.x += m_pParticlesPredicted[p].Vx;
931 m_pParticlesPredicted[p].blob.y += m_pParticlesPredicted[p].Vy;
934 { /* Stop several particles: */
935 m_pParticlesPredicted[p].Vx = 0;
936 m_pParticlesPredicted[p].Vy = 0;
939 { /* Update position: */
940 float S = (m_Blob.w + m_Blob.h)*0.5f;
941 m_pParticlesPredicted[p].blob.x += m_PosVar*S*r[0];
942 m_pParticlesPredicted[p].blob.y += m_PosVar*S*r[1];
944 /* Update velocity: */
945 m_pParticlesPredicted[p].Vx += (float)(m_PosVar*S*0.1*r[3]);
946 m_pParticlesPredicted[p].Vy += (float)(m_PosVar*S*0.1*r[4]);
950 m_pParticlesPredicted[p].blob.w *= (1+m_SizeVar*r[2]);
951 m_pParticlesPredicted[p].blob.h *= (1+m_SizeVar*r[2]);
953 /* Truncate size of particle: */
954 if(m_pParticlesPredicted[p].blob.w > m_ImgSize.width*0.5f)
956 m_pParticlesPredicted[p].blob.w = m_ImgSize.width*0.5f;
959 if(m_pParticlesPredicted[p].blob.h > m_ImgSize.height*0.5f)
961 m_pParticlesPredicted[p].blob.h = m_ImgSize.height*0.5f;
964 if(m_pParticlesPredicted[p].blob.w < 1 )
966 m_pParticlesPredicted[p].blob.w = 1;
969 if(m_pParticlesPredicted[p].blob.h < 1)
971 m_pParticlesPredicted[p].blob.h = 1;
973 } /* "Prediction" of particle. */
976 void UpdateWeightsMS(IplImage* pImg, IplImage* /*pImgFG*/)
980 if( m_HistForParalel[0].m_pHist==NULL || m_HistForParalel[0].m_pHist->cols != m_BinNumTotal)
983 for(t=0; t<m_ThreadNum; ++t)
984 m_HistForParalel[t].Resize(m_BinNumTotal);
989 #pragma omp parallel for num_threads(m_ThreadNum) schedule(runtime)
991 for(p=0;p<m_ParticleNum;++p)
992 { /* Calculate weights for particles: */
996 assert(omp_get_thread_num()<m_ThreadNum);
999 B = GetBhattacharyya(
1001 &(m_pParticlesPredicted[p].blob)
1003 ,&(m_HistForParalel[omp_get_thread_num()])
1006 m_pParticlesPredicted[p].W *= exp((B-1)/(2*S));
1008 } /* Calculate weights for particles. */
1011 void UpdateWeightsCC(IplImage* /*pImg*/, IplImage* /*pImgFG*/)
1015 #pragma omp parallel for
1017 for(p=0; p<m_ParticleNum; ++p)
1018 { /* Calculate weights for particles: */
1020 m_pParticlesPredicted[p].W *= W;
1021 } /* Calculate weights for particles. */
1025 { /* Resample particle: */
1029 for(p=0; p<m_ParticleNum; ++p)
1031 Sum += m_pParticlesPredicted[p].W;
1034 for(p=0; p<m_ParticleNum; ++p)
1036 double T = Sum * cvRandReal(&m_RNG); /* Set current random threshold for cululative weight. */
1040 for(p2=0; p2<m_ParticleNum; ++p2)
1042 Sum2 += m_pParticlesPredicted[p2].W;
1046 if(p2>=m_ParticleNum)p2=m_ParticleNum-1;
1047 m_pParticlesResampled[p] = m_pParticlesPredicted[p2];
1048 m_pParticlesResampled[p].W = 1;
1050 } /* Find next particle. */
1051 } /* Resample particle. */
1055 virtual void Init(CvBlob* pBlobInit, IplImage* pImg, IplImage* pImgFG = NULL)
1058 CvBlobTrackerOneMSFG::Init(pBlobInit, pImg, pImgFG);
1063 PP.blob = pBlobInit[0];
1064 for(i=0;i<m_ParticleNum;++i)
1066 m_pParticlesPredicted[i] = PP;
1067 m_pParticlesResampled[i] = PP;
1069 m_Blob = pBlobInit[0];
1071 } /* CvBlobTrackerOneMSPF::Init*/
1073 virtual CvBlob* Process(CvBlob* pBlobPrev, IplImage* pImg, IplImage* pImgFG = NULL)
1077 m_ImgSize.width = pImg->width;
1078 m_ImgSize.height = pImg->height;
1081 m_Blob = pBlobPrev[0];
1083 { /* Check blob size and realloc kernels if it is necessary: */
1084 int w = cvRound(m_Blob.w);
1085 int h = cvRound(m_Blob.h);
1086 if( w != m_ObjSize.width || h!=m_ObjSize.height)
1089 /* After this ( w != m_ObjSize.width || h!=m_ObjSize.height) should be false. */
1091 } /* Check blob size and realloc kernels if it is necessary. */
1096 int64 ticks = cvGetTickCount();
1099 UpdateWeightsMS(pImg, pImgFG);
1102 ticks = cvGetTickCount() - ticks;
1103 fprintf(stderr, "PF UpdateWeights, %d ticks\n", (int)ticks);
1104 ticks = cvGetTickCount();
1110 ticks = cvGetTickCount() - ticks;
1111 fprintf(stderr, "PF Resampling, %d ticks\n", (int)ticks);
1114 { /* Find average result: */
1121 DefParticle* pP = m_pParticlesResampled;
1123 for(p=0; p<m_ParticleNum; ++p)
1125 float W = (float)pP[p].W;
1126 x += W*pP[p].blob.x;
1127 y += W*pP[p].blob.y;
1128 w += W*pP[p].blob.w;
1129 h += W*pP[p].blob.h;
1140 } /* Find average result. */
1144 DrawDebug(pImg, pImgFG);
1149 } /* CvBlobTrackerOneMSPF::Process */
1151 virtual void SkipProcess(CvBlob* pBlob, IplImage* /*pImg*/, IplImage* /*pImgFG*/ = NULL)
1154 for(p=0; p<m_ParticleNum; ++p)
1156 m_pParticlesResampled[p].blob = pBlob[0];
1157 m_pParticlesResampled[p].Vx = 0;
1158 m_pParticlesResampled[p].Vy = 0;
1159 m_pParticlesResampled[p].W = 1;
1163 virtual void Release(){delete this;};
1164 virtual void ParamUpdate()
1169 }; /* CvBlobTrackerOneMSPF */
1171 CvBlobTrackerOne* cvCreateBlobTrackerOneMSPF()
1173 return (CvBlobTrackerOne*) new CvBlobTrackerOneMSPF;
1176 CvBlobTracker* cvCreateBlobTrackerMSPF()
1178 return cvCreateBlobTrackerList(cvCreateBlobTrackerOneMSPF);