mexopencv  0.1
mex interface for opencv library
connectedComponents.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<=4);
23 
24  // Argument vector
25  vector<MxArray> rhs(prhs, prhs+nrhs);
26 
27  // Option processing
28  int connectivity = 8;
29  int ltype = CV_32S;
30  for (int i=1; i<nrhs; i+=2) {
31  string key(rhs[i].toString());
32  if (key == "Connectivity")
33  connectivity = rhs[i+1].toInt();
34  else if (key == "LType")
35  ltype = ClassNameMap[rhs[i+1].toString()];
36  else
37  mexErrMsgIdAndTxt("mexopencv:error",
38  "Unrecognized option %s", key.c_str());
39  }
40 
41  // Process
42  Mat img(rhs[0].toMat(CV_8U)), labels;
43  int N = 0;
44  if (nlhs > 2) {
45  Mat stats, centroids;
46  N = connectedComponentsWithStats(img, labels, stats, centroids,
47  connectivity, ltype);
48  plhs[2] = MxArray(stats);
49  if (nlhs > 3)
50  plhs[3] = MxArray(centroids);
51  }
52  else
53  N = connectedComponents(img, labels, connectivity, ltype);
54  plhs[0] = MxArray(labels);
55  if (nlhs > 1)
56  plhs[1] = MxArray(N);
57 }
const ConstMap< std::string, int > ClassNameMap
Translates class name used in MATLAB to equivalent OpenCV depth.
Definition: mexopencv.hpp:27
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.