mexopencv  0.1
mex interface for opencv library
calcCovarMatrix.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 using namespace std;
10 using namespace cv;
11 
19 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
20 {
21  // Check the number of arguments
22  nargchk(nrhs>=1 && (nrhs%2)==1 && nlhs<=2);
23 
24  // Argument vector
25  vector<MxArray> rhs(prhs, prhs+nrhs);
26 
27  // Option processing
28  Mat mean;
29  int flags = cv::COVAR_NORMAL | cv::COVAR_ROWS;
30  int ctype = CV_64F;
31  for (int i=1; i<nrhs; i+=2) {
32  string key(rhs[i].toString());
33  if (key=="Mean") {
34  mean = rhs[i+1].toMat();
35  flags |= cv::COVAR_USE_AVG;
36  }
37  else if (key=="Flags")
38  flags = rhs[i+1].toInt();
39  else if (key=="Scrambled") {
40  //UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::COVAR_SCRAMBLED);
41  UPDATE_FLAG(flags, !rhs[i+1].toBool(), cv::COVAR_NORMAL);
42  }
43  else if (key=="Normal")
44  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::COVAR_NORMAL);
45  else if (key=="UseAvg")
46  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::COVAR_USE_AVG);
47  else if (key=="Scale")
48  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::COVAR_SCALE);
49  else if (key=="Rows") {
50  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::COVAR_ROWS);
51  UPDATE_FLAG(flags, !rhs[i+1].toBool(), cv::COVAR_COLS);
52  }
53  else if (key=="Cols") {
54  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::COVAR_COLS);
55  UPDATE_FLAG(flags, !rhs[i+1].toBool(), cv::COVAR_ROWS);
56  }
57  else if (key=="CType")
58  ctype = (rhs[i+1].isChar()) ?
59  ClassNameMap[rhs[i+1].toString()] : rhs[i+1].toInt();
60  else
61  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
62  }
63 
64  // Process
65  Mat samples(rhs[0].toMat()), covar;
66  calcCovarMatrix(samples, covar, mean, flags, ctype);
67  plhs[0] = MxArray(covar);
68  if (nlhs>1)
69  plhs[1] = MxArray(mean);
70 }
const ConstMap< std::string, int > ClassNameMap
Translates class name used in MATLAB to equivalent OpenCV depth.
Definition: mexopencv.hpp:27
#define UPDATE_FLAG(NUM, TF, BIT)
set or clear a bit in flag depending on bool value
Definition: mexopencv.hpp:159
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
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.