Update the trunk to the OpenCV's CVS (2008-07-14)
[opencv] / cvaux / src / vs / blobtrackingkalman.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 /*======================= KALMAN FILTER AS TRACKER =========================*/
44 /* State vector is (x,y,w,h,dx,dy,dw,dh). */
45 /* Measurement is (x,y,w,h) */
46
47 /* Dynamic matrix A: */
48 const float A8[] = { 1, 0, 0, 0, 1, 0, 0, 0,
49                      0, 1, 0, 0, 0, 1, 0, 0,
50                      0, 0, 1, 0, 0, 0, 1, 0,
51                      0, 0, 0, 1, 0, 0, 0, 1,
52                      0, 0, 0, 0, 1, 0, 0, 0,
53                      0, 0, 0, 0, 0, 1, 0, 0,
54                      0, 0, 0, 0, 0, 0, 1, 0,
55                      0, 0, 0, 0, 0, 0, 0, 1};
56
57 /* Measurement matrix H: */
58 const float H8[] = { 1, 0, 0, 0, 0, 0, 0, 0,
59                      0, 1, 0, 0, 0, 0, 0, 0,
60                      0, 0, 1, 0, 0, 0, 0, 0,
61                      0, 0, 0, 1, 0, 0, 0, 0};
62
63 /* Matices for zero size velocity: */ 
64 /* Dynamic matrix A: */
65 const float A6[] = { 1, 0, 0, 0, 1, 0,
66                      0, 1, 0, 0, 0, 1,
67                      0, 0, 1, 0, 0, 0,
68                      0, 0, 0, 1, 0, 0,
69                      0, 0, 0, 0, 1, 0,
70                      0, 0, 0, 0, 0, 1};
71
72 /* Measurement matrix H: */
73 const float H6[] = { 1, 0, 0, 0, 0, 0,
74                      0, 1, 0, 0, 0, 0,
75                      0, 0, 1, 0, 0, 0,
76                      0, 0, 0, 1, 0, 0};
77
78 #define STATE_NUM 6
79 #define A A6
80 #define H H6
81 class CvBlobTrackerOneKalman:public CvBlobTrackerOne
82 {
83 private:
84     CvBlob      m_Blob;
85     CvKalman*   m_pKalman;
86     int         m_Frame;
87
88 public:
89     CvBlobTrackerOneKalman()
90     {
91         m_Frame = 0;
92         m_pKalman = cvCreateKalman(STATE_NUM,4);
93         memcpy( m_pKalman->transition_matrix->data.fl, A, sizeof(A));
94         memcpy( m_pKalman->measurement_matrix->data.fl, H, sizeof(H));
95         cvSetIdentity( m_pKalman->process_noise_cov, cvRealScalar(1e-5) );
96         cvSetIdentity( m_pKalman->measurement_noise_cov, cvRealScalar(1e-1) );
97     //    CV_MAT_ELEM(*m_pKalman->measurement_noise_cov, float, 2,2) *= (float)pow(20,2);
98     //    CV_MAT_ELEM(*m_pKalman->measurement_noise_cov, float, 3,3) *= (float)pow(20,2);
99         cvSetIdentity( m_pKalman->error_cov_post, cvRealScalar(1));
100         cvZero(m_pKalman->state_post);
101         cvZero(m_pKalman->state_pre);
102     }
103
104     ~CvBlobTrackerOneKalman()
105     {
106         cvReleaseKalman(&m_pKalman);
107     }
108
109     virtual void Init(CvBlob* pBlob, IplImage* /*pImg*/, IplImage* /*pImgFG*/ = NULL)
110     {
111         m_Blob = pBlob[0];
112         m_pKalman->state_post->data.fl[0] = CV_BLOB_X(pBlob);
113         m_pKalman->state_post->data.fl[1] = CV_BLOB_Y(pBlob);
114         m_pKalman->state_post->data.fl[2] = CV_BLOB_WX(pBlob);
115         m_pKalman->state_post->data.fl[3] = CV_BLOB_WY(pBlob);
116     }
117
118     virtual CvBlob* Process(CvBlob* pBlob, IplImage* /*pImg*/, IplImage* /*pImgFG*/ = NULL)
119     {
120         CvBlob* pBlobRes = &m_Blob;
121         float   Z[4];
122         CvMat   Zmat = cvMat(4,1,CV_32F,Z);
123         m_Blob = pBlob[0];
124
125         if(m_Frame < 2)
126         {   /* First call: */
127             m_pKalman->state_post->data.fl[0+4] = CV_BLOB_X(pBlob)-m_pKalman->state_post->data.fl[0];
128             m_pKalman->state_post->data.fl[1+4] = CV_BLOB_Y(pBlob)-m_pKalman->state_post->data.fl[1];
129             if(m_pKalman->DP>6)
130             {
131                 m_pKalman->state_post->data.fl[2+4] = CV_BLOB_WX(pBlob)-m_pKalman->state_post->data.fl[2];
132                 m_pKalman->state_post->data.fl[3+4] = CV_BLOB_WY(pBlob)-m_pKalman->state_post->data.fl[3];
133             }
134             m_pKalman->state_post->data.fl[0] = CV_BLOB_X(pBlob);
135             m_pKalman->state_post->data.fl[1] = CV_BLOB_Y(pBlob);
136             m_pKalman->state_post->data.fl[2] = CV_BLOB_WX(pBlob);
137             m_pKalman->state_post->data.fl[3] = CV_BLOB_WY(pBlob);
138             memcpy(m_pKalman->state_pre->data.fl,m_pKalman->state_post->data.fl,sizeof(float)*STATE_NUM);
139         }
140         else
141         {   /* Another call: */
142             Z[0] = CV_BLOB_X(pBlob);
143             Z[1] = CV_BLOB_Y(pBlob);
144             Z[2] = CV_BLOB_WX(pBlob);
145             Z[3] = CV_BLOB_WY(pBlob);
146             cvKalmanCorrect(m_pKalman,&Zmat);
147             cvKalmanPredict(m_pKalman,0);
148             cvMatMulAdd(m_pKalman->measurement_matrix, m_pKalman->state_pre, NULL, &Zmat);
149             CV_BLOB_X(pBlobRes) = Z[0];
150             CV_BLOB_Y(pBlobRes) = Z[1];
151             CV_BLOB_WX(pBlobRes) = Z[2];
152             CV_BLOB_WY(pBlobRes) = Z[3];
153         }
154         m_Frame++;
155         return pBlobRes;
156     }
157     virtual void Release()
158     {
159         delete this;
160     }
161 };  /* class CvBlobTrackerOneKalman */
162
163 static CvBlobTrackerOne* cvCreateModuleBlobTrackerOneKalman()
164 {
165     return (CvBlobTrackerOne*) new CvBlobTrackerOneKalman;
166 }
167
168 CvBlobTracker* cvCreateBlobTrackerKalman()
169 {
170     return cvCreateBlobTrackerList(cvCreateModuleBlobTrackerOneKalman);
171 }