mexopencv  3.4.1
MEX interface for OpenCV library
threshold.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  ("Otsu", cv::THRESH_OTSU)
17  ("Triangle", cv::THRESH_TRIANGLE);
18 }
19 
27 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
28 {
29  // Check the number of arguments
30  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=2);
31 
32  // Argument vector
33  vector<MxArray> rhs(prhs, prhs+nrhs);
34 
35  // Option processing
36  double maxval = 255;
37  int type = cv::THRESH_BINARY;
38  for (int i=2; i<nrhs; i+=2) {
39  string key(rhs[i].toString());
40  if (key == "MaxValue")
41  maxval = rhs[i+1].toDouble();
42  else if (key == "Type")
43  type = ThreshType[rhs[i+1].toString()];
44  else
45  mexErrMsgIdAndTxt("mexopencv:error",
46  "Unrecognized option %s", key.c_str());
47  }
48 
49  // Second argument
50  double thresh = 0;
51  if (rhs[1].isChar())
52  type |= AutoThresholdTypesMap[rhs[1].toString()];
53  else
54  thresh = rhs[1].toDouble();
55 
56  // Process
57  Mat src(rhs[0].toMat()), // 8u, 16s, 16u, 32f, 64f
58  dst;
59  thresh = threshold(src, dst, thresh, maxval, type);
60  plhs[0] = MxArray(dst);
61  if (nlhs>1)
62  plhs[1] = MxArray(thresh);
63 }
const ConstMap< std::string, int > ThreshType
Thresholding type map for option processing.
Definition: mexopencv.hpp:94
THRESH_TRIANGLE
STL namespace.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
const ConstMap< string, int > AutoThresholdTypesMap
automatic threshold type for option processing
Definition: threshold.cpp:15
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
THRESH_BINARY
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: threshold.cpp:27
STL class.
Global constant definitions.
T c_str(T... args)
THRESH_OTSU
double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type)
int type() const
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
cv::Mat toMat() const