mexopencv  0.1
mex interface for opencv library
distanceTransform.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  ("3", cv::DIST_MASK_3)
16  ("5", cv::DIST_MASK_5)
17  ("Precise", cv::DIST_MASK_PRECISE);
18 
20 const ConstMap<string,int> DistLabelTypes = ConstMap<string,int>
21  ("CComp", cv::DIST_LABEL_CCOMP)
22  ("Pixel", cv::DIST_LABEL_PIXEL);
23 }
24 
32 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
33 {
34  // Check the number of arguments
35  nargchk(nrhs>=1 && (nrhs%2)==1 && nlhs<=2);
36 
37  // Argument vector
38  vector<MxArray> rhs(prhs, prhs+nrhs);
39  bool with_labels = (nlhs > 1);
40 
41  // Option processing
42  int distanceType = cv::DIST_L2;
43  int maskSize = cv::DIST_MASK_3;
44  int labelType = cv::DIST_LABEL_CCOMP;
45  int dstType = CV_32F;
46  for (int i=1; i<nrhs; i+=2) {
47  string key(rhs[i].toString());
48  if (key=="DistanceType")
49  distanceType = DistType[rhs[i+1].toString()];
50  else if (key=="MaskSize")
51  maskSize = (rhs[i+1].isChar()) ?
52  DistMask[rhs[i+1].toString()] : rhs[i+1].toInt();
53  else if (with_labels && key=="LabelType")
54  labelType = DistLabelTypes[rhs[i+1].toString()];
55  else if (!with_labels && key=="DstType")
56  dstType = ClassNameMap[rhs[i+1].toString()];
57  else
58  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
59  }
60 
61  // Process
62  Mat src(rhs[0].toMat(CV_8U)), dst;
63  if (with_labels) {
64  Mat labels;
65  distanceTransform(src, dst, labels, distanceType, maskSize, labelType);
66  plhs[1] = MxArray(labels);
67  }
68  else {
69  distanceTransform(src, dst, distanceType, maskSize, dstType);
70  }
71  plhs[0] = MxArray(dst);
72 }
const ConstMap< std::string, int > ClassNameMap
Translates class name used in MATLAB to equivalent OpenCV depth.
Definition: mexopencv.hpp:27
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
const ConstMap< std::string, int > DistType
Distance types for Distance Transform and M-estimators.
Definition: mexopencv.hpp:87
Global constant definitions.
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.