Update to 2.0.0 tree from current Fremantle build
[opencv] / src / cvaux / vs / blobtrackgen1.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 typedef struct DefBlobTrack
45 {
46     CvBlob      blob;
47     CvBlobSeq*  pSeq;
48     int         FrameBegin;
49     int         FrameLast;
50     int         Saved; /* flag */
51 } DefBlobTrack;
52
53
54 static void SaveTrack(DefBlobTrack* pTrack, char* pFileName, int norm = 0)
55 {   /* Save blob track: */
56     int         j;
57     FILE*       out = NULL;
58     CvBlobSeq*  pS = pTrack->pSeq;
59     CvBlob*     pB0 = pS?pS->GetBlob(0):NULL;
60
61     if(pFileName == NULL) return;
62     if(pTrack == NULL) return;
63
64     out = fopen(pFileName,"at");
65     if(out == NULL)
66     {
67         printf("Warning! Cannot open %s file for track output\n", pFileName);
68         return;
69     }
70
71     fprintf(out,"%d",pTrack->FrameBegin);
72
73     if(pS) for(j=0; j<pS->GetBlobNum(); ++j)
74     {
75         CvBlob* pB = pS->GetBlob(j);
76
77         fprintf(out,", %.1f, %.1f", CV_BLOB_X(pB),CV_BLOB_Y(pB));
78
79         if(CV_BLOB_WX(pB0)>0)
80             fprintf(out,", %.2f",CV_BLOB_WX(pB)/(norm?CV_BLOB_WX(pB0):1));
81
82         if(CV_BLOB_WY(pB0)>0)
83             fprintf(out,", %.2f",CV_BLOB_WY(pB)/(norm?CV_BLOB_WY(pB0):1));
84     }
85     fprintf(out,"\n");
86     fclose(out);
87     pTrack->Saved = 1;
88 }   /* Save blob track. */
89
90 class CvBlobTrackGen1:public CvBlobTrackGen
91 {
92
93 public:
94     CvBlobTrackGen1(int BlobSizeNorm = 0):m_TrackList(sizeof(DefBlobTrack))
95     {
96         m_BlobSizeNorm = BlobSizeNorm;
97         m_Frame = 0;
98         m_pFileName = NULL;
99
100         SetModuleName("Gen1");
101     };
102
103     ~CvBlobTrackGen1()
104     {
105         int i;
106         for(i=m_TrackList.GetBlobNum();i>0;--i)
107         {
108             DefBlobTrack* pTrack = (DefBlobTrack*)m_TrackList.GetBlob(i-1);
109             if(!pTrack->Saved)
110             {   /* Save track: */
111                 SaveTrack(pTrack, m_pFileName, m_BlobSizeNorm);
112             }   /* Save track. */
113
114             /* Delete sequence: */
115             delete pTrack->pSeq;
116
117             pTrack->pSeq = NULL;
118
119         }   /* Check next track. */
120     }   /*  Destructor. */
121
122     void    SetFileName(char* pFileName){m_pFileName = pFileName;};
123
124     void    AddBlob(CvBlob* pBlob)
125     {
126         DefBlobTrack* pTrack = (DefBlobTrack*)m_TrackList.GetBlobByID(CV_BLOB_ID(pBlob));
127
128         if(pTrack==NULL)
129         {   /* Add new track: */
130             DefBlobTrack    Track;
131             Track.blob = pBlob[0];
132             Track.FrameBegin = m_Frame;
133             Track.pSeq = new CvBlobSeq;
134             Track.Saved = 0;
135             m_TrackList.AddBlob((CvBlob*)&Track);
136             pTrack = (DefBlobTrack*)m_TrackList.GetBlobByID(CV_BLOB_ID(pBlob));
137         }   /* Add new track. */
138
139         assert(pTrack);
140         pTrack->FrameLast = m_Frame;
141         assert(pTrack->pSeq);
142         pTrack->pSeq->AddBlob(pBlob);
143     };
144
145     void    Process(IplImage* /*pImg*/ = NULL, IplImage* /*pFG*/ = NULL)
146     {
147         int i;
148
149         for(i=m_TrackList.GetBlobNum(); i>0; --i)
150         {
151             DefBlobTrack* pTrack = (DefBlobTrack*)m_TrackList.GetBlob(i-1);
152
153             if(pTrack->FrameLast < m_Frame && !pTrack->Saved)
154             {   /* Save track: */
155                 SaveTrack(pTrack, m_pFileName, m_BlobSizeNorm);
156                 if(pTrack->Saved)
157                 {   /* delete sequence */
158                     delete pTrack->pSeq;
159                     pTrack->pSeq = NULL;
160                     m_TrackList.DelBlob(i-1);
161                 }
162             }   /* Save track. */
163         }   /*  Check next track. */
164         m_Frame++;
165     }
166
167     void Release()
168     {
169         delete this;
170     }
171
172 protected:
173     int         m_Frame;
174     char*       m_pFileName;
175     CvBlobSeq   m_TrackList;
176     int         m_BlobSizeNorm;
177 };  /* class CvBlobTrackGen1 */
178
179
180 CvBlobTrackGen* cvCreateModuleBlobTrackGen1()
181 {
182     return (CvBlobTrackGen*) new CvBlobTrackGen1(0);
183 }
184
185