mexopencv  0.1
mex interface for opencv library
threshold.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 using namespace std;
10 using namespace cv;
11 
12 namespace {
14 const ConstMap<string,int> AutoThresholdTypesMap = ConstMap<string,int>
15  ("Otsu", cv::THRESH_OTSU)
16  ("Triangle", cv::THRESH_TRIANGLE);
17 }
18 
26 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
27 {
28  // Check the number of arguments
29  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=2);
30 
31  // Argument vector
32  vector<MxArray> rhs(prhs, prhs+nrhs);
33 
34  // Option processing
35  double maxval = 255;
36  int type = cv::THRESH_BINARY;
37  for (int i=2; i<nrhs; i+=2) {
38  string key(rhs[i].toString());
39  if (key == "MaxValue")
40  maxval = rhs[i+1].toDouble();
41  else if (key == "Type")
42  type = ThreshType[rhs[i+1].toString()];
43  else
44  mexErrMsgIdAndTxt("mexopencv:error",
45  "Unrecognized option %s", key.c_str());
46  }
47 
48  // Second argument
49  double thresh = 0;
50  if (rhs[1].isChar())
51  type |= AutoThresholdTypesMap[rhs[1].toString()];
52  else
53  thresh = rhs[1].toDouble();
54 
55  // Process
56  Mat src(rhs[0].toMat(rhs[0].isUint8() ? CV_8U :
57  (rhs[0].isInt16() ? CV_16S : CV_32F))), dst;
58  thresh = threshold(src, dst, thresh, maxval, type);
59  plhs[0] = MxArray(dst);
60  if (nlhs>1)
61  plhs[1] = MxArray(thresh);
62 }
const ConstMap< std::string, int > ThreshType
Thresholding type map for option processing.
Definition: mexopencv.hpp:79
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.
Definition: threshold.cpp:26
Global constant definitions.
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927