mexopencv  0.1
mex interface for opencv library
KalmanFilter_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 using namespace std;
10 using namespace cv;
11 
12 // Persistent objects
13 namespace {
15 int last_id = 0;
17 map<int,Ptr<KalmanFilter> > obj_;
18 }
19 
27 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
28 {
29  // Check the number of arguments
30  nargchk(nrhs>=2 && nlhs<=1);
31 
32  // Argument vector
33  vector<MxArray> rhs(prhs, prhs+nrhs);
34  int id = rhs[0].toInt();
35  string method(rhs[1].toString());
36 
37  // Constructor is called. Create a new object from argument
38  if (method == "new") {
39  nargchk(nrhs==2 && nlhs<=1);
40  obj_[++last_id] = makePtr<KalmanFilter>();
41  plhs[0] = MxArray(last_id);
42  return;
43  }
44 
45  // Big operation switch
46  Ptr<KalmanFilter> obj = obj_[id];
47  if (method == "delete") {
48  nargchk(nrhs==2 && nlhs==0);
49  obj_.erase(id);
50  }
51  else if (method == "init") {
52  nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs==0);
53  int dynamParams = rhs[2].toInt();
54  int measureParams = rhs[3].toInt();
55  int controlParams = 0;
56  int type = CV_64F;
57  for (int i=4; i<nrhs; i+=2) {
58  string key(rhs[i].toString());
59  if (key=="ControlParams")
60  controlParams = rhs[i+1].toInt();
61  else if (key=="Type")
62  type = (rhs[i+1].isChar()) ?
63  ClassNameMap[rhs[i+1].toString()] : rhs[i+1].toInt();
64  else
65  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
66  }
67  obj->init(dynamParams, measureParams, controlParams, type);
68  }
69  else if (method == "predict") {
70  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
71  Mat control;
72  for (int i=2; i<nrhs; i+=2) {
73  string key(rhs[i].toString());
74  if (key=="Control")
75  control = rhs[i+1].toMat();
76  else
77  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
78  }
79  plhs[0] = MxArray(obj->predict(control));
80  }
81  else if (method == "correct") {
82  nargchk(nrhs==3 && nlhs<=1);
83  Mat measurement(rhs[2].toMat());
84  plhs[0] = MxArray(obj->correct(measurement));
85  }
86  else if (method == "get") {
87  nargchk(nrhs==3 && nlhs<=1);
88  string prop(rhs[2].toString());
89  if (prop == "statePre")
90  plhs[0] = MxArray(obj->statePre);
91  else if (prop == "statePost")
92  plhs[0] = MxArray(obj->statePost);
93  else if (prop == "transitionMatrix")
94  plhs[0] = MxArray(obj->transitionMatrix);
95  else if (prop == "controlMatrix")
96  plhs[0] = MxArray(obj->controlMatrix);
97  else if (prop == "measurementMatrix")
98  plhs[0] = MxArray(obj->measurementMatrix);
99  else if (prop == "measurementNoiseCov")
100  plhs[0] = MxArray(obj->measurementNoiseCov);
101  else if (prop == "processNoiseCov")
102  plhs[0] = MxArray(obj->processNoiseCov);
103  else if (prop == "errorCovPre")
104  plhs[0] = MxArray(obj->errorCovPre);
105  else if (prop == "errorCovPost")
106  plhs[0] = MxArray(obj->errorCovPost);
107  else if (prop == "gain")
108  plhs[0] = MxArray(obj->gain);
109  else
110  mexErrMsgIdAndTxt("mexopencv:error",
111  "Unrecognized property %s", prop.c_str());
112  }
113  else if (method == "set") {
114  nargchk(nrhs==4 && nlhs==0);
115  string prop(rhs[2].toString());
116  if (prop == "statePre")
117  obj->statePre = rhs[3].toMat();
118  else if (prop == "statePost")
119  obj->statePost = rhs[3].toMat();
120  else if (prop == "transitionMatrix")
121  obj->transitionMatrix = rhs[3].toMat();
122  else if (prop == "controlMatrix")
123  obj->controlMatrix = rhs[3].toMat();
124  else if (prop == "measurementMatrix")
125  obj->measurementMatrix = rhs[3].toMat();
126  else if (prop == "measurementNoiseCov")
127  obj->measurementNoiseCov = rhs[3].toMat();
128  else if (prop == "processNoiseCov")
129  obj->processNoiseCov = rhs[3].toMat();
130  else if (prop == "errorCovPre")
131  obj->errorCovPre = rhs[3].toMat();
132  else if (prop == "errorCovPost")
133  obj->errorCovPost = rhs[3].toMat();
134  else if (prop == "gain")
135  obj->gain = rhs[3].toMat();
136  else
137  mexErrMsgIdAndTxt("mexopencv:error",
138  "Unrecognized property %s", prop.c_str());
139  }
140  else
141  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized operation");
142 }
const ConstMap< std::string, int > ClassNameMap
Translates class name used in MATLAB to equivalent OpenCV depth.
Definition: mexopencv.hpp:27
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/ouput arguments number check.
Definition: mexopencv.hpp:166
Global constant definitions.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.