1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
10 // Intel License Agreement
12 // Copyright (C) 2000, Intel Corporation, all rights reserved.
13 // Third party copyrights are property of their respective owners.
15 // Redistribution and use in source and binary forms, with or without modification,
16 // are permitted provided that the following conditions are met:
18 // * Redistribution's of source code must retain the above copyright notice,
19 // this list of conditions and the following disclaimer.
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.
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.
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.
43 /*======================= KALMAN FILTER AS TRACKER =========================*/
44 /* State vector is (x,y,w,h,dx,dy,dw,dh). */
45 /* Measurement is (x,y,w,h) */
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};
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};
63 /* Matices for zero size velocity: */
64 /* Dynamic matrix A: */
65 const float A6[] = { 1, 0, 0, 0, 1, 0,
72 /* Measurement matrix H: */
73 const float H6[] = { 1, 0, 0, 0, 0, 0,
81 class CvBlobTrackerOneKalman:public CvBlobTrackerOne
89 CvBlobTrackerOneKalman()
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);
104 ~CvBlobTrackerOneKalman()
106 cvReleaseKalman(&m_pKalman);
109 virtual void Init(CvBlob* pBlob, IplImage* /*pImg*/, IplImage* /*pImgFG*/ = NULL)
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);
118 virtual CvBlob* Process(CvBlob* pBlob, IplImage* /*pImg*/, IplImage* /*pImgFG*/ = NULL)
120 CvBlob* pBlobRes = &m_Blob;
122 CvMat Zmat = cvMat(4,1,CV_32F,Z);
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];
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];
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);
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];
157 virtual void Release()
161 }; /* class CvBlobTrackerOneKalman */
163 static CvBlobTrackerOne* cvCreateModuleBlobTrackerOneKalman()
165 return (CvBlobTrackerOne*) new CvBlobTrackerOneKalman;
168 CvBlobTracker* cvCreateBlobTrackerKalman()
170 return cvCreateBlobTrackerList(cvCreateModuleBlobTrackerOneKalman);