Move the sources to trunk
[opencv] / cvaux / src / vs / blobtrackanalysis.cpp
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 #include "_cvaux.h"
43
44 /*======================= FILTER LIST SHELL =====================*/
45 typedef struct DefTrackAnalyser
46 {
47     CvBlob                  blob;
48     CvBlobTrackAnalysisOne* pFilter;
49     int                     m_LastFrame;
50     int                     state;
51 } DefTrackAnalyser;
52
53 class CvBlobTrackAnalysisList : public CvBlobTrackAnalysis
54 {
55 protected:
56     CvBlobTrackAnalysisOne* (*m_CreateAnalysis)();
57     CvBlobSeq               m_TrackAnalyserList;
58     int                     m_Frame;
59 public:
60     CvBlobTrackAnalysisList(CvBlobTrackAnalysisOne* (*create)()):m_TrackAnalyserList(sizeof(DefTrackAnalyser))
61     {
62         m_Frame = 0;
63         m_CreateAnalysis = create;
64     }
65     ~CvBlobTrackAnalysisList()
66     {
67         int i;
68         for(i=m_TrackAnalyserList.GetBlobNum();i>0;--i)
69         {
70             DefTrackAnalyser* pF = (DefTrackAnalyser*)m_TrackAnalyserList.GetBlob(i-1);
71             pF->pFilter->Release();
72         }
73     };
74     virtual void    AddBlob(CvBlob* pBlob)
75     {
76         DefTrackAnalyser* pF = (DefTrackAnalyser*)m_TrackAnalyserList.GetBlobByID(CV_BLOB_ID(pBlob));
77         if(pF == NULL)
78         { /* create new filter */
79             DefTrackAnalyser F;
80             F.state = 0;
81             F.blob = pBlob[0];
82             F.m_LastFrame = m_Frame;
83             F.pFilter = m_CreateAnalysis();
84             m_TrackAnalyserList.AddBlob((CvBlob*)&F);
85             pF = (DefTrackAnalyser*)m_TrackAnalyserList.GetBlobByID(CV_BLOB_ID(pBlob));
86         }
87
88         assert(pF);
89         pF->blob = pBlob[0];
90         pF->m_LastFrame = m_Frame;
91     };
92     virtual void    Process(IplImage* pImg, IplImage* pFG)
93     {
94         int i;
95         for(i=m_TrackAnalyserList.GetBlobNum();i>0;--i)
96         {
97             DefTrackAnalyser* pF = (DefTrackAnalyser*)m_TrackAnalyserList.GetBlob(i-1);
98             if(pF->m_LastFrame == m_Frame)
99             {/* process */
100                 int ID = CV_BLOB_ID(pF);
101                 pF->state = pF->pFilter->Process(&(pF->blob), pImg, pFG);
102                 CV_BLOB_ID(pF) = ID;
103             }
104             else
105             {/* delete blob filter */
106                 pF->pFilter->Release();
107                 m_TrackAnalyserList.DelBlob(i-1);
108             }
109         }/* next blob */
110         m_Frame++;
111     };
112     float GetState(int BlobID)
113     {
114         DefTrackAnalyser* pF = (DefTrackAnalyser*)m_TrackAnalyserList.GetBlobByID(BlobID);
115         return pF?pF->state:0.f;
116     };
117     void    Release(){delete this;};
118
119 }; /* CvBlobTrackAnalysisList */
120
121 CvBlobTrackAnalysis* cvCreateBlobTrackAnalysisList(CvBlobTrackAnalysisOne* (*create)())
122 {
123     return (CvBlobTrackAnalysis*) new CvBlobTrackAnalysisList(create);
124 }
125
126 /* ======================== Analyser modules ============================= */
127