mexopencv  3.4.1
MEX interface for OpenCV library
EMD.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/imgproc.hpp"
10 using namespace std;
11 using namespace cv;
12 
20 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
21 {
22  // Check the number of arguments
23  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=3);
24 
25  // Argument vector
26  vector<MxArray> rhs(prhs, prhs+nrhs);
27 
28  // Option processing
29  int distType = cv::DIST_L2;
30  Mat cost;
31  float lowerBound = 0;
32  for (int i=2; i<nrhs; i+=2) {
33  string key(rhs[i].toString());
34  if (key == "DistType")
35  distType = (rhs[i+1].isChar()) ?
36  DistType[rhs[i+1].toString()] : rhs[i+1].toInt();
37  else if (key == "Cost")
38  cost = rhs[i+1].toMat(CV_32F);
39  else if (key == "LowerBound")
40  lowerBound = rhs[i+1].toFloat();
41  else
42  mexErrMsgIdAndTxt("mexopencv:error",
43  "Unrecognized option %s", key.c_str());
44  }
45  if (distType==cv::DIST_USER && cost.empty())
46  mexErrMsgIdAndTxt("mexopencv:error",
47  "In case of user-defined distance, cost matrix must be defined");
48 
49  // Process
50  Mat signature1(rhs[0].toMat(CV_32F)),
51  signature2(rhs[1].toMat(CV_32F)),
52  flow;
53  float emd = EMD(signature1, signature2, distType, cost,
54  (nlhs>1 && cost.empty() ? &lowerBound : NULL),
55  (nlhs>2 ? flow : noArray()));
56  plhs[0] = MxArray(emd);
57  if (nlhs>1)
58  plhs[1] = MxArray(lowerBound);
59  if (nlhs>2)
60  plhs[2] = MxArray(flow);
61 }
STL namespace.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
DIST_USER
#define CV_32F
InputOutputArray noArray()
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: EMD.cpp:20
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
const ConstMap< std::string, int > DistType
Distance types for Distance Transform and M-estimators.
Definition: mexopencv.hpp:102
STL class.
Global constant definitions.
T c_str(T... args)
float EMD(InputArray signature1, InputArray signature2, int distType, InputArray cost=noArray(), float *lowerBound=0, OutputArray flow=noArray())
bool empty() const
cv::Mat toMat() const