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