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