mexopencv  3.4.1
MEX interface for OpenCV library
matchShapes.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 
13 namespace {
16  ("I1", CV_CONTOURS_MATCH_I1)
17  ("I2", CV_CONTOURS_MATCH_I2)
18  ("I3", CV_CONTOURS_MATCH_I3);
19 }
20 
28 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
29 {
30  // Check the number of arguments
31  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
32 
33  // Argument vector
34  vector<MxArray> rhs(prhs, prhs+nrhs);
35 
36  // Option processing
37  int method = CV_CONTOURS_MATCH_I1;
38  double parameter = 0;
39  for (int i=2; i<nrhs; i+=2) {
40  string key(rhs[i].toString());
41  if (key == "Method")
42  method = ShapeMatchMethodsMap[rhs[i+1].toString()];
43  else if (key == "Parameter")
44  parameter = rhs[i+1].toDouble();
45  else
46  mexErrMsgIdAndTxt("mexopencv:error",
47  "Unrecognized option %s", key.c_str());
48  }
49 
50  // Process
51  double result = 0;
52  if ((rhs[0].isNumeric() || rhs[0].isLogical()) &&
53  (rhs[1].isNumeric() || rhs[1].isLogical())) {
54  Mat contour1(rhs[0].toMat()), // CV_8U, CV_16U, CV_16S, CV_32F, CV_64F
55  contour2(rhs[1].toMat());
56  result = matchShapes(contour1, contour2, method, parameter);
57  }
58  else if (rhs[0].isCell() && rhs[1].isCell()) {
59  vector<Point2f> contour1(rhs[0].toVector<Point2f>()),
60  contour2(rhs[1].toVector<Point2f>());
61  result = matchShapes(contour1, contour2, method, parameter);
62  }
63  else
64  mexErrMsgIdAndTxt("mexopencv:error", "Invalid contour argument");
65  plhs[0] = MxArray(result);
66 }
double matchShapes(InputArray contour1, InputArray contour2, int method, double parameter)
CV_CONTOURS_MATCH_I2
STL namespace.
CV_CONTOURS_MATCH_I3
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
CV_CONTOURS_MATCH_I1
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.
Global constant definitions.
T c_str(T... args)
const ConstMap< string, int > ShapeMatchMethodsMap
Shape matching methods for option processing.
Definition: matchShapes.cpp:15
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: matchShapes.cpp:28
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
cv::Mat toMat() const