mexopencv  3.4.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",
62  "Unrecognized option %s", key.c_str());
63  }
64 
65  // Process
66  Mat samples(rhs[0].toMat()), covar;
67  calcCovarMatrix(samples, covar, mean, flags, ctype);
68  plhs[0] = MxArray(covar);
69  if (nlhs>1)
70  plhs[1] = MxArray(mean);
71 }
const ConstMap< std::string, int > ClassNameMap
Translates class name used in MATLAB to equivalent OpenCV depth.
Definition: mexopencv.hpp:27
Scalar mean(InputArray src, InputArray mask=noArray())
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
STL namespace.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
COVAR_NORMAL
COVAR_USE_AVG
COVAR_COLS
#define UPDATE_FLAG(NUM, TF, BIT)
set or clear a bit in flag depending on bool value
Definition: mexopencv.hpp:174
#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...
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.
COVAR_ROWS
Global constant definitions.
void calcCovarMatrix(const Mat *samples, int nsamples, Mat &covar, Mat &mean, int flags, int ctype=CV_64F)
T c_str(T... args)
COVAR_SCALE
cv::Mat toMat() const