Move the sources to trunk
[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         if(storage==NULL)
68         {
69             printf("WARNING!!! Can not open %s file for trajectories writing",m_pFileName);
70         }
71
72         for(i=0;i<1024 && m_pFileName[i]!='.' && m_pFileName[i]!=0;++i)video_name[i]=m_pFileName[i];
73         video_name[i] = 0;
74         for(;i>0;i--)
75         {
76             if(video_name[i-1] == '\\') break;
77             if(video_name[i-1] == '/') break;
78             if(video_name[i-1] == ':') break;
79         }
80         struct_name = video_name + i;
81
82         cvStartWriteStruct(storage, struct_name, CV_NODE_SEQ);
83         for(i=0;i<ObjNum;++i)
84         {
85             char            obj_name[1024];
86             DefBlobTrack*   pTrack = (DefBlobTrack*)m_TrackList.GetBlob(i);
87             if(pTrack==NULL) continue;
88             sprintf(obj_name,"%s_obj%d",struct_name,i);
89             cvStartWriteStruct(storage, NULL, CV_NODE_MAP);
90             cvWriteInt(storage, "FrameBegin", pTrack->FrameBegin);
91             cvWriteString(storage, "VideoObj", obj_name);
92             cvEndWriteStruct( storage );
93             pTrack->Saved = 1;
94         }
95         cvEndWriteStruct( storage );
96
97         for(i=0;i<ObjNum;++i)
98         {
99             char            obj_name[1024];
100             DefBlobTrack*   pTrack = (DefBlobTrack*)m_TrackList.GetBlob(i);
101             CvBlobSeq*      pSeq = pTrack->pSeq;
102             sprintf(obj_name,"%s_obj%d",struct_name,i);
103             cvStartWriteStruct(storage, obj_name, CV_NODE_MAP);
104             
105             { /* write pos */
106                 int             j;
107                 CvPoint2D32f    p;
108                 cvStartWriteStruct(storage, "Pos", CV_NODE_SEQ|CV_NODE_FLOW);
109                 for(j=0;j<pSeq->GetBlobNum();++j)
110                 {
111                     CvBlob* pB = pSeq->GetBlob(j);
112                     p.x = pB->x/(m_Size.width-1);
113                     p.y = pB->y/(m_Size.height-1);
114                     cvWriteRawData(storage, &p, 1 ,"ff");
115                 }
116                 cvEndWriteStruct( storage );
117             }
118             
119             { /* write size */
120                 int             j;
121                 CvPoint2D32f    p;
122                 cvStartWriteStruct(storage, "Size", CV_NODE_SEQ|CV_NODE_FLOW);
123                 for(j=0;j<pSeq->GetBlobNum();++j)
124                 {
125                     CvBlob* pB = pSeq->GetBlob(j);
126                     p.x = pB->w/(m_Size.width-1);
127                     p.y = pB->h/(m_Size.height-1);
128                     cvWriteRawData(storage, &p, 1 ,"ff");
129                 }
130                 cvEndWriteStruct( storage );
131             }
132             cvEndWriteStruct( storage );
133         }
134         cvReleaseFileStorage(&storage);
135     }/* Save All */
136 public:
137     CvBlobTrackGenYML():m_TrackList(sizeof(DefBlobTrack))
138     {
139         m_Frame = 0;
140         m_pFileName = NULL;
141         m_Size = cvSize(2,2);
142     };
143     ~CvBlobTrackGenYML()
144     {
145         int i;
146         SaveAll();
147         for(i=m_TrackList.GetBlobNum();i>0;--i)
148         {
149             DefBlobTrack* pTrack = (DefBlobTrack*)m_TrackList.GetBlob(i-1);
150             /* delete sequence */
151             delete pTrack->pSeq;
152             pTrack->pSeq = NULL;
153         }/* check next track */
154     }/* destructor */
155
156     void    SetFileName(char* pFileName){m_pFileName = pFileName;};
157     void    AddBlob(CvBlob* pBlob)
158     {
159         DefBlobTrack* pTrack = (DefBlobTrack*)m_TrackList.GetBlobByID(CV_BLOB_ID(pBlob));
160         if(pTrack==NULL)
161         {/* add new track */
162             DefBlobTrack    Track;
163             Track.blob = pBlob[0];
164             Track.FrameBegin = m_Frame;
165             Track.pSeq = new CvBlobSeq;
166             Track.Saved = 0;
167             m_TrackList.AddBlob((CvBlob*)&Track);
168             pTrack = (DefBlobTrack*)m_TrackList.GetBlobByID(CV_BLOB_ID(pBlob));
169         }/* add new track */
170
171         assert(pTrack);
172         pTrack->FrameLast = m_Frame;
173         assert(pTrack->pSeq);
174         pTrack->pSeq->AddBlob(pBlob);
175     };
176     void    Process(IplImage* pImg = NULL, IplImage* /*pFG*/ = NULL)
177     {
178         int i;
179         m_Size = cvSize(pImg->width,pImg->height);
180         for(i=m_TrackList.GetBlobNum();i>0;--i)
181         {
182             DefBlobTrack* pTrack = (DefBlobTrack*)m_TrackList.GetBlob(i-1);
183             if(pTrack->FrameLast < m_Frame && !pTrack->Saved)
184             {/* save track */
185                 SaveAll();
186             }/* save track */
187         }/* check next track */
188         m_Frame++;
189     }
190     void Release()
191     {
192         delete this;
193     }
194 }; /* class CvBlobTrackGenYML */
195
196
197 CvBlobTrackGen* cvCreateModuleBlobTrackGenYML()
198 {
199     return (CvBlobTrackGen*) new CvBlobTrackGenYML;
200 }
201
202