Update the changelog
[opencv] / cvaux / src / vs / blobtrackgenyml.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 //
12 // Copyright (C) 2000, Intel Corporation, all rights reserved.
13 // Third party copyrights are property of their respective owners.
14 //
15 // Redistribution and use in source and binary forms, with or without modification,
16 // are permitted provided that the following conditions are met:
17 //
18 //   * Redistribution's of source code must retain the above copyright notice,
19 //     this list of conditions and the following disclaimer.
20 //
21 //   * Redistribution's in binary form must reproduce the above copyright notice,
22 //     this list of conditions and the following disclaimer in the documentation
23 //     and/or other materials provided with the distribution.
24 //
25 //   * The name of Intel Corporation may not be used to endorse or promote products
26 //     derived from this software without specific prior written permission.
27 //
28 // This software is provided by the copyright holders and contributors "as is" and
29 // any express or implied warranties, including, but not limited to, the implied
30 // warranties of merchantability and fitness for a particular purpose are disclaimed.
31 // In no event shall the Intel Corporation or contributors be liable for any direct,
32 // indirect, incidental, special, exemplary, or consequential damages
33 // (including, but not limited to, procurement of substitute goods or services;
34 // loss of use, data, or profits; or business interruption) however caused
35 // and on any theory of liability, whether in contract, strict liability,
36 // or tort (including negligence or otherwise) arising in any way out of
37 // the use of this software, even if advised of the possibility of such damage.
38 //
39 //M*/
40
41 #include "_cvaux.h"
42
43 typedef struct DefBlobTrack
44 {
45     CvBlob      blob;
46     CvBlobSeq*  pSeq;
47     int         FrameBegin;
48     int         FrameLast;
49     int         Saved; /* flag */
50 } DefBlobTrack;
51
52 class CvBlobTrackGenYML:public CvBlobTrackGen
53 {
54 protected:
55     int         m_Frame;
56     char*       m_pFileName;
57     CvBlobSeq   m_TrackList;
58     CvSize      m_Size;
59     
60     void SaveAll()
61     {
62         int     ObjNum = m_TrackList.GetBlobNum();
63         int     i;
64         char    video_name[1024];
65         char*   struct_name = NULL;
66         CvFileStorage* storage = cvOpenFileStorage(m_pFileName,NULL,CV_STORAGE_WRITE_TEXT);
67
68         if(storage==NULL)
69         {
70             printf("WARNING!!! Cannot open %s file for trajectory output.", m_pFileName);
71         }
72
73         for(i=0; i<1024 && m_pFileName[i]!='.' && m_pFileName[i]!=0; ++i) video_name[i]=m_pFileName[i];
74         video_name[i] = 0;
75
76         for(;i>0; i--)
77         {
78             if(video_name[i-1] == '\\') break;
79             if(video_name[i-1] == '/') break;
80             if(video_name[i-1] == ':') break;
81         }
82         struct_name = video_name + i;
83
84         cvStartWriteStruct(storage, struct_name, CV_NODE_SEQ);
85
86         for(i=0; i<ObjNum; ++i)
87         {
88             char            obj_name[1024];
89             DefBlobTrack*   pTrack = (DefBlobTrack*)m_TrackList.GetBlob(i);
90             if(pTrack==NULL) continue;
91             sprintf(obj_name,"%s_obj%d",struct_name,i);
92             cvStartWriteStruct(storage, NULL, CV_NODE_MAP);
93             cvWriteInt(storage, "FrameBegin", pTrack->FrameBegin);
94             cvWriteString(storage, "VideoObj", obj_name);
95             cvEndWriteStruct( storage );
96             pTrack->Saved = 1;
97         }
98         cvEndWriteStruct( storage );
99
100         for(i=0;i<ObjNum;++i)
101         {
102             char            obj_name[1024];
103             DefBlobTrack*   pTrack = (DefBlobTrack*)m_TrackList.GetBlob(i);
104             CvBlobSeq*      pSeq = pTrack->pSeq;
105             sprintf(obj_name,"%s_obj%d",struct_name,i);
106             cvStartWriteStruct(storage, obj_name, CV_NODE_MAP);
107             
108             {   /* Write position: */
109                 int             j;
110                 CvPoint2D32f    p;
111                 cvStartWriteStruct(storage, "Pos", CV_NODE_SEQ|CV_NODE_FLOW);
112                 for(j=0;j<pSeq->GetBlobNum();++j)
113                 {
114                     CvBlob* pB = pSeq->GetBlob(j);
115                     p.x = pB->x/(m_Size.width-1);
116                     p.y = pB->y/(m_Size.height-1);
117                     cvWriteRawData(storage, &p, 1 ,"ff");
118                 }
119                 cvEndWriteStruct( storage );
120             }
121             
122             {   /* Write size: */
123                 int             j;
124                 CvPoint2D32f    p;
125                 cvStartWriteStruct(storage, "Size", CV_NODE_SEQ|CV_NODE_FLOW);
126
127                 for(j=0; j<pSeq->GetBlobNum(); ++j)
128                 {
129                     CvBlob* pB = pSeq->GetBlob(j);
130                     p.x = pB->w/(m_Size.width-1);
131                     p.y = pB->h/(m_Size.height-1);
132                     cvWriteRawData(storage, &p, 1 ,"ff");
133                 }
134                 cvEndWriteStruct( storage );
135             }
136             cvEndWriteStruct( storage );
137         }
138         cvReleaseFileStorage(&storage);
139
140     }   /* Save All */
141
142 public:
143     CvBlobTrackGenYML():m_TrackList(sizeof(DefBlobTrack))
144     {
145         m_Frame = 0;
146         m_pFileName = NULL;
147         m_Size = cvSize(2,2);
148     };
149
150     ~CvBlobTrackGenYML()
151     {
152         int i;
153         SaveAll();
154
155         for(i=m_TrackList.GetBlobNum(); i>0; --i)
156         {
157             DefBlobTrack* pTrack = (DefBlobTrack*)m_TrackList.GetBlob(i-1);
158             /* Delete sequence: */
159             delete pTrack->pSeq;
160             pTrack->pSeq = NULL;
161         }   /* Check next track. */
162
163     }   /* Destructor. */
164
165     void    SetFileName(char* pFileName){m_pFileName = pFileName;};
166     void    AddBlob(CvBlob* pBlob)
167     {
168         DefBlobTrack* pTrack = (DefBlobTrack*)m_TrackList.GetBlobByID(CV_BLOB_ID(pBlob));
169
170         if(pTrack==NULL)
171         {   /* Add new track: */
172             DefBlobTrack    Track;
173             Track.blob = pBlob[0];
174             Track.FrameBegin = m_Frame;
175             Track.pSeq = new CvBlobSeq;
176             Track.Saved = 0;
177             m_TrackList.AddBlob((CvBlob*)&Track);
178             pTrack = (DefBlobTrack*)m_TrackList.GetBlobByID(CV_BLOB_ID(pBlob));
179         }   /* Add new track. */
180
181         assert(pTrack);
182         pTrack->FrameLast = m_Frame;
183         assert(pTrack->pSeq);
184         pTrack->pSeq->AddBlob(pBlob);
185     };
186     void    Process(IplImage* pImg = NULL, IplImage* /*pFG*/ = NULL)
187     {
188         int i;
189         m_Size = cvSize(pImg->width,pImg->height);
190
191         for(i=m_TrackList.GetBlobNum(); i>0; --i)
192         {
193             DefBlobTrack* pTrack = (DefBlobTrack*)m_TrackList.GetBlob(i-1);
194
195             if(pTrack->FrameLast < m_Frame && !pTrack->Saved)
196             {   /* Save track: */
197                 SaveAll();
198             }   /* Save track. */
199
200         }   /* Check next track. */
201
202         m_Frame++;
203     }
204
205     void Release()
206     {
207         delete this;
208     }
209 };  /* class CvBlobTrackGenYML */
210
211
212 CvBlobTrackGen* cvCreateModuleBlobTrackGenYML()
213 {
214     return (CvBlobTrackGen*) new CvBlobTrackGenYML;
215 }
216
217