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*/
343 ~CvBlobTrackerOneMSFG()
346 if(m_HistModel) cvReleaseMat(&m_HistModel);
347 if(m_HistCandidate) cvReleaseMat(&m_HistCandidate);
348 if(m_HistTemp) cvReleaseMat(&m_HistTemp);
350 if(m_KernelHist) cvReleaseMat(&m_KernelHist);
351 if(m_KernelMeanShift) cvReleaseMat(&m_KernelMeanShift);
355 virtual void Init(CvBlob* pBlobInit, IplImage* pImg, IplImage* pImgFG = NULL)
357 int w = cvRound(CV_BLOB_WX(pBlobInit));
358 int h = cvRound(CV_BLOB_WY(pBlobInit));
359 if(w<CV_BLOB_MINW)w=CV_BLOB_MINW;
360 if(h<CV_BLOB_MINH)h=CV_BLOB_MINH;
363 if(w>pImg->width)w=pImg->width;
364 if(h>pImg->height)h=pImg->height;
368 CollectHist(pImg, pImgFG, pBlobInit, &m_HistModel);
369 m_Blob = pBlobInit[0];
372 virtual CvBlob* Process(CvBlob* pBlobPrev, IplImage* pImg, IplImage* pImgFG = NULL)
378 m_Blob = pBlobPrev[0];
381 { /* Check blob size and realloc kernels if it is necessary: */
382 int w = cvRound(m_Blob.w);
383 int h = cvRound(m_Blob.h);
384 if( w != m_ObjSize.width || h!=m_ObjSize.height)
387 /* after this ( w != m_ObjSize.width || h!=m_ObjSize.height) shoiuld be false */
389 } /* Check blob size and realloc kernels if it is necessary: */
392 for(iter=0; iter<m_IterNum; ++iter)
394 float newx=0,newy=0,sum=0;
398 //CvPoint Center = cvPoint(cvRound(m_Blob.x),cvRound(m_Blob.y));
399 CollectHist(pImg, NULL, &m_Blob, &m_HistCandidate);
400 B0 = calcBhattacharyya();
402 if(m_Wnd)if(CV_BLOB_ID(pBlobPrev)==0 && iter == 0)
404 IplImage* pW = cvCloneImage(pImgFG);
405 IplImage* pWFG = cvCloneImage(pImgFG);
411 assert(m_ObjSize.width < pImg->width);
412 assert(m_ObjSize.height < pImg->height);
414 /* Calculate shift vector: */
415 for(y=0; y<pImg->height; ++y)
417 unsigned char* pImgData = &CV_IMAGE_ELEM(pImg,unsigned char,y,0);
418 unsigned char* pMaskData = pImgFG?(&CV_IMAGE_ELEM(pImgFG,unsigned char,y,0)):NULL;
420 for(x=0; x<pImg->width; ++x, pImgData+=3)
422 int xk = cvRound(x-(m_Blob.x-m_Blob.w*0.5));
423 int yk = cvRound(y-(m_Blob.y-m_Blob.h*0.5));
427 int index = HIST_INDEX(pImgData);
428 assert(index >= 0 && index < m_BinNumTotal);
430 if(fabs(x-m_Blob.x)>m_Blob.w*0.6) continue;
431 if(fabs(y-m_Blob.y)>m_Blob.h*0.6) continue;
433 if(xk < 0 || xk >= m_KernelMeanShift->cols) continue;
434 if(yk < 0 || yk >= m_KernelMeanShift->rows) continue;
436 if(m_HistModel.m_HistVolume>0)
437 HM = ((DefHistType*)m_HistModel.m_pHist->data.ptr)[index]/m_HistModel.m_HistVolume;
439 if(m_HistCandidate.m_HistVolume>0)
440 HC = ((DefHistType*)m_HistCandidate.m_pHist->data.ptr)[index]/m_HistCandidate.m_HistVolume;
442 K = *(DefHistType*)CV_MAT_ELEM_PTR_FAST(m_KernelMeanShift[0],yk,xk,sizeof(DefHistType));
446 double V = sqrt(HM / HC);
447 int Vi = cvRound(V * 64);
449 if(Vi > 255) Vi = 255;
450 CV_IMAGE_ELEM(pW,uchar,y,x) = (uchar)Vi;
452 V += m_FGWeight*(pMaskData?(pMaskData[x]/255.0f):0);
454 Vi = cvRound(V * 64);
456 if(Vi > 255) Vi = 255;
457 CV_IMAGE_ELEM(pWFG,uchar,y,x) = (uchar)Vi;
463 //cvNamedWindow("MSFG_W",0);
464 //cvShowImage("MSFG_W",pW);
465 //cvNamedWindow("MSFG_WFG",0);
466 //cvShowImage("MSFG_WFG",pWFG);
467 //cvNamedWindow("MSFG_FG",0);
468 //cvShowImage("MSFG_FG",pImgFG);
470 //cvSaveImage("MSFG_W.bmp",pW);
471 //cvSaveImage("MSFG_WFG.bmp",pWFG);
472 //cvSaveImage("MSFG_FG.bmp",pImgFG);
477 /* Calculate new position by meanshift: */
479 /* Calculate new position: */
482 int x0 = cvRound(m_Blob.x - m_ObjSize.width*0.5);
483 int y0 = cvRound(m_Blob.y - m_ObjSize.height*0.5);
486 assert(m_ObjSize.width < pImg->width);
487 assert(m_ObjSize.height < pImg->height);
489 /* Crop blob bounds: */
490 if((x0+m_ObjSize.width)>=pImg->width) x0=pImg->width-m_ObjSize.width-1;
491 if((y0+m_ObjSize.height)>=pImg->height) y0=pImg->height-m_ObjSize.height-1;
495 /* Calculate shift vector: */
496 for(y=0; y<m_ObjSize.height; ++y)
498 unsigned char* pImgData = &CV_IMAGE_ELEM(pImg,unsigned char,y+y0,x0*3);
499 unsigned char* pMaskData = pImgFG?(&CV_IMAGE_ELEM(pImgFG,unsigned char,y+y0,x0)):NULL;
500 DefHistType* pKernelData = (DefHistType*)CV_MAT_ELEM_PTR_FAST(m_KernelMeanShift[0],y,0,sizeof(DefHistType));
502 for(x=0; x<m_ObjSize.width; ++x, pImgData+=3)
504 DefHistType K = pKernelData[x];
507 int index = HIST_INDEX(pImgData);
508 assert(index >= 0 && index < m_BinNumTotal);
510 if(m_HistModel.m_HistVolume>0)
511 HM = ((DefHistType*)m_HistModel.m_pHist->data.ptr)[index]/m_HistModel.m_HistVolume;
513 if(m_HistCandidate.m_HistVolume>0)
514 HC = ((DefHistType*)m_HistCandidate.m_pHist->data.ptr)[index]/m_HistCandidate.m_HistVolume;
518 double V = sqrt(HM / HC);
519 if(!m_Collision && m_FGWeight>0 && pMaskData)
521 V += m_FGWeight*(pMaskData[x]/255.0f);
523 K *= (float)MIN(V,100000.);
540 } /* if m_Dim == 3. */
542 /* Calculate new position by meanshift: */
545 { /* Iterate using bahattcharrya coefficient: */
548 // CvPoint NewCenter = cvPoint(cvRound(newx),cvRound(newy));
551 CollectHist(pImg, NULL, &B, &m_HistCandidate);
552 B1 = calcBhattacharyya();
554 newx = 0.5f*(newx+m_Blob.x);
555 newy = 0.5f*(newy+m_Blob.y);
556 if(fabs(newx-m_Blob.x)<0.1 && fabs(newy-m_Blob.y)<0.1) break;
557 } /* Iterate using bahattcharrya coefficient. */
559 if(fabs(newx-m_Blob.x)<0.5 && fabs(newy-m_Blob.y)<0.5) break;
562 } /* Next iteration. */
564 while(!m_Collision && m_FGWeight>0)
565 { /* Update size if no collision. */
568 double M00,X,Y,XX,YY;
573 r.width = cvRound(m_Blob.w*1.5+0.5);
574 r.height = cvRound(m_Blob.h*1.5+0.5);
575 r.x = cvRound(m_Blob.x - 0.5*r.width);
576 r.y = cvRound(m_Blob.y - 0.5*r.height);
579 if(r.x+r.width >= pImgFG->width) break;
580 if(r.y+r.height >= pImgFG->height) break;
581 if(r.height < 5 || r.width < 5) break;
583 cvMoments( cvGetSubRect(pImgFG,&mat,r), &m, 0 );
584 M00 = cvGetSpatialMoment( &m, 0, 0 );
586 X = cvGetSpatialMoment( &m, 1, 0 )/M00;
587 Y = cvGetSpatialMoment( &m, 0, 1 )/M00;
588 XX = (cvGetSpatialMoment( &m, 2, 0 )/M00) - X*X;
589 YY = (cvGetSpatialMoment( &m, 0, 2 )/M00) - Y*Y;
590 NewBlob = cvBlob(r.x+(float)X,r.y+(float)Y,(float)(4*sqrt(XX)),(float)(4*sqrt(YY)));
592 NewBlob.w = Alpha*NewBlob.w+m_Blob.w*(1-Alpha);
593 NewBlob.h = Alpha*NewBlob.h+m_Blob.h*(1-Alpha);
595 m_Blob.w = MAX(NewBlob.w,5);
596 m_Blob.h = MAX(NewBlob.h,5);
599 } /* Update size if no collision. */
603 }; /* CvBlobTrackerOneMSFG::Process */
605 virtual double GetConfidence(CvBlob* pBlob, IplImage* pImg, IplImage* /*pImgFG*/ = NULL, IplImage* pImgUnusedReg = NULL)
608 double B = GetBhattacharyya(pImg, pImgUnusedReg, pBlob, &m_HistTemp);
609 return exp((B-1)/(2*S));
611 }; /*CvBlobTrackerOneMSFG::*/
613 virtual void Update(CvBlob* pBlob, IplImage* pImg, IplImage* pImgFG = NULL)
614 { /* Update histogram: */
615 UpdateModelHist(pImg, pImgFG, pBlob?pBlob:&m_Blob);
616 } /*CvBlobTrackerOneMSFG::*/
618 virtual void Release(){delete this;};
619 virtual void SetCollision(int CollisionFlag)
621 m_Collision = CollisionFlag;
623 virtual void SaveState(CvFileStorage* fs)
625 cvWriteStruct(fs, "Blob", &m_Blob, "ffffi");
626 cvWriteInt(fs,"Collision", m_Collision);
627 cvWriteInt(fs,"HistVolume", cvRound(m_HistModel.m_HistVolume));
628 cvWrite(fs,"Hist", m_HistModel.m_pHist);
630 virtual void LoadState(CvFileStorage* fs, CvFileNode* node)
633 cvReadStructByName(fs, node, "Blob",&m_Blob, "ffffi");
634 m_Collision = cvReadIntByName(fs,node,"Collision",m_Collision);
635 pM = (CvMat*)cvRead(fs,cvGetFileNodeByName(fs,node,"Hist"));
638 m_HistModel.m_pHist = pM;
639 m_HistModel.m_HistVolume = (float)cvSum(pM).val[0];
643 }; /*CvBlobTrackerOneMSFG*/
646 void CvBlobTrackerOneMSFG::CollectHist(IplImage* pImg, IplImage* pMask, CvBlob* pBlob, DefHist* pHist)
648 int UsePrecalculatedKernel = 0;
649 int BW = cvRound(pBlob->w);
650 int BH = cvRound(pBlob->h);
651 DefHistType Volume = 0;
652 int x0 = cvRound(pBlob->x - BW*0.5);
653 int y0 = cvRound(pBlob->y - BH*0.5);
656 UsePrecalculatedKernel = (BW == m_ObjSize.width && BH == m_ObjSize.height ) ;
659 cvSet(pHist->m_pHist,cvScalar(1.0/m_BinNumTotal)); /* no zero bins, all bins have very small value*/
662 assert(BW < pImg->width);
663 assert(BH < pImg->height);
664 if((x0+BW)>=pImg->width) BW=pImg->width-x0-1;
665 if((y0+BH)>=pImg->height) BH=pImg->height-y0-1;
673 unsigned char* pImgData = &CV_IMAGE_ELEM(pImg,unsigned char,y+y0,x0*3);
674 unsigned char* pMaskData = pMask?(&CV_IMAGE_ELEM(pMask,unsigned char,y+y0,x0)):NULL;
675 DefHistType* pKernelData = NULL;
677 if(UsePrecalculatedKernel)
679 pKernelData = ((DefHistType*)CV_MAT_ELEM_PTR_FAST(m_KernelHist[0],y,0,sizeof(DefHistType)));
682 for(x=0; x<BW; ++x, pImgData+=3)
685 int index = HIST_INDEX(pImgData);
686 assert(index >= 0 && index < pHist->m_pHist->cols);
688 if(UsePrecalculatedKernel)
694 float dx = (x+x0-pBlob->x)/(pBlob->w*0.5);
695 float dy = (y+y0-pBlob->y)/(pBlob->h*0.5);
696 double r2 = dx*dx+dy*dy;
697 K = GetKernelHist(r2);
702 K *= pMaskData[x]*0.003921568627450980392156862745098;
705 ((DefHistType*)(pHist->m_pHist->data.ptr))[index] += K;
709 } /* if m_Dim == 3. */
711 pHist->m_HistVolume = Volume;
716 CvBlobTrackerOne* cvCreateBlobTrackerOneMSFG()
718 return (CvBlobTrackerOne*) new CvBlobTrackerOneMSFG;
721 CvBlobTracker* cvCreateBlobTrackerMSFG()
723 return cvCreateBlobTrackerList(cvCreateBlobTrackerOneMSFG);
726 /* Create specific tracker without FG
727 * usin - just simple mean-shift tracker: */
728 class CvBlobTrackerOneMS:public CvBlobTrackerOneMSFG
733 SetParam("FGWeight",0);
734 DelParam("FGWeight");
738 CvBlobTrackerOne* cvCreateBlobTrackerOneMS()
740 return (CvBlobTrackerOne*) new CvBlobTrackerOneMS;
743 CvBlobTracker* cvCreateBlobTrackerMS()
745 return cvCreateBlobTrackerList(cvCreateBlobTrackerOneMS);
748 typedef struct DefParticle
755 class CvBlobTrackerOneMSPF:public CvBlobTrackerOneMS
766 DefParticle* m_pParticlesPredicted;
767 DefParticle* m_pParticlesResampled;
771 DefHist* m_HistForParalel;
775 virtual void SaveState(CvFileStorage* fs)
777 CvBlobTrackerOneMS::SaveState(fs);
778 cvWriteInt(fs,"ParticleNum",m_ParticleNum);
779 cvWriteStruct(fs,"ParticlesPredicted",m_pParticlesPredicted,"ffffiffd",m_ParticleNum);
780 cvWriteStruct(fs,"ParticlesResampled",m_pParticlesResampled,"ffffiffd",m_ParticleNum);
783 virtual void LoadState(CvFileStorage* fs, CvFileNode* node)
786 CvBlobTrackerOneMS::LoadState(fs,node);
787 m_ParticleNum = cvReadIntByName(fs,node,"ParticleNum",m_ParticleNum);
791 printf("sizeof(DefParticle) is %d\n", sizeof(DefParticle));
792 cvReadStructByName(fs,node,"ParticlesPredicted",m_pParticlesPredicted,"ffffiffd");
793 cvReadStructByName(fs,node,"ParticlesResampled",m_pParticlesResampled,"ffffiffd");
796 CvBlobTrackerOneMSPF()
798 m_pParticlesPredicted = NULL;
799 m_pParticlesResampled = NULL;
802 AddParam("ParticleNum",&m_ParticleNum);
803 CommentParam("ParticleNum","Number of particles");
807 AddParam("UseVel",&m_UseVel);
808 CommentParam("UseVel","Percent of particles which use velocity feature");
811 AddParam("SizeVar",&m_SizeVar);
812 CommentParam("SizeVar","Size variation (in object size)");
815 AddParam("PosVar",&m_PosVar);
816 CommentParam("PosVar","Position variation (in object size)");
821 m_ThreadNum = omp_get_num_procs();
822 m_HistForParalel = new DefHist[m_ThreadNum];
827 ~CvBlobTrackerOneMSPF()
829 if(m_pParticlesResampled)cvFree(&m_pParticlesResampled);
830 if(m_pParticlesPredicted)cvFree(&m_pParticlesPredicted);
832 if(m_HistForParalel) delete[] m_HistForParalel;
839 if(m_pParticlesResampled)cvFree(&m_pParticlesResampled);
840 if(m_pParticlesPredicted)cvFree(&m_pParticlesPredicted);
841 m_pParticlesPredicted = (DefParticle*)cvAlloc(sizeof(DefParticle)*m_ParticleNum);
842 m_pParticlesResampled = (DefParticle*)cvAlloc(sizeof(DefParticle)*m_ParticleNum);
845 void DrawDebug(IplImage* pImg, IplImage* /*pImgFG*/)
850 DefParticle* pBP = k?m_pParticlesResampled:m_pParticlesPredicted;
851 //const char* name = k?"MSPF resampled particle":"MSPF Predicted particle";
852 IplImage* pI = cvCloneImage(pImg);
853 int h,hN = m_ParticleNum;
854 CvBlob C = cvBlob(0,0,0,0);
858 CvBlob B = pBP[h].blob;
859 int CW = cvRound(255*pBP[h].W);
861 int x = cvRound(CV_BLOB_RX(pB)), y = cvRound(CV_BLOB_RY(pB));
862 CvSize s = cvSize(MAX(1,x), MAX(1,y));
872 cvPointFrom32f(CV_BLOB_CENTER(pB)),
877 } /* Next hypothesis. */
885 cvPointFrom32f(CV_BLOB_CENTER(&C)),
886 cvSize(cvRound(C.w*0.5),cvRound(C.h*0.5)),
888 CV_RGB(0,0,255), 1 );
891 cvPointFrom32f(CV_BLOB_CENTER(&m_Blob)),
892 cvSize(cvRound(m_Blob.w*0.5),cvRound(m_Blob.h*0.5)),
894 CV_RGB(0,255,0), 1 );
896 //cvNamedWindow(name,0);
897 //cvShowImage(name,pI);
901 //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);
908 for(p=0; p<m_ParticleNum; ++p)
909 { /* "Prediction" of particle: */
912 CvMat rm = cvMat(1,5,CV_32F,r);
913 cvRandArr(&m_RNG,&rm,CV_RAND_NORMAL,cvScalar(0),cvScalar(1));
915 m_pParticlesPredicted[p] = m_pParticlesResampled[p];
917 if(cvRandReal(&m_RNG)<0.5)
918 { /* Half of particles will predict based on external blob: */
919 m_pParticlesPredicted[p].blob = m_Blob;
922 if(cvRandReal(&m_RNG)<m_UseVel)
923 { /* Predict moving particle by usual way by using speed: */
924 m_pParticlesPredicted[p].blob.x += m_pParticlesPredicted[p].Vx;
925 m_pParticlesPredicted[p].blob.y += m_pParticlesPredicted[p].Vy;
928 { /* Stop several particles: */
929 m_pParticlesPredicted[p].Vx = 0;
930 m_pParticlesPredicted[p].Vy = 0;
933 { /* Update position: */
934 float S = (m_Blob.w + m_Blob.h)*0.5f;
935 m_pParticlesPredicted[p].blob.x += m_PosVar*S*r[0];
936 m_pParticlesPredicted[p].blob.y += m_PosVar*S*r[1];
938 /* Update velocity: */
939 m_pParticlesPredicted[p].Vx += (float)(m_PosVar*S*0.1*r[3]);
940 m_pParticlesPredicted[p].Vy += (float)(m_PosVar*S*0.1*r[4]);
944 m_pParticlesPredicted[p].blob.w *= (1+m_SizeVar*r[2]);
945 m_pParticlesPredicted[p].blob.h *= (1+m_SizeVar*r[2]);
947 /* Truncate size of particle: */
948 if(m_pParticlesPredicted[p].blob.w > m_ImgSize.width*0.5f)
950 m_pParticlesPredicted[p].blob.w = m_ImgSize.width*0.5f;
953 if(m_pParticlesPredicted[p].blob.h > m_ImgSize.height*0.5f)
955 m_pParticlesPredicted[p].blob.h = m_ImgSize.height*0.5f;
958 if(m_pParticlesPredicted[p].blob.w < 1 )
960 m_pParticlesPredicted[p].blob.w = 1;
963 if(m_pParticlesPredicted[p].blob.h < 1)
965 m_pParticlesPredicted[p].blob.h = 1;
967 } /* "Prediction" of particle. */
970 void UpdateWeightsMS(IplImage* pImg, IplImage* /*pImgFG*/)
974 if( m_HistForParalel[0].m_pHist==NULL || m_HistForParalel[0].m_pHist->cols != m_BinNumTotal)
977 for(t=0; t<m_ThreadNum; ++t)
978 m_HistForParalel[t].Resize(m_BinNumTotal);
983 #pragma omp parallel for num_threads(m_ThreadNum),schedule(runtime)
985 for(p=0;p<m_ParticleNum;++p)
986 { /* Calculate weights for particles: */
990 assert(omp_get_thread_num()<m_ThreadNum);
993 B = GetBhattacharyya(
995 &(m_pParticlesPredicted[p].blob)
997 ,&(m_HistForParalel[omp_get_thread_num()])
1000 m_pParticlesPredicted[p].W *= exp((B-1)/(2*S));
1002 } /* Calculate weights for particles. */
1005 void UpdateWeightsCC(IplImage* /*pImg*/, IplImage* /*pImgFG*/)
1009 #pragma omp parallel for
1011 for(p=0; p<m_ParticleNum; ++p)
1012 { /* Calculate weights for particles: */
1014 m_pParticlesPredicted[p].W *= W;
1015 } /* Calculate weights for particles. */
1019 { /* Resample particle: */
1023 for(p=0; p<m_ParticleNum; ++p)
1025 Sum += m_pParticlesPredicted[p].W;
1028 for(p=0; p<m_ParticleNum; ++p)
1030 double T = Sum * cvRandReal(&m_RNG); /* Set current random threshold for cululative weight. */
1034 for(p2=0; p2<m_ParticleNum; ++p2)
1036 Sum2 += m_pParticlesPredicted[p2].W;
1040 if(p2>=m_ParticleNum)p2=m_ParticleNum-1;
1041 m_pParticlesResampled[p] = m_pParticlesPredicted[p2];
1042 m_pParticlesResampled[p].W = 1;
1044 } /* Find next particle. */
1045 } /* Resample particle. */
1049 virtual void Init(CvBlob* pBlobInit, IplImage* pImg, IplImage* pImgFG = NULL)
1052 CvBlobTrackerOneMSFG::Init(pBlobInit, pImg, pImgFG);
1057 PP.blob = pBlobInit[0];
1058 for(i=0;i<m_ParticleNum;++i)
1060 m_pParticlesPredicted[i] = PP;
1061 m_pParticlesResampled[i] = PP;
1063 m_Blob = pBlobInit[0];
1065 } /* CvBlobTrackerOneMSPF::Init*/
1067 virtual CvBlob* Process(CvBlob* pBlobPrev, IplImage* pImg, IplImage* pImgFG = NULL)
1071 m_ImgSize.width = pImg->width;
1072 m_ImgSize.height = pImg->height;
1075 m_Blob = pBlobPrev[0];
1077 { /* Check blob size and realloc kernels if it is necessary: */
1078 int w = cvRound(m_Blob.w);
1079 int h = cvRound(m_Blob.h);
1080 if( w != m_ObjSize.width || h!=m_ObjSize.height)
1083 /* After this ( w != m_ObjSize.width || h!=m_ObjSize.height) should be false. */
1085 } /* Check blob size and realloc kernels if it is necessary. */
1090 int64 ticks = cvGetTickCount();
1093 UpdateWeightsMS(pImg, pImgFG);
1096 ticks = cvGetTickCount() - ticks;
1097 fprintf(stderr, "PF UpdateWeights, %d ticks\n", (int)ticks);
1098 ticks = cvGetTickCount();
1104 ticks = cvGetTickCount() - ticks;
1105 fprintf(stderr, "PF Resampling, %d ticks\n", (int)ticks);
1108 { /* Find average result: */
1115 DefParticle* pP = m_pParticlesResampled;
1117 for(p=0; p<m_ParticleNum; ++p)
1119 float W = (float)pP[p].W;
1120 x += W*pP[p].blob.x;
1121 y += W*pP[p].blob.y;
1122 w += W*pP[p].blob.w;
1123 h += W*pP[p].blob.h;
1134 } /* Find average result. */
1138 DrawDebug(pImg, pImgFG);
1143 } /* CvBlobTrackerOneMSPF::Process */
1145 virtual void SkipProcess(CvBlob* pBlob, IplImage* /*pImg*/, IplImage* /*pImgFG*/ = NULL)
1148 for(p=0; p<m_ParticleNum; ++p)
1150 m_pParticlesResampled[p].blob = pBlob[0];
1151 m_pParticlesResampled[p].Vx = 0;
1152 m_pParticlesResampled[p].Vy = 0;
1153 m_pParticlesResampled[p].W = 1;
1157 virtual void Release(){delete this;};
1158 virtual void ParamUpdate()
1163 }; /* CvBlobTrackerOneMSPF */
1165 CvBlobTrackerOne* cvCreateBlobTrackerOneMSPF()
1167 return (CvBlobTrackerOne*) new CvBlobTrackerOneMSPF;
1170 CvBlobTracker* cvCreateBlobTrackerMSPF()
1172 return cvCreateBlobTrackerList(cvCreateBlobTrackerOneMSPF);