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