mexopencv  0.1
mex interface for opencv library
norm.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 && nlhs<=1);
23 
24  // Argument vector
25  vector<MxArray> rhs(prhs, prhs+nrhs);
26 
27  // cv::norm has two overloaded variants
28  bool diff_variant = (nrhs>1 && rhs[1].isNumeric());
29  nargchk((nrhs%2) == (diff_variant ? 0 : 1));
30 
31  // Option processing
32  int normType = cv::NORM_L2;
33  bool relative = false;
34  Mat mask;
35  for (int i=(diff_variant ? 2 : 1); i<nrhs; i+=2) {
36  string key(rhs[i].toString());
37  if (key == "NormType")
38  normType = NormType[rhs[i+1].toString()];
39  else if (key == "Relative")
40  relative = rhs[i+1].toBool();
41  else if (key == "Mask")
42  mask = rhs[i+1].toMat(CV_8U);
43  else
44  mexErrMsgIdAndTxt("mexopencv:error",
45  "Unrecognized option %s", key.c_str());
46  }
47  normType |= (relative ? cv::NORM_RELATIVE : 0);
48 
49  // Process
50  double nrm;
51  Mat src1(rhs[0].toMat());
52  if (diff_variant) {
53  Mat src2(rhs[1].toMat());
54  nrm = norm(src1, src2, normType, mask);
55  }
56  else
57  nrm = norm(src1, normType, mask);
58  plhs[0] = MxArray(nrm);
59 }
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.
Definition: norm.cpp:19
const ConstMap< std::string, int > NormType
Norm type map for option processing.
Definition: mexopencv.hpp:135