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 /* 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};
61 /* matices for zero size velosity */
62 /* dinamic matrix A */
63 const float A6[] = { 1, 0, 0, 0, 1, 0,
69 /* measurement matrix H */
70 const float H6[] = { 1, 0, 0, 0, 0, 0,
78 class CvBlobTrackerOneKalman:public CvBlobTrackerOne
85 CvBlobTrackerOneKalman()
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);
99 ~CvBlobTrackerOneKalman()
101 cvReleaseKalman(&m_pKalman);
103 virtual void Init(CvBlob* pBlob, IplImage* /*pImg*/, IplImage* /*pImgFG*/ = NULL)
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);
111 virtual CvBlob* Process(CvBlob* pBlob, IplImage* /*pImg*/, IplImage* /*pImgFG*/ = NULL)
113 CvBlob* pBlobRes = &m_Blob;
115 CvMat Zmat = cvMat(4,1,CV_32F,Z);
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];
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];
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);
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];
149 virtual void Release()
153 }; /* class CvBlobTrackerOneKalman */
155 static CvBlobTrackerOne* cvCreateModuleBlobTrackerOneKalman()
157 return (CvBlobTrackerOne*) new CvBlobTrackerOneKalman;
160 CvBlobTracker* cvCreateBlobTrackerKalman()
162 return cvCreateBlobTrackerList(cvCreateModuleBlobTrackerOneKalman);