mexopencv  0.1
mex interface for opencv library
findContours.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 using namespace std;
10 using namespace cv;
11 
12 namespace {
15  ("External", cv::RETR_EXTERNAL) // retrieve only the most external (top-level) contours
16  ("List", cv::RETR_LIST) // retrieve all the contours without any hierarchical information
17  ("CComp", cv::RETR_CCOMP) // retrieve the connected components (that can possibly be nested)
18  ("Tree", cv::RETR_TREE) // retrieve all the contours and the whole hierarchy
19  ("FloodFill", cv::RETR_FLOODFILL);
20 
23  ("None", cv::CHAIN_APPROX_NONE)
24  ("Simple", cv::CHAIN_APPROX_SIMPLE)
25  ("TC89_L1", cv::CHAIN_APPROX_TC89_L1)
26  ("TC89_KCOS", cv::CHAIN_APPROX_TC89_KCOS);
27 }
28 
36 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
37 {
38  // Check the number of arguments
39  nargchk(nrhs>=1 && (nrhs%2)==1 && nlhs<=2);
40 
41  // Argument vector
42  vector<MxArray> rhs(prhs, prhs+nrhs);
43 
44  // Option processing
45  int mode = cv::RETR_EXTERNAL;
46  int method = cv::CHAIN_APPROX_NONE;
47  Point offset;
48  for (int i=1; i<nrhs; i+=2) {
49  string key(rhs[i].toString());
50  if (key=="Mode")
51  mode = ContourMode[rhs[i+1].toString()];
52  else if (key=="Method")
53  method = ContourType[rhs[i+1].toString()];
54  else if (key=="Offset")
55  offset = rhs[i+1].toPoint();
56  else
57  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
58  }
59 
60  // Process
61  Mat image(rhs[0].toMat(rhs[0].isInt32() ? CV_32S : CV_8U));
62  vector<vector<Point> > contours;
63  vector<Vec4i> hierarchy;
64  findContours(image, contours, ((nlhs>1) ? hierarchy : noArray()),
65  mode, method, offset);
66  plhs[0] = MxArray(contours);
67  if (nlhs > 1)
68  plhs[1] = MxArray(hierarchy);
69 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
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.
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927