mexopencv  3.4.1
MEX interface for OpenCV library
connectedComponents.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  ("Wu", cv::CCL_WU)
17  ("Default", cv::CCL_DEFAULT)
18  ("Grana", cv::CCL_GRANA);
19 }
20 
28 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
29 {
30  // Check the number of arguments
31  nargchk(nrhs>=1 && (nrhs%2)==1 && nlhs<=4);
32 
33  // Argument vector
34  vector<MxArray> rhs(prhs, prhs+nrhs);
35 
36  // Option processing
37  int connectivity = 8;
38  int ltype = CV_32S;
39  int ccltype = cv::CCL_DEFAULT;
40  for (int i=1; i<nrhs; i+=2) {
41  string key(rhs[i].toString());
42  if (key == "Connectivity")
43  connectivity = rhs[i+1].toInt();
44  else if (key == "LType")
45  ltype = ClassNameMap[rhs[i+1].toString()];
46  else if (key == "Method")
47  ccltype = CCLAlgMap[rhs[i+1].toString()];
48  else
49  mexErrMsgIdAndTxt("mexopencv:error",
50  "Unrecognized option %s", key.c_str());
51  }
52 
53  // Process
54  Mat img(rhs[0].toMat(CV_8U)), labels;
55  int N = 0;
56  if (nlhs > 2) {
57  Mat stats, centroids;
58  N = connectedComponentsWithStats(img, labels, stats, centroids,
59  connectivity, ltype, ccltype);
60  plhs[2] = MxArray(stats);
61  if (nlhs > 3)
62  plhs[3] = MxArray(centroids);
63  }
64  else
65  N = connectedComponents(img, labels, connectivity, ltype, ccltype);
66  plhs[0] = MxArray(labels);
67  if (nlhs > 1)
68  plhs[1] = MxArray(N);
69 }
const ConstMap< std::string, int > ClassNameMap
Translates class name used in MATLAB to equivalent OpenCV depth.
Definition: mexopencv.hpp:27
#define CV_8U
STL namespace.
CCL_GRANA
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
int connectedComponents(InputArray image, OutputArray labels, int connectivity, int ltype, int ccltype)
const ConstMap< string, int > CCLAlgMap
Map type specification.
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
CCL_DEFAULT
int connectedComponentsWithStats(InputArray image, OutputArray labels, OutputArray stats, OutputArray centroids, int connectivity, int ltype, int ccltype)
STL class.
#define CV_32S
Global constant definitions.
T c_str(T... args)
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
cv::Mat toMat() const