Update the changelog
[opencv] / cvaux / src / 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
101     ~CvBlobTrackGen1()
102     {
103         int i;
104         for(i=m_TrackList.GetBlobNum();i>0;--i)
105         {
106             DefBlobTrack* pTrack = (DefBlobTrack*)m_TrackList.GetBlob(i-1);
107             if(!pTrack->Saved)
108             {   /* Save track: */
109                 SaveTrack(pTrack, m_pFileName, m_BlobSizeNorm);
110             }   /* Save track. */
111
112             /* Delete sequence: */
113             delete pTrack->pSeq;
114
115             pTrack->pSeq = NULL;
116
117         }   /* Check next track. */
118     }   /*  Destructor. */
119
120     void    SetFileName(char* pFileName){m_pFileName = pFileName;};
121
122     void    AddBlob(CvBlob* pBlob)
123     {
124         DefBlobTrack* pTrack = (DefBlobTrack*)m_TrackList.GetBlobByID(CV_BLOB_ID(pBlob));
125
126         if(pTrack==NULL)
127         {   /* Add new track: */
128             DefBlobTrack    Track;
129             Track.blob = pBlob[0];
130             Track.FrameBegin = m_Frame;
131             Track.pSeq = new CvBlobSeq;
132             Track.Saved = 0;
133             m_TrackList.AddBlob((CvBlob*)&Track);
134             pTrack = (DefBlobTrack*)m_TrackList.GetBlobByID(CV_BLOB_ID(pBlob));
135         }   /* Add new track. */
136
137         assert(pTrack);
138         pTrack->FrameLast = m_Frame;
139         assert(pTrack->pSeq);
140         pTrack->pSeq->AddBlob(pBlob);
141     };
142
143     void    Process(IplImage* /*pImg*/ = NULL, IplImage* /*pFG*/ = NULL)
144     {
145         int i;
146
147         for(i=m_TrackList.GetBlobNum(); i>0; --i)
148         {
149             DefBlobTrack* pTrack = (DefBlobTrack*)m_TrackList.GetBlob(i-1);
150
151             if(pTrack->FrameLast < m_Frame && !pTrack->Saved)
152             {   /* Save track: */
153                 SaveTrack(pTrack, m_pFileName, m_BlobSizeNorm);
154                 if(pTrack->Saved)
155                 {   /* delete sequence */
156                     delete pTrack->pSeq;
157                     pTrack->pSeq = NULL;
158                     m_TrackList.DelBlob(i-1);
159                 }
160             }   /* Save track. */
161         }   /*  Check next track. */
162         m_Frame++;
163     }
164
165     void Release()
166     {
167         delete this;
168     }
169
170 protected:
171     int         m_Frame;
172     char*       m_pFileName;
173     CvBlobSeq   m_TrackList;
174     int         m_BlobSizeNorm;
175 };  /* class CvBlobTrackGen1 */
176
177
178 CvBlobTrackGen* cvCreateModuleBlobTrackGen1()
179 {
180     return (CvBlobTrackGen*) new CvBlobTrackGen1(0);
181 }
182
183