Update to 2.0.0 tree from current Fremantle build
[opencv] / src / cvaux / vs / blobtrackpostproclinear.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 /*======================= TIME AVERAGING FILTER =========================*/
44 #define TIME_WND 5
45 class CvBlobTrackPostProcTimeAver:public CvBlobTrackPostProcOne
46 {
47
48 protected:
49     CvBlob      m_Blob;
50     CvBlob      m_pBlobs[TIME_WND];
51     float       m_Weights[TIME_WND];
52     int         m_Frame;
53
54 public:
55     CvBlobTrackPostProcTimeAver( int KernelType = 0)
56     {
57         int i;
58         m_Frame = 0;
59         for(i=0;i<TIME_WND;++i)
60         {
61             m_Weights[i] = 1;
62             if(KernelType == 1)
63             {
64                 m_Weights[i] = (float)exp((-2.3*i)/(TIME_WND-1)); /* last weight is 0.1 of first weight */
65             }
66         }
67
68         SetModuleName("TimeAver");
69     };
70
71    ~CvBlobTrackPostProcTimeAver(){};
72
73     CvBlob* Process(CvBlob* pBlob)
74     {
75         float   WSum = 0;
76         int     i;
77         int index = m_Frame % TIME_WND;
78         int size = MIN((m_Frame+1), TIME_WND);
79         m_pBlobs[index] = pBlob[0];
80         m_Blob.x = m_Blob.y = m_Blob.w = m_Blob.h = 0;
81
82         for(i=0; i<size; ++i)
83         {
84             float   W = m_Weights[i];
85             int     index  = (m_Frame - i + TIME_WND) % TIME_WND;
86             m_Blob.x += W*m_pBlobs[index].x;
87             m_Blob.y += W*m_pBlobs[index].y;
88             m_Blob.w += W*m_pBlobs[index].w;
89             m_Blob.h += W*m_pBlobs[index].h;
90             WSum += W;
91         }
92         assert(WSum>0);
93
94         m_Blob.x /= WSum;
95         m_Blob.y /= WSum;
96         m_Blob.w /= WSum;
97         m_Blob.h /= WSum;
98
99         m_Frame++;
100         return &m_Blob;
101     };
102
103     void Release()
104     {
105         delete this;
106     }
107 };  /* class CvBlobTrackPostProcTimeAver */
108
109 CvBlobTrackPostProcOne* cvCreateModuleBlobTrackPostProcTimeAverRectOne()
110 {
111     return (CvBlobTrackPostProcOne*) new CvBlobTrackPostProcTimeAver(0);
112 }
113
114 CvBlobTrackPostProcOne* cvCreateModuleBlobTrackPostProcTimeAverExpOne()
115 {
116     return (CvBlobTrackPostProcOne*) new CvBlobTrackPostProcTimeAver(1);
117 }
118
119 CvBlobTrackPostProc* cvCreateModuleBlobTrackPostProcTimeAverRect()
120 {
121     return cvCreateBlobTrackPostProcList(cvCreateModuleBlobTrackPostProcTimeAverRectOne);
122 }
123
124 CvBlobTrackPostProc* cvCreateModuleBlobTrackPostProcTimeAverExp()
125 {
126     return cvCreateBlobTrackPostProcList(cvCreateModuleBlobTrackPostProcTimeAverExpOne);
127 }
128 /*======================= KALMAN FILTER =========================*/