mexopencv  0.1
mex interface for opencv library
convexHull.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<=1);
23 
24  // Argument vector
25  vector<MxArray> rhs(prhs, prhs+nrhs);
26 
27  // Option processing
28  bool clockwise = false;
29  bool returnPoints = true;
30  for (int i=1; i<nrhs; i+=2) {
31  string key(rhs[i].toString());
32  if (key=="Clockwise")
33  clockwise = rhs[i+1].toBool();
34  else if (key=="ReturnPoints")
35  returnPoints = rhs[i+1].toBool();
36  else
37  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
38  }
39 
40  // Process
41  if (rhs[0].isNumeric()) {
42  Mat points(rhs[0].toMat(rhs[0].isSingle() ? CV_32F : CV_32S));
43  Mat hull; // either points or indices depending on returnPoints
44  convexHull(points, hull, clockwise, returnPoints);
45  plhs[0] = MxArray(hull.reshape(1,0)); // Nx2 or Nx1
46  }
47  else if (rhs[0].isCell()) {
48  vector<Point> points(rhs[0].toVector<Point>());
49  if (returnPoints) {
50  vector<Point> hull;
51  convexHull(points, hull, clockwise, returnPoints);
52  plhs[0] = MxArray(hull);
53  }
54  else {
55  vector<int> hull;
56  convexHull(points, hull, clockwise, returnPoints);
57  plhs[0] = MxArray(hull);
58  }
59  }
60 }
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: convexHull.cpp:19