Move the sources to trunk
[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! Can not 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         if(CV_BLOB_WX(pB0)>0)
79             fprintf(out,", %.2f",CV_BLOB_WX(pB)/(norm?CV_BLOB_WX(pB0):1));
80         if(CV_BLOB_WY(pB0)>0)
81             fprintf(out,", %.2f",CV_BLOB_WY(pB)/(norm?CV_BLOB_WY(pB0):1));
82     }
83     fprintf(out,"\n");
84     fclose(out);
85     pTrack->Saved = 1;
86 }/* save blob track */
87
88 class CvBlobTrackGen1:public CvBlobTrackGen
89 {
90
91 public:
92     CvBlobTrackGen1(int BlobSizeNorm = 0):m_TrackList(sizeof(DefBlobTrack))
93     {
94         m_BlobSizeNorm = BlobSizeNorm;
95         m_Frame = 0;
96         m_pFileName = NULL;
97     };
98     ~CvBlobTrackGen1()
99     {
100         int i;
101         for(i=m_TrackList.GetBlobNum();i>0;--i)
102         {
103             DefBlobTrack* pTrack = (DefBlobTrack*)m_TrackList.GetBlob(i-1);
104             if(!pTrack->Saved)
105             {/* save track */
106                 SaveTrack(pTrack, m_pFileName, m_BlobSizeNorm);
107             }/* save track */
108
109             /* delete sequence */
110             delete pTrack->pSeq;
111             pTrack->pSeq = NULL;
112         }/* check next track */
113     }/* destructor */
114
115     void    SetFileName(char* pFileName){m_pFileName = pFileName;};
116     void    AddBlob(CvBlob* pBlob)
117     {
118         DefBlobTrack* pTrack = (DefBlobTrack*)m_TrackList.GetBlobByID(CV_BLOB_ID(pBlob));
119         if(pTrack==NULL)
120         {/* add new track */
121             DefBlobTrack    Track;
122             Track.blob = pBlob[0];
123             Track.FrameBegin = m_Frame;
124             Track.pSeq = new CvBlobSeq;
125             Track.Saved = 0;
126             m_TrackList.AddBlob((CvBlob*)&Track);
127             pTrack = (DefBlobTrack*)m_TrackList.GetBlobByID(CV_BLOB_ID(pBlob));
128         }/* add new track */
129
130         assert(pTrack);
131         pTrack->FrameLast = m_Frame;
132         assert(pTrack->pSeq);
133         pTrack->pSeq->AddBlob(pBlob);
134     };
135     void    Process(IplImage* /*pImg*/ = NULL, IplImage* /*pFG*/ = NULL)
136     {
137         int i;
138         for(i=m_TrackList.GetBlobNum();i>0;--i)
139         {
140             DefBlobTrack* pTrack = (DefBlobTrack*)m_TrackList.GetBlob(i-1);
141             if(pTrack->FrameLast < m_Frame && !pTrack->Saved)
142             {/* save track */
143                 SaveTrack(pTrack, m_pFileName, m_BlobSizeNorm);
144                 if(pTrack->Saved)
145                 {   /* delete sequence */
146                     delete pTrack->pSeq;
147                     pTrack->pSeq = NULL;
148                     m_TrackList.DelBlob(i-1);
149                 }
150             }/* save track */
151         }/* check next track */
152         m_Frame++;
153     }
154     void Release()
155     {
156         delete this;
157     }
158 protected:
159     int         m_Frame;
160     char*       m_pFileName;
161     CvBlobSeq   m_TrackList;
162     int         m_BlobSizeNorm;
163 }; /* class CvBlobTrackGen1 */
164
165
166 CvBlobTrackGen* cvCreateModuleBlobTrackGen1()
167 {
168     return (CvBlobTrackGen*) new CvBlobTrackGen1(0);
169 }
170
171