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