mexopencv  0.1
mex interface for opencv library
EMD.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>=2 && (nrhs%2)==0 && nlhs<=3);
23 
24  // Argument vector
25  vector<MxArray> rhs(prhs, prhs+nrhs);
26 
27  // Option processing
28  int distType = cv::DIST_L2;
29  Mat cost;
30  float lowerBound = 0;
31  for (int i=2; i<nrhs; i+=2) {
32  string key(rhs[i].toString());
33  if (key == "DistType")
34  distType = (rhs[i+1].isChar()) ?
35  DistType[rhs[i+1].toString()] : rhs[i+1].toInt();
36  else if (key == "Cost")
37  cost = rhs[i+1].toMat(CV_32F);
38  else if (key == "LowerBound")
39  lowerBound = rhs[i+1].toFloat();
40  else
41  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
42  }
43  if (distType==cv::DIST_USER && cost.empty())
44  mexErrMsgIdAndTxt("mexopencv:error",
45  "In case of user-defined distance, cost matrix must be defined");
46 
47  // Process
48  Mat signature1(rhs[0].toMat(CV_32F)),
49  signature2(rhs[1].toMat(CV_32F)),
50  flow;
51  float emd = EMD(signature1, signature2, distType, cost,
52  (nlhs>1 && cost.empty() ? &lowerBound : NULL),
53  (nlhs>2 ? flow : noArray()));
54  plhs[0] = MxArray(emd);
55  if (nlhs>1)
56  plhs[1] = MxArray(lowerBound);
57  if (nlhs>2)
58  plhs[2] = MxArray(flow);
59 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: EMD.cpp:19
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
const ConstMap< std::string, int > DistType
Distance types for Distance Transform and M-estimators.
Definition: mexopencv.hpp:87
Global constant definitions.