ACT-CV - Machine Vision for Cognitive Modeling
OpenCVKalman.h
Go to the documentation of this file.
1 // -*- mode: c++; indent-tabs-mode: nil; c-basic-offset: 4; coding: iso-8859-1; -*-
2 
3 /*
4 ACT-CV - Machine Vision for Cognitive Modeling
5 Copyright (c) 2008 Marc Halbruegge
6 
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11 
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 
22 
28 #ifndef OPENCVKALMAN_H_
29 #define OPENCVKALMAN_H_
30 
31 #include <time.h>
32 
37 template<unsigned int _dims>
38 class KalmanFilter {
39  CvKalman* kalman;
40  CvMat* measurement;
41 
42  // for setting to random state
43  CvRandState rng;
44 public:
46  kalman = cvCreateKalman(2*_dims, _dims, 0 );
47  measurement = cvCreateMat(_dims, 1, CV_32FC1 );
48 
49  time_t seed;
50  time(&seed);
51  cvRandInit(&rng, (double) 0, (double) 0, (int)seed);
52  }
54  cvReleaseKalman(&kalman);
55  cvReleaseMat(&measurement);
56  }
57 
67  void InitRandom(float m, float sd, float processNoise, float measurementNoise) {
68 
69  // transition matrix
70  float A[2*_dims][2*_dims];
71  for (int i=0;i<2*_dims;i++) {
72  for (int j=0;j<2*_dims;j++) {
73  A[i][j]=0;
74  }
75  }
76  // pos + vel stay as they are
77  for (int i=0;i<2*_dims;i++) {
78  A[i][i]=1;
79  }
80  // add vel to pos
81  for (int i=0;i<_dims;i++) {
82  A[i][i+_dims]=1;
83  }
84 
85  memcpy(kalman->transition_matrix->data.fl, A, sizeof(A));
86 
87  cvSetIdentity(kalman->measurement_matrix, cvRealScalar(1) );
88  cvSetIdentity(kalman->process_noise_cov, cvRealScalar(processNoise) );
89  cvSetIdentity(kalman->measurement_noise_cov, cvRealScalar(measurementNoise) );
90 
91  //cvSetIdentity(kalman->error_cov_post, cvRealScalar(1));
92  cvSetIdentity(kalman->error_cov_post, cvRealScalar(sd));
93  cvSetIdentity(kalman->error_cov_pre, cvRealScalar(sd));
94 
95  /* choose random initial state */
96 #if 0
97  cvRandSetRange(&rng, sd, m, -1);
98  rng.disttype = CV_RAND_NORMAL;
99  cvRand(&rng, kalman->state_post);
100 #else
101  for (int i=0;i<_dims;i++) {
102  kalman->state_post->data.fl[i]=m; // position
103  kalman->state_post->data.fl[_dims+i]=0; // velocity
104  }
105 #endif
106  }
107 
109  void AddMeasure(const float *x) {
110  memcpy(measurement->data.fl, x, sizeof(float)*_dims);
111  cvKalmanCorrect(kalman, measurement);
112  }
113 
115  void GetCorrectedState(float *x) {
116  memcpy(x, kalman->state_post->data.fl, sizeof(float)*_dims*2);
117  }
118 
119  void GetVariance(float *x) {
120  for (int i=0;i<_dims*2;i++) {
121  x[i]=cvGetReal2D(kalman->error_cov_post, i, i);
122  }
123  }
124 
126  const CvMat* Predict() {
127  return cvKalmanPredict(kalman, 0);
128  }
129 
131  void Predict(float *x) {
132  const CvMat *prediction = Predict();
133  memcpy(x, prediction->data.fl, sizeof(float)*_dims*2);
134  }
135 };
136 
137 
138 #endif /*OPENCVKALMAN_H_*/


ACT-CV - Machine Vision for Cognitive Modeling
© 2015 Marc Halbruegge (actcvlibrary@googlemail.com)