mexopencv  3.4.1
MEX interface for OpenCV library
morphologyEx.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  ("Erode", cv::MORPH_ERODE)
17  ("Dilate", cv::MORPH_DILATE)
18  ("Open", cv::MORPH_OPEN)
19  ("Close", cv::MORPH_CLOSE)
20  ("Gradient", cv::MORPH_GRADIENT)
21  ("Tophat", cv::MORPH_TOPHAT)
22  ("Blackhat", cv::MORPH_BLACKHAT)
23  ("HitMiss", cv::MORPH_HITMISS);
24 }
25 
33 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
34 {
35  // Check the number of arguments
36  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
37 
38  // Argument vector
39  vector<MxArray> rhs(prhs, prhs+nrhs);
40  int op = MorphType[rhs[1].toString()];
41 
42  // Option processing
43  Mat kernel;
44  Point anchor(-1,-1);
45  int iterations = 1;
46  int borderType = cv::BORDER_CONSTANT;
47  Scalar borderValue = morphologyDefaultBorderValue();
48  for (int i=2; i<nrhs; i+=2) {
49  string key(rhs[i].toString());
50  if (key == "Element")
51  // structuring element is normally binary, but HitMiss uses 0/+1/-1
52  kernel = rhs[i+1].toMat(op == cv::MORPH_HITMISS ? CV_32S : CV_8U);
53  else if (key == "Anchor")
54  anchor = rhs[i+1].toPoint();
55  else if (key == "Iterations")
56  iterations = rhs[i+1].toInt();
57  else if (key == "BorderType")
58  borderType = BorderType[rhs[i+1].toString()];
59  else if (key == "BorderValue")
60  borderValue = rhs[i+1].toScalar();
61  else
62  mexErrMsgIdAndTxt("mexopencv:error",
63  "Unrecognized option %s", key.c_str());
64  }
65 
66  // Process
67  Mat src(rhs[0].toMat()), dst;
68  morphologyEx(src, dst, op, kernel, anchor, iterations,
69  borderType, borderValue);
70  plhs[0] = MxArray(dst);
71 }
void morphologyEx(InputArray src, OutputArray dst, int op, InputArray kernel, Point anchor=Point(-1,-1), int iterations=1, int borderType=BORDER_CONSTANT, const Scalar &borderValue=morphologyDefaultBorderValue())
#define CV_8U
MORPH_BLACKHAT
STL namespace.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
MORPH_ERODE
MORPH_OPEN
MORPH_HITMISS
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...
const ConstMap< std::string, int > BorderType
Border type map for option processing.
Definition: mexopencv.hpp:66
MORPH_GRADIENT
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.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
#define CV_32S
Global constant definitions.
T c_str(T... args)
static Scalar morphologyDefaultBorderValue()
MORPH_DILATE
const ConstMap< string, int > MorphType
Type map for morphological operation for option processing.
BORDER_CONSTANT
MORPH_CLOSE
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
MORPH_TOPHAT
cv::Mat toMat() const