mexopencv  3.4.1
MEX interface for OpenCV library
findContours.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  ("External", cv::RETR_EXTERNAL) // retrieve only the most external (top-level) contours
17  ("List", cv::RETR_LIST) // retrieve all the contours without any hierarchical information
18  ("CComp", cv::RETR_CCOMP) // retrieve the connected components (that can possibly be nested)
19  ("Tree", cv::RETR_TREE) // retrieve all the contours and the whole hierarchy
20  ("FloodFill", cv::RETR_FLOODFILL); // connected components of a multi-level image (only for CV_32SC1)
21 
24  //("ChainCode", CV_CHAIN_CODE)
25  //("LinkRuns", CV_LINK_RUNS)
26  ("None", cv::CHAIN_APPROX_NONE)
27  ("Simple", cv::CHAIN_APPROX_SIMPLE)
28  ("TC89_L1", cv::CHAIN_APPROX_TC89_L1)
29  ("TC89_KCOS", cv::CHAIN_APPROX_TC89_KCOS);
30 }
31 
39 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
40 {
41  // Check the number of arguments
42  nargchk(nrhs>=1 && (nrhs%2)==1 && nlhs<=2);
43 
44  // Argument vector
45  vector<MxArray> rhs(prhs, prhs+nrhs);
46 
47  // Option processing
48  int mode = cv::RETR_LIST;
49  int method = cv::CHAIN_APPROX_SIMPLE;
50  Point offset;
51  for (int i=1; i<nrhs; i+=2) {
52  string key(rhs[i].toString());
53  if (key == "Mode")
54  mode = ContourMode[rhs[i+1].toString()];
55  else if (key == "Method")
56  method = ContourType[rhs[i+1].toString()];
57  else if (key == "Offset")
58  offset = rhs[i+1].toPoint();
59  else
60  mexErrMsgIdAndTxt("mexopencv:error",
61  "Unrecognized option %s", key.c_str());
62  }
63 
64  // Process
65  Mat image(rhs[0].toMat(rhs[0].isInt32() ? CV_32S : CV_8U));
66  vector<vector<Point> > contours;
67  vector<Vec4i> hierarchy;
68  findContours(image, contours, ((nlhs>1) ? hierarchy : noArray()),
69  mode, method, offset);
70  plhs[0] = MxArray(contours);
71  if (nlhs > 1)
72  plhs[1] = MxArray(hierarchy);
73 }
CHAIN_APPROX_TC89_KCOS
RETR_EXTERNAL
#define CV_8U
STL namespace.
CHAIN_APPROX_TC89_L1
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
RETR_LIST
RETR_TREE
InputOutputArray noArray()
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...
RETR_CCOMP
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.
#define CV_32S
Global constant definitions.
const ConstMap< string, int > ContourType
Type of the contour approximation algorithm for option processing.
RETR_FLOODFILL
T c_str(T... args)
CHAIN_APPROX_SIMPLE
void findContours(InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset=Point())
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
const ConstMap< string, int > ContourMode
Mode of the contour retrieval algorithm for option processing.
CHAIN_APPROX_NONE
cv::Mat toMat() const