496b8be20e4e44bb82720f6b8f0aac7e5072b993
[platform/upstream/opencv.git] / modules / legacy / include / opencv2 / legacy / blobtrack.hpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of Intel Corporation may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41
42
43 #ifndef __OPENCV_VIDEOSURVEILLANCE_H__
44 #define __OPENCV_VIDEOSURVEILLANCE_H__
45
46 /* Turn off the functionality until cvaux/src/Makefile.am gets updated: */
47 //#if _MSC_VER >= 1200
48
49 #include "opencv2/core/core_c.h"
50 #include <stdio.h>
51
52 #if (defined _MSC_VER && _MSC_VER >= 1200) || defined __BORLANDC__
53 #define cv_stricmp stricmp
54 #define cv_strnicmp strnicmp
55 #if defined WINCE
56 #define strdup _strdup
57 #define stricmp _stricmp
58 #endif
59 #elif defined __GNUC__ || defined __sun
60 #define cv_stricmp strcasecmp
61 #define cv_strnicmp strncasecmp
62 #else
63 #error Do not know how to make case-insensitive string comparison on this platform
64 #endif
65
66 //struct DefParam;
67 struct CvDefParam
68 {
69     struct CvDefParam*    next;
70     char*               pName;
71     char*               pComment;
72     double*             pDouble;
73     double              Double;
74     float*              pFloat;
75     float               Float;
76     int*                pInt;
77     int                 Int;
78     char**              pStr;
79     char*               Str;
80 };
81
82 class CV_EXPORTS CvVSModule
83 {
84 private: /* Internal data: */
85     CvDefParam*   m_pParamList;
86     char*       m_pModuleTypeName;
87     char*       m_pModuleName;
88     char*       m_pNickName;
89 protected:
90     int         m_Wnd;
91 public: /* Constructor and destructor: */
92     CvVSModule();
93     virtual ~CvVSModule();
94 private: /* Internal functions: */
95     void    FreeParam(CvDefParam** pp);
96     CvDefParam* NewParam(const char* name);
97     CvDefParam* GetParamPtr(int index);
98     CvDefParam* GetParamPtr(const char* name);
99 protected: /* INTERNAL INTERFACE */
100     int  IsParam(const char* name);
101     void AddParam(const char* name, double* pAddr);
102     void AddParam(const char* name, float* pAddr);
103     void AddParam(const char* name, int* pAddr);
104     void AddParam(const char* name, const char** pAddr);
105     void AddParam(const char* name);
106     void CommentParam(const char* name, const char* pComment);
107     void SetTypeName(const char* name);
108     void SetModuleName(const char* name);
109     void DelParam(const char* name);
110
111 public: /* EXTERNAL INTERFACE */
112     const char* GetParamName(int index);
113     const char* GetParamComment(const char* name);
114     double GetParam(const char* name);
115     const char* GetParamStr(const char* name);
116     void   SetParam(const char* name, double val);
117     void   SetParamStr(const char* name, const char* str);
118     void TransferParamsFromChild(CvVSModule* pM, const char* prefix = NULL);
119     void TransferParamsToChild(CvVSModule* pM, char* prefix = NULL);
120     virtual void ParamUpdate();
121     const char*   GetTypeName();
122     int     IsModuleTypeName(const char* name);
123     char*   GetModuleName();
124     int     IsModuleName(const char* name);
125     void SetNickName(const char* pStr);
126     const char* GetNickName();
127     virtual void SaveState(CvFileStorage*);
128     virtual void LoadState(CvFileStorage*, CvFileNode*);
129
130     virtual void Release() = 0;
131 };/* CvVMModule */
132
133 CV_EXPORTS void cvWriteStruct(CvFileStorage* fs, const char* name, void* addr, const char* desc, int num=1);
134 CV_EXPORTS void cvReadStructByName(CvFileStorage* fs, CvFileNode* node, const char* name, void* addr, const char* desc);
135
136 /* FOREGROUND DETECTOR INTERFACE */
137 class CV_EXPORTS CvFGDetector : public CvVSModule
138 {
139 public:
140     CvFGDetector();
141     virtual IplImage* GetMask() = 0;
142     /* Process current image: */
143     virtual void    Process(IplImage* pImg) = 0;
144     /* Release foreground detector: */
145     virtual void    Release() = 0;
146 };
147
148 CV_EXPORTS void cvReleaseFGDetector(CvFGDetector** ppT );
149 CV_EXPORTS CvFGDetector* cvCreateFGDetectorBase(int type, void *param);
150
151
152 /* BLOB STRUCTURE*/
153 struct CvBlob
154 {
155     float   x,y; /* blob position   */
156     float   w,h; /* blob sizes      */
157     int     ID;  /* blob ID         */
158 };
159
160 inline CvBlob cvBlob(float x,float y, float w, float h)
161 {
162     CvBlob B = {x,y,w,h,0};
163     return B;
164 }
165 #define CV_BLOB_MINW 5
166 #define CV_BLOB_MINH 5
167 #define CV_BLOB_ID(pB) (((CvBlob*)(pB))->ID)
168 #define CV_BLOB_CENTER(pB) cvPoint2D32f(((CvBlob*)(pB))->x,((CvBlob*)(pB))->y)
169 #define CV_BLOB_X(pB) (((CvBlob*)(pB))->x)
170 #define CV_BLOB_Y(pB) (((CvBlob*)(pB))->y)
171 #define CV_BLOB_WX(pB) (((CvBlob*)(pB))->w)
172 #define CV_BLOB_WY(pB) (((CvBlob*)(pB))->h)
173 #define CV_BLOB_RX(pB) (0.5f*CV_BLOB_WX(pB))
174 #define CV_BLOB_RY(pB) (0.5f*CV_BLOB_WY(pB))
175 #define CV_BLOB_RECT(pB) cvRect(cvRound(((CvBlob*)(pB))->x-CV_BLOB_RX(pB)),cvRound(((CvBlob*)(pB))->y-CV_BLOB_RY(pB)),cvRound(CV_BLOB_WX(pB)),cvRound(CV_BLOB_WY(pB)))
176 /* END BLOB STRUCTURE*/
177
178
179 /* simple BLOBLIST */
180 class CV_EXPORTS CvBlobSeq
181 {
182 public:
183     CvBlobSeq(int BlobSize = sizeof(CvBlob))
184     {
185         m_pMem = cvCreateMemStorage();
186         m_pSeq = cvCreateSeq(0,sizeof(CvSeq),BlobSize,m_pMem);
187         strcpy(m_pElemFormat,"ffffi");
188     }
189     virtual ~CvBlobSeq()
190     {
191         cvReleaseMemStorage(&m_pMem);
192     };
193     virtual CvBlob* GetBlob(int BlobIndex)
194     {
195         return (CvBlob*)cvGetSeqElem(m_pSeq,BlobIndex);
196     };
197     virtual CvBlob* GetBlobByID(int BlobID)
198     {
199         int i;
200         for(i=0; i<m_pSeq->total; ++i)
201             if(BlobID == CV_BLOB_ID(GetBlob(i)))
202                 return GetBlob(i);
203         return NULL;
204     };
205     virtual void DelBlob(int BlobIndex)
206     {
207         cvSeqRemove(m_pSeq,BlobIndex);
208     };
209     virtual void DelBlobByID(int BlobID)
210     {
211         int i;
212         for(i=0; i<m_pSeq->total; ++i)
213         {
214             if(BlobID == CV_BLOB_ID(GetBlob(i)))
215             {
216                 DelBlob(i);
217                 return;
218             }
219         }
220     };
221     virtual void Clear()
222     {
223         cvClearSeq(m_pSeq);
224     };
225     virtual void AddBlob(CvBlob* pB)
226     {
227         cvSeqPush(m_pSeq,pB);
228     };
229     virtual int GetBlobNum()
230     {
231         return m_pSeq->total;
232     };
233     virtual void Write(CvFileStorage* fs, const char* name)
234     {
235         const char*  attr[] = {"dt",m_pElemFormat,NULL};
236         if(fs)
237         {
238             cvWrite(fs,name,m_pSeq,cvAttrList(attr,NULL));
239         }
240     }
241     virtual void Load(CvFileStorage* fs, CvFileNode* node)
242     {
243         if(fs==NULL) return;
244         CvSeq* pSeq = (CvSeq*)cvRead(fs, node);
245         if(pSeq)
246         {
247             int i;
248             cvClearSeq(m_pSeq);
249             for(i=0;i<pSeq->total;++i)
250             {
251                 void* pB = cvGetSeqElem( pSeq, i );
252                 cvSeqPush( m_pSeq, pB );
253             }
254         }
255     }
256     void AddFormat(const char* str){strcat(m_pElemFormat,str);}
257 protected:
258     CvMemStorage*   m_pMem;
259     CvSeq*          m_pSeq;
260     char            m_pElemFormat[1024];
261 };
262 /* simple BLOBLIST */
263
264
265 /* simple TRACKLIST */
266 struct CvBlobTrack
267 {
268     int         TrackID;
269     int         StartFrame;
270     CvBlobSeq*  pBlobSeq;
271 };
272
273 class CV_EXPORTS CvBlobTrackSeq
274 {
275 public:
276     CvBlobTrackSeq(int TrackSize = sizeof(CvBlobTrack));
277     virtual ~CvBlobTrackSeq();
278     virtual CvBlobTrack* GetBlobTrack(int TrackIndex);
279     virtual CvBlobTrack* GetBlobTrackByID(int TrackID);
280     virtual void DelBlobTrack(int TrackIndex);
281     virtual void DelBlobTrackByID(int TrackID);
282     virtual void Clear();
283     virtual void AddBlobTrack(int TrackID, int StartFrame = 0);
284     virtual int GetBlobTrackNum();
285 protected:
286     CvMemStorage*   m_pMem;
287     CvSeq*          m_pSeq;
288 };
289
290 /* simple TRACKLIST */
291
292
293 /* BLOB DETECTOR INTERFACE */
294 class CV_EXPORTS CvBlobDetector: public CvVSModule
295 {
296 public:
297     CvBlobDetector(){SetTypeName("BlobDetector");};
298     /* Try to detect new blob entrance based on foreground mask. */
299     /* pFGMask - image of foreground mask */
300     /* pNewBlob - pointer to CvBlob structure which will be filled if new blob entrance detected */
301     /* pOldBlobList - pointer to blob list which already exist on image */
302     virtual int DetectNewBlob(IplImage* pImg, IplImage* pImgFG, CvBlobSeq* pNewBlobList, CvBlobSeq* pOldBlobList) = 0;
303     /* release blob detector */
304     virtual void Release()=0;
305 };
306
307 /* Release any blob detector: */
308 CV_EXPORTS void cvReleaseBlobDetector(CvBlobDetector** ppBD);
309
310 /* Declarations of constructors of implemented modules: */
311 CV_EXPORTS CvBlobDetector* cvCreateBlobDetectorSimple();
312 CV_EXPORTS CvBlobDetector* cvCreateBlobDetectorCC();
313
314 struct CV_EXPORTS CvDetectedBlob : public CvBlob
315 {
316     float response;
317 };
318
319 CV_INLINE CvDetectedBlob cvDetectedBlob( float x, float y, float w, float h, int ID = 0, float response = 0.0F )
320 {
321     CvDetectedBlob b;
322     b.x = x; b.y = y; b.w = w; b.h = h; b.ID = ID; b.response = response;
323     return b;
324 }
325
326
327 class CV_EXPORTS CvObjectDetector
328 {
329 public:
330     CvObjectDetector( const char* /*detector_file_name*/ = 0 );
331     ~CvObjectDetector();
332
333     /*
334      * Release the current detector and load new detector from file
335      * (if detector_file_name is not 0)
336      * Return true on success:
337      */
338     bool Load( const char* /*detector_file_name*/ = 0 );
339
340     /* Return min detector window size: */
341     CvSize GetMinWindowSize() const;
342
343     /* Return max border: */
344     int GetMaxBorderSize() const;
345
346     /*
347      * Detect the object on the image and push the detected
348      * blobs into <detected_blob_seq> which must be the sequence of <CvDetectedBlob>s
349      */
350     void Detect( const CvArr* /*img*/, /* out */ CvBlobSeq* /*detected_blob_seq*/ = 0 );
351
352 protected:
353     class CvObjectDetectorImpl* impl;
354 };
355
356
357 CV_INLINE CvRect cvRectIntersection( const CvRect r1, const CvRect r2 )
358 {
359     CvRect r = cvRect( MAX(r1.x, r2.x), MAX(r1.y, r2.y), 0, 0 );
360
361     r.width  = MIN(r1.x + r1.width, r2.x + r2.width) - r.x;
362     r.height = MIN(r1.y + r1.height, r2.y + r2.height) - r.y;
363
364     return r;
365 }
366
367
368 /*
369  * CvImageDrawer
370  *
371  * Draw on an image the specified ROIs from the source image and
372  * given blobs as ellipses or rectangles:
373  */
374
375 struct CvDrawShape
376 {
377     enum {RECT, ELLIPSE} shape;
378     CvScalar color;
379 };
380
381 /*extern const CvDrawShape icv_shape[] =
382 {
383     { CvDrawShape::ELLIPSE, CV_RGB(255,0,0) },
384     { CvDrawShape::ELLIPSE, CV_RGB(0,255,0) },
385     { CvDrawShape::ELLIPSE, CV_RGB(0,0,255) },
386     { CvDrawShape::ELLIPSE, CV_RGB(255,255,0) },
387     { CvDrawShape::ELLIPSE, CV_RGB(0,255,255) },
388     { CvDrawShape::ELLIPSE, CV_RGB(255,0,255) }
389 };*/
390
391 class CV_EXPORTS CvImageDrawer
392 {
393 public:
394     CvImageDrawer() : m_image(0) {}
395     ~CvImageDrawer() { cvReleaseImage( &m_image ); }
396     void SetShapes( const CvDrawShape* shapes, int num );
397     /* <blob_seq> must be the sequence of <CvDetectedBlob>s */
398     IplImage* Draw( const CvArr* src, CvBlobSeq* blob_seq = 0, const CvSeq* roi_seq = 0 );
399     IplImage* GetImage() { return m_image; }
400 protected:
401     //static const int MAX_SHAPES = sizeof(icv_shape) / sizeof(icv_shape[0]);;
402
403     IplImage* m_image;
404     CvDrawShape m_shape[16];
405 };
406
407
408
409 /* Trajectory generation module: */
410 class CV_EXPORTS CvBlobTrackGen: public CvVSModule
411 {
412 public:
413     CvBlobTrackGen(){SetTypeName("BlobTrackGen");};
414     virtual void    SetFileName(char* pFileName) = 0;
415     virtual void    AddBlob(CvBlob* pBlob) = 0;
416     virtual void    Process(IplImage* pImg = NULL, IplImage* pFG = NULL) = 0;
417     virtual void    Release() = 0;
418 };
419
420 inline void cvReleaseBlobTrackGen(CvBlobTrackGen** pBTGen)
421 {
422     if(*pBTGen)(*pBTGen)->Release();
423     *pBTGen = 0;
424 }
425
426 /* Declarations of constructors of implemented modules: */
427 CV_EXPORTS CvBlobTrackGen* cvCreateModuleBlobTrackGen1();
428 CV_EXPORTS CvBlobTrackGen* cvCreateModuleBlobTrackGenYML();
429
430
431
432 /* BLOB TRACKER INTERFACE */
433 class CV_EXPORTS CvBlobTracker: public CvVSModule
434 {
435 public:
436     CvBlobTracker();
437
438     /* Add new blob to track it and assign to this blob personal ID */
439     /* pBlob - pointer to structure with blob parameters (ID is ignored)*/
440     /* pImg - current image */
441     /* pImgFG - current foreground mask */
442     /* Return pointer to new added blob: */
443     virtual CvBlob* AddBlob(CvBlob* pBlob, IplImage* pImg, IplImage* pImgFG = NULL ) = 0;
444
445     /* Return number of currently tracked blobs: */
446     virtual int     GetBlobNum() = 0;
447
448     /* Return pointer to specified by index blob: */
449     virtual CvBlob* GetBlob(int BlobIndex) = 0;
450
451     /* Delete blob by its index: */
452     virtual void    DelBlob(int BlobIndex) = 0;
453
454     /* Process current image and track all existed blobs: */
455     virtual void    Process(IplImage* pImg, IplImage* pImgFG = NULL) = 0;
456
457     /* Release blob tracker: */
458     virtual void    Release() = 0;
459
460
461     /* Process one blob (for multi hypothesis tracing): */
462     virtual void ProcessBlob(int BlobIndex, CvBlob* pBlob, IplImage* /*pImg*/, IplImage* /*pImgFG*/ = NULL);
463
464     /* Get confidence/wieght/probability (0-1) for blob: */
465     virtual double  GetConfidence(int /*BlobIndex*/, CvBlob* /*pBlob*/, IplImage* /*pImg*/, IplImage* /*pImgFG*/ = NULL);
466
467     virtual double GetConfidenceList(CvBlobSeq* pBlobList, IplImage* pImg, IplImage* pImgFG = NULL);
468
469     virtual void UpdateBlob(int /*BlobIndex*/, CvBlob* /*pBlob*/, IplImage* /*pImg*/, IplImage* /*pImgFG*/ = NULL);
470
471     /* Update all blob models: */
472     virtual void Update(IplImage* pImg, IplImage* pImgFG = NULL);
473
474     /* Return pointer to blob by its unique ID: */
475     virtual int     GetBlobIndexByID(int BlobID);
476
477     /* Return pointer to blob by its unique ID: */
478     virtual CvBlob* GetBlobByID(int BlobID);
479
480     /* Delete blob by its ID: */
481     virtual void    DelBlobByID(int BlobID);
482
483     /* Set new parameters for specified (by index) blob: */
484     virtual void    SetBlob(int /*BlobIndex*/, CvBlob* /*pBlob*/);
485
486     /* Set new parameters for specified (by ID) blob: */
487     virtual void    SetBlobByID(int BlobID, CvBlob* pBlob);
488
489     /*  ===============  MULTI HYPOTHESIS INTERFACE ==================  */
490
491     /* Return number of position hyposetis of currently tracked blob: */
492     virtual int     GetBlobHypNum(int /*BlobIdx*/);
493
494     /* Return pointer to specified blob hypothesis by index blob: */
495     virtual CvBlob* GetBlobHyp(int BlobIndex, int /*hypothesis*/);
496
497     /* Set new parameters for specified (by index) blob hyp
498      * (can be called several times for each hyp ):
499      */
500     virtual void    SetBlobHyp(int /*BlobIndex*/, CvBlob* /*pBlob*/);
501 };
502
503 CV_EXPORTS void cvReleaseBlobTracker(CvBlobTracker**ppT );
504 /* BLOB TRACKER INTERFACE */
505
506 /*BLOB TRACKER ONE INTERFACE */
507 class CV_EXPORTS CvBlobTrackerOne : public CvVSModule
508 {
509 public:
510     virtual void Init(CvBlob* pBlobInit, IplImage* pImg, IplImage* pImgFG = NULL) = 0;
511     virtual CvBlob* Process(CvBlob* pBlobPrev, IplImage* pImg, IplImage* pImgFG = NULL) = 0;
512     virtual void Release() =  0;
513
514     /* Non-required methods: */
515     virtual void SkipProcess(CvBlob* /*pBlobPrev*/, IplImage* /*pImg*/, IplImage* /*pImgFG*/ = NULL){};
516     virtual void Update(CvBlob* /*pBlob*/, IplImage* /*pImg*/, IplImage* /*pImgFG*/ = NULL){};
517     virtual void SetCollision(int /*CollisionFlag*/){}; /* call in case of blob collision situation*/
518     virtual double GetConfidence(CvBlob* /*pBlob*/, IplImage* /*pImg*/,
519                                  IplImage* /*pImgFG*/ = NULL, IplImage* /*pImgUnusedReg*/ = NULL)
520     {
521         return 1;
522     };
523 };
524 inline void cvReleaseBlobTrackerOne(CvBlobTrackerOne **ppT )
525 {
526     ppT[0]->Release();
527     ppT[0] = 0;
528 }
529 CV_EXPORTS CvBlobTracker* cvCreateBlobTrackerList(CvBlobTrackerOne* (*create)());
530 /*BLOB TRACKER ONE INTERFACE */
531
532 /* Declarations of constructors of implemented modules: */
533
534 /* Some declarations for specific MeanShift tracker: */
535 #define PROFILE_EPANECHNIKOV    0
536 #define PROFILE_DOG             1
537 struct CvBlobTrackerParamMS
538 {
539     int     noOfSigBits;
540     int     appearance_profile;
541     int     meanshift_profile;
542     float   sigma;
543 };
544
545 CV_EXPORTS CvBlobTracker* cvCreateBlobTrackerMS1(CvBlobTrackerParamMS* param);
546 CV_EXPORTS CvBlobTracker* cvCreateBlobTrackerMS2(CvBlobTrackerParamMS* param);
547 CV_EXPORTS CvBlobTracker* cvCreateBlobTrackerMS1ByList();
548
549 /* Some declarations for specific Likelihood tracker: */
550 struct CvBlobTrackerParamLH
551 {
552     int     HistType; /* see Prob.h */
553     int     ScaleAfter;
554 };
555
556 /* Without scale optimization: */
557 CV_EXPORTS CvBlobTracker* cvCreateBlobTrackerLHR(CvBlobTrackerParamLH* /*param*/ = NULL);
558
559 /* With scale optimization: */
560 CV_EXPORTS CvBlobTracker* cvCreateBlobTrackerLHRS(CvBlobTrackerParamLH* /*param*/ = NULL);
561
562 /* Simple blob tracker based on connected component tracking: */
563 CV_EXPORTS CvBlobTracker* cvCreateBlobTrackerCC();
564
565 /* Connected component tracking and mean-shift particle filter collion-resolver: */
566 CV_EXPORTS CvBlobTracker* cvCreateBlobTrackerCCMSPF();
567
568 /* Blob tracker that integrates meanshift and connected components: */
569 CV_EXPORTS CvBlobTracker* cvCreateBlobTrackerMSFG();
570 CV_EXPORTS CvBlobTracker* cvCreateBlobTrackerMSFGS();
571
572 /* Meanshift without connected-components */
573 CV_EXPORTS CvBlobTracker* cvCreateBlobTrackerMS();
574
575 /* Particle filtering via Bhattacharya coefficient, which        */
576 /* is roughly the dot-product of two probability densities.      */
577 /* See: Real-Time Tracking of Non-Rigid Objects using Mean Shift */
578 /*      Comanicius, Ramesh, Meer, 2000, 8p                       */
579 /*      http://citeseer.ist.psu.edu/321441.html                  */
580 CV_EXPORTS CvBlobTracker* cvCreateBlobTrackerMSPF();
581
582 /* =========== tracker integrators trackers =============*/
583
584 /* Integrator based on Particle Filtering method: */
585 //CV_EXPORTS CvBlobTracker* cvCreateBlobTrackerIPF();
586
587 /* Rule based integrator: */
588 //CV_EXPORTS CvBlobTracker* cvCreateBlobTrackerIRB();
589
590 /* Integrator based on data fusion using particle filtering: */
591 //CV_EXPORTS CvBlobTracker* cvCreateBlobTrackerIPFDF();
592
593
594
595
596 /* Trajectory postprocessing module: */
597 class CV_EXPORTS CvBlobTrackPostProc: public CvVSModule
598 {
599 public:
600     CvBlobTrackPostProc(){SetTypeName("BlobTrackPostProc");};
601     virtual void    AddBlob(CvBlob* pBlob) = 0;
602     virtual void    Process() = 0;
603     virtual int     GetBlobNum() = 0;
604     virtual CvBlob* GetBlob(int index) = 0;
605     virtual void    Release() = 0;
606
607     /* Additional functionality: */
608     virtual CvBlob* GetBlobByID(int BlobID)
609     {
610         int i;
611         for(i=GetBlobNum();i>0;i--)
612         {
613             CvBlob* pB=GetBlob(i-1);
614             if(pB->ID==BlobID) return pB;
615         }
616         return NULL;
617     };
618 };
619
620 inline void cvReleaseBlobTrackPostProc(CvBlobTrackPostProc** pBTPP)
621 {
622     if(pBTPP == NULL) return;
623     if(*pBTPP)(*pBTPP)->Release();
624     *pBTPP = 0;
625 }
626
627 /* Trajectory generation module: */
628 class CV_EXPORTS CvBlobTrackPostProcOne: public CvVSModule
629 {
630 public:
631     CvBlobTrackPostProcOne(){SetTypeName("BlobTrackPostOne");};
632     virtual CvBlob* Process(CvBlob* pBlob) = 0;
633     virtual void    Release() = 0;
634 };
635
636 /* Create blob tracking post processing module based on simle module: */
637 CV_EXPORTS CvBlobTrackPostProc* cvCreateBlobTrackPostProcList(CvBlobTrackPostProcOne* (*create)());
638
639
640 /* Declarations of constructors of implemented modules: */
641 CV_EXPORTS CvBlobTrackPostProc* cvCreateModuleBlobTrackPostProcKalman();
642 CV_EXPORTS CvBlobTrackPostProc* cvCreateModuleBlobTrackPostProcTimeAverRect();
643 CV_EXPORTS CvBlobTrackPostProc* cvCreateModuleBlobTrackPostProcTimeAverExp();
644
645
646 /* PREDICTORS */
647 /* blob PREDICTOR */
648 class CvBlobTrackPredictor: public CvVSModule
649 {
650 public:
651     CvBlobTrackPredictor(){SetTypeName("BlobTrackPredictor");};
652     virtual CvBlob* Predict() = 0;
653     virtual void    Update(CvBlob* pBlob) = 0;
654     virtual void    Release() = 0;
655 };
656 CV_EXPORTS CvBlobTrackPredictor* cvCreateModuleBlobTrackPredictKalman();
657
658
659
660 /* Trajectory analyser module: */
661 class CV_EXPORTS CvBlobTrackAnalysis: public CvVSModule
662 {
663 public:
664     CvBlobTrackAnalysis(){SetTypeName("BlobTrackAnalysis");};
665     virtual void    AddBlob(CvBlob* pBlob) = 0;
666     virtual void    Process(IplImage* pImg, IplImage* pFG) = 0;
667     virtual float   GetState(int BlobID) = 0;
668     /* return 0 if trajectory is normal
669        return >0 if trajectory abnormal */
670     virtual const char*   GetStateDesc(int /*BlobID*/){return NULL;};
671     virtual void    SetFileName(char* /*DataBaseName*/){};
672     virtual void    Release() = 0;
673 };
674
675
676 inline void cvReleaseBlobTrackAnalysis(CvBlobTrackAnalysis** pBTPP)
677 {
678     if(pBTPP == NULL) return;
679     if(*pBTPP)(*pBTPP)->Release();
680     *pBTPP = 0;
681 }
682
683 /* Feature-vector generation module: */
684 class CV_EXPORTS CvBlobTrackFVGen : public CvVSModule
685 {
686 public:
687     CvBlobTrackFVGen(){SetTypeName("BlobTrackFVGen");};
688     virtual void    AddBlob(CvBlob* pBlob) = 0;
689     virtual void    Process(IplImage* pImg, IplImage* pFG) = 0;
690     virtual void    Release() = 0;
691     virtual int     GetFVSize() = 0;
692     virtual int     GetFVNum() = 0;
693     virtual float*  GetFV(int index, int* pFVID) = 0; /* Returns pointer to FV, if return 0 then FV not created */
694     virtual float*  GetFVVar(){return NULL;}; /* Returns pointer to array of variation of values of FV, if returns 0 then FVVar does not exist. */
695     virtual float*  GetFVMin() = 0; /* Returns pointer to array of minimal values of FV, if returns 0 then FVrange does not exist */
696     virtual float*  GetFVMax() = 0; /* Returns pointer to array of maximal values of FV, if returns 0 then FVrange does not exist */
697 };
698
699
700 /* Trajectory Analyser module: */
701 class CV_EXPORTS CvBlobTrackAnalysisOne
702 {
703 public:
704     virtual ~CvBlobTrackAnalysisOne() {};
705     virtual int     Process(CvBlob* pBlob, IplImage* pImg, IplImage* pFG) = 0;
706     /* return 0 if trajectory is normal
707        return >0 if trajectory abnormal */
708     virtual void    Release() = 0;
709 };
710
711 /* Create blob tracking post processing module based on simle module: */
712 CV_EXPORTS CvBlobTrackAnalysis* cvCreateBlobTrackAnalysisList(CvBlobTrackAnalysisOne* (*create)());
713
714 /* Declarations of constructors of implemented modules: */
715
716 /* Based on histogram analysis of 2D FV (x,y): */
717 CV_EXPORTS CvBlobTrackAnalysis* cvCreateModuleBlobTrackAnalysisHistP();
718
719 /* Based on histogram analysis of 4D FV (x,y,vx,vy): */
720 CV_EXPORTS CvBlobTrackAnalysis* cvCreateModuleBlobTrackAnalysisHistPV();
721
722 /* Based on histogram analysis of 5D FV (x,y,vx,vy,state): */
723 CV_EXPORTS CvBlobTrackAnalysis* cvCreateModuleBlobTrackAnalysisHistPVS();
724
725 /* Based on histogram analysis of 4D FV (startpos,stoppos): */
726 CV_EXPORTS CvBlobTrackAnalysis* cvCreateModuleBlobTrackAnalysisHistSS();
727
728
729
730 /* Based on SVM classifier analysis of 2D FV (x,y): */
731 //CV_EXPORTS CvBlobTrackAnalysis* cvCreateModuleBlobTrackAnalysisSVMP();
732
733 /* Based on SVM classifier analysis of 4D FV (x,y,vx,vy): */
734 //CV_EXPORTS CvBlobTrackAnalysis* cvCreateModuleBlobTrackAnalysisSVMPV();
735
736 /* Based on SVM classifier analysis of 5D FV (x,y,vx,vy,state): */
737 //CV_EXPORTS CvBlobTrackAnalysis* cvCreateModuleBlobTrackAnalysisSVMPVS();
738
739 /* Based on SVM classifier analysis of 4D FV (startpos,stoppos): */
740 //CV_EXPORTS CvBlobTrackAnalysis* cvCreateModuleBlobTrackAnalysisSVMSS();
741
742 /* Track analysis based on distance between tracks: */
743 CV_EXPORTS CvBlobTrackAnalysis* cvCreateModuleBlobTrackAnalysisTrackDist();
744
745 /* Analyzer based on reation Road and height map: */
746 //CV_EXPORTS CvBlobTrackAnalysis* cvCreateModuleBlobTrackAnalysis3DRoadMap();
747
748 /* Analyzer that makes OR decision using set of analyzers: */
749 CV_EXPORTS CvBlobTrackAnalysis* cvCreateModuleBlobTrackAnalysisIOR();
750
751 /* Estimator of human height: */
752 class CV_EXPORTS CvBlobTrackAnalysisHeight: public CvBlobTrackAnalysis
753 {
754 public:
755     virtual double  GetHeight(CvBlob* pB) = 0;
756 };
757 //CV_EXPORTS CvBlobTrackAnalysisHeight* cvCreateModuleBlobTrackAnalysisHeightScale();
758
759
760
761 /* AUTO BLOB TRACKER INTERFACE -- pipeline of 3 modules: */
762 class CV_EXPORTS CvBlobTrackerAuto: public CvVSModule
763 {
764 public:
765     CvBlobTrackerAuto(){SetTypeName("BlobTrackerAuto");};
766     virtual void        Process(IplImage* pImg, IplImage* pMask = NULL) = 0;
767     virtual CvBlob*     GetBlob(int index) = 0;
768     virtual CvBlob*     GetBlobByID(int ID) = 0;
769     virtual int         GetBlobNum() = 0;
770     virtual IplImage*   GetFGMask(){return NULL;};
771     virtual float       GetState(int BlobID) = 0;
772     virtual const char*       GetStateDesc(int BlobID) = 0;
773     /* return 0 if trajectory is normal;
774      * return >0 if trajectory abnormal. */
775     virtual void    Release() = 0;
776 };
777 inline void cvReleaseBlobTrackerAuto(CvBlobTrackerAuto** ppT)
778 {
779     ppT[0]->Release();
780     ppT[0] = 0;
781 }
782 /* END AUTO BLOB TRACKER INTERFACE */
783
784
785 /* Constructor functions and data for specific BlobTRackerAuto modules: */
786
787 /* Parameters of blobtracker auto ver1: */
788 struct CvBlobTrackerAutoParam1
789 {
790     int                     FGTrainFrames; /* Number of frames needed for FG (foreground) detector to train.        */
791
792     CvFGDetector*           pFG;           /* FGDetector module. If this field is NULL the Process FG mask is used. */
793
794     CvBlobDetector*         pBD;           /* Selected blob detector module.                                        */
795                                            /* If this field is NULL default blobdetector module will be created.    */
796
797     CvBlobTracker*          pBT;           /* Selected blob tracking module.                                        */
798                                            /* If this field is NULL default blobtracker module will be created.     */
799
800     CvBlobTrackGen*         pBTGen;        /* Selected blob trajectory generator.                                   */
801                                            /* If this field is NULL no generator is used.                           */
802
803     CvBlobTrackPostProc*    pBTPP;         /* Selected blob trajectory postprocessing module.                       */
804                                            /* If this field is NULL no postprocessing is done.                      */
805
806     int                     UsePPData;
807
808     CvBlobTrackAnalysis*    pBTA;          /* Selected blob trajectory analysis module.                             */
809                                            /* If this field is NULL no track analysis is done.                      */
810 };
811
812 /* Create blob tracker auto ver1: */
813 CV_EXPORTS CvBlobTrackerAuto* cvCreateBlobTrackerAuto1(CvBlobTrackerAutoParam1* param = NULL);
814
815 /* Simple loader for many auto trackers by its type : */
816 inline CvBlobTrackerAuto* cvCreateBlobTrackerAuto(int type, void* param)
817 {
818     if(type == 0) return cvCreateBlobTrackerAuto1((CvBlobTrackerAutoParam1*)param);
819     return 0;
820 }
821
822
823
824 struct CvTracksTimePos
825 {
826     int len1,len2;
827     int beg1,beg2;
828     int end1,end2;
829     int comLen; //common length for two tracks
830     int shift1,shift2;
831 };
832
833 /*CV_EXPORTS int cvCompareTracks( CvBlobTrackSeq *groundTruth,
834                    CvBlobTrackSeq *result,
835                    FILE *file);*/
836
837
838 /* Constructor functions:  */
839
840 CV_EXPORTS void cvCreateTracks_One(CvBlobTrackSeq *TS);
841 CV_EXPORTS void cvCreateTracks_Same(CvBlobTrackSeq *TS1, CvBlobTrackSeq *TS2);
842 CV_EXPORTS void cvCreateTracks_AreaErr(CvBlobTrackSeq *TS1, CvBlobTrackSeq *TS2, int addW, int addH);
843
844
845 /* HIST API */
846 class CV_EXPORTS CvProb
847 {
848 public:
849     virtual ~CvProb() {};
850
851     /* Calculate probability value: */
852     virtual double Value(int* /*comp*/, int /*x*/ = 0, int /*y*/ = 0){return -1;};
853
854     /* Update histograpp Pnew = (1-W)*Pold + W*Padd*/
855     /* W weight of new added prob */
856     /* comps - matrix of new fetature vectors used to update prob */
857     virtual void AddFeature(float W, int* comps, int x =0, int y = 0) = 0;
858     virtual void Scale(float factor = 0, int x = -1, int y = -1) = 0;
859     virtual void Release() = 0;
860 };
861 inline void cvReleaseProb(CvProb** ppProb){ppProb[0]->Release();ppProb[0]=NULL;}
862 /* HIST API */
863
864 /* Some Prob: */
865 CV_EXPORTS CvProb* cvCreateProbS(int dim, CvSize size, int sample_num);
866 CV_EXPORTS CvProb* cvCreateProbMG(int dim, CvSize size, int sample_num);
867 CV_EXPORTS CvProb* cvCreateProbMG2(int dim, CvSize size, int sample_num);
868 CV_EXPORTS CvProb* cvCreateProbHist(int dim, CvSize size);
869
870 #define CV_BT_HIST_TYPE_S     0
871 #define CV_BT_HIST_TYPE_MG    1
872 #define CV_BT_HIST_TYPE_MG2   2
873 #define CV_BT_HIST_TYPE_H     3
874 inline CvProb* cvCreateProb(int type, int dim, CvSize size = cvSize(1,1), void* /*param*/ = NULL)
875 {
876     if(type == CV_BT_HIST_TYPE_S) return cvCreateProbS(dim, size, -1);
877     if(type == CV_BT_HIST_TYPE_MG) return cvCreateProbMG(dim, size, -1);
878     if(type == CV_BT_HIST_TYPE_MG2) return cvCreateProbMG2(dim, size, -1);
879     if(type == CV_BT_HIST_TYPE_H) return cvCreateProbHist(dim, size);
880     return NULL;
881 }
882
883
884
885 /* Noise type definitions: */
886 #define CV_NOISE_NONE               0
887 #define CV_NOISE_GAUSSIAN           1
888 #define CV_NOISE_UNIFORM            2
889 #define CV_NOISE_SPECKLE            3
890 #define CV_NOISE_SALT_AND_PEPPER    4
891
892 /* Add some noise to image: */
893 /* pImg - (input) image without noise */
894 /* pImg - (output) image with noise */
895 /* noise_type - type of added noise */
896 /*  CV_NOISE_GAUSSIAN - pImg += n , n - is gaussian noise with Ampl standart deviation */
897 /*  CV_NOISE_UNIFORM - pImg += n , n - is uniform noise with Ampl standart deviation */
898 /*  CV_NOISE_SPECKLE - pImg += n*pImg , n - is gaussian noise with Ampl standart deviation */
899 /*  CV_NOISE_SALT_AND_PAPPER - pImg = pImg with blacked and whited pixels,
900             Ampl is density of brocken pixels (0-there are not broken pixels, 1 - all pixels are broken)*/
901 /* Ampl - "amplitude" of noise */
902 //CV_EXPORTS void cvAddNoise(IplImage* pImg, int noise_type, double Ampl, CvRNG* rnd_state = NULL);
903
904 /*================== GENERATOR OF TEST VIDEO SEQUENCE ===================== */
905 typedef void CvTestSeq;
906
907 /* pConfigfile - Name of file (yml or xml) with description of test sequence */
908 /* videos - array of names of test videos described in "pConfigfile" file */
909 /* numvideos - size of "videos" array */
910 CV_EXPORTS CvTestSeq* cvCreateTestSeq(char* pConfigfile, char** videos, int numvideo, float Scale = 1, int noise_type = CV_NOISE_NONE, double noise_ampl = 0);
911 CV_EXPORTS void cvReleaseTestSeq(CvTestSeq** ppTestSeq);
912
913 /* Generate next frame from test video seq and return pointer to it: */
914 CV_EXPORTS IplImage* cvTestSeqQueryFrame(CvTestSeq* pTestSeq);
915
916 /* Return pointer to current foreground mask: */
917 CV_EXPORTS IplImage* cvTestSeqGetFGMask(CvTestSeq* pTestSeq);
918
919 /* Return pointer to current image: */
920 CV_EXPORTS IplImage* cvTestSeqGetImage(CvTestSeq* pTestSeq);
921
922 /* Return frame size of result test video: */
923 CV_EXPORTS CvSize cvTestSeqGetImageSize(CvTestSeq* pTestSeq);
924
925 /* Return number of frames result test video: */
926 CV_EXPORTS int cvTestSeqFrameNum(CvTestSeq* pTestSeq);
927
928 /* Return number of existing objects.
929  * This is general number of any objects.
930  * For example number of trajectories may be equal or less than returned value:
931  */
932 CV_EXPORTS int cvTestSeqGetObjectNum(CvTestSeq* pTestSeq);
933
934 /* Return 0 if there is not position for current defined on current frame */
935 /* Return 1 if there is object position and pPos was filled */
936 CV_EXPORTS int cvTestSeqGetObjectPos(CvTestSeq* pTestSeq, int ObjIndex, CvPoint2D32f* pPos);
937 CV_EXPORTS int cvTestSeqGetObjectSize(CvTestSeq* pTestSeq, int ObjIndex, CvPoint2D32f* pSize);
938
939 /* Add noise to final image: */
940 CV_EXPORTS void cvTestSeqAddNoise(CvTestSeq* pTestSeq, int noise_type = CV_NOISE_NONE, double noise_ampl = 0);
941
942 /* Add Intensity variation: */
943 CV_EXPORTS void cvTestSeqAddIntensityVariation(CvTestSeq* pTestSeq, float DI_per_frame, float MinI, float MaxI);
944 CV_EXPORTS void cvTestSeqSetFrame(CvTestSeq* pTestSeq, int n);
945
946 #endif
947
948 /* End of file. */