Update to 2.0.0 tree from current Fremantle build
[opencv] / src / cvaux / 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         SetModuleName("Kalman");
104     }
105
106     ~CvBlobTrackerOneKalman()
107     {
108         cvReleaseKalman(&m_pKalman);
109     }
110
111     virtual void Init(CvBlob* pBlob, IplImage* /*pImg*/, IplImage* /*pImgFG*/ = NULL)
112     {
113         m_Blob = pBlob[0];
114         m_pKalman->state_post->data.fl[0] = CV_BLOB_X(pBlob);
115         m_pKalman->state_post->data.fl[1] = CV_BLOB_Y(pBlob);
116         m_pKalman->state_post->data.fl[2] = CV_BLOB_WX(pBlob);
117         m_pKalman->state_post->data.fl[3] = CV_BLOB_WY(pBlob);
118     }
119
120     virtual CvBlob* Process(CvBlob* pBlob, IplImage* /*pImg*/, IplImage* /*pImgFG*/ = NULL)
121     {
122         CvBlob* pBlobRes = &m_Blob;
123         float   Z[4];
124         CvMat   Zmat = cvMat(4,1,CV_32F,Z);
125         m_Blob = pBlob[0];
126
127         if(m_Frame < 2)
128         {   /* First call: */
129             m_pKalman->state_post->data.fl[0+4] = CV_BLOB_X(pBlob)-m_pKalman->state_post->data.fl[0];
130             m_pKalman->state_post->data.fl[1+4] = CV_BLOB_Y(pBlob)-m_pKalman->state_post->data.fl[1];
131             if(m_pKalman->DP>6)
132             {
133                 m_pKalman->state_post->data.fl[2+4] = CV_BLOB_WX(pBlob)-m_pKalman->state_post->data.fl[2];
134                 m_pKalman->state_post->data.fl[3+4] = CV_BLOB_WY(pBlob)-m_pKalman->state_post->data.fl[3];
135             }
136             m_pKalman->state_post->data.fl[0] = CV_BLOB_X(pBlob);
137             m_pKalman->state_post->data.fl[1] = CV_BLOB_Y(pBlob);
138             m_pKalman->state_post->data.fl[2] = CV_BLOB_WX(pBlob);
139             m_pKalman->state_post->data.fl[3] = CV_BLOB_WY(pBlob);
140             memcpy(m_pKalman->state_pre->data.fl,m_pKalman->state_post->data.fl,sizeof(float)*STATE_NUM);
141         }
142         else
143         {   /* Another call: */
144             Z[0] = CV_BLOB_X(pBlob);
145             Z[1] = CV_BLOB_Y(pBlob);
146             Z[2] = CV_BLOB_WX(pBlob);
147             Z[3] = CV_BLOB_WY(pBlob);
148             cvKalmanCorrect(m_pKalman,&Zmat);
149             cvKalmanPredict(m_pKalman,0);
150             cvMatMulAdd(m_pKalman->measurement_matrix, m_pKalman->state_pre, NULL, &Zmat);
151             CV_BLOB_X(pBlobRes) = Z[0];
152             CV_BLOB_Y(pBlobRes) = Z[1];
153             CV_BLOB_WX(pBlobRes) = Z[2];
154             CV_BLOB_WY(pBlobRes) = Z[3];
155         }
156         m_Frame++;
157         return pBlobRes;
158     }
159     virtual void Release()
160     {
161         delete this;
162     }
163 };  /* class CvBlobTrackerOneKalman */
164
165 static CvBlobTrackerOne* cvCreateModuleBlobTrackerOneKalman()
166 {
167     return (CvBlobTrackerOne*) new CvBlobTrackerOneKalman;
168 }
169
170 CvBlobTracker* cvCreateBlobTrackerKalman()
171 {
172     return cvCreateBlobTrackerList(cvCreateModuleBlobTrackerOneKalman);
173 }