mexopencv  0.1
mex interface for opencv library
grabCut.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  ("InitWithRect", cv::GC_INIT_WITH_RECT)
16  ("InitWithMask", cv::GC_INIT_WITH_MASK)
17  ("Eval", cv::GC_EVAL);
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<=3);
31 
32  // Argument vector
33  vector<MxArray> rhs(prhs, prhs+nrhs);
34  bool rect_variant = (rhs[1].numel() == 4 &&
35  (rhs[1].rows()==1 || rhs[1].cols()==1));
36 
37  // Option processing
38  Mat bgdModel, fgdModel;
39  int iterCount = 10;
40  int mode = cv::GC_EVAL;
41  for (int i=2; i<nrhs; i+=2) {
42  string key(rhs[i].toString());
43  if (key=="BgdModel")
44  bgdModel = rhs[i+1].toMat(CV_64F);
45  else if (key=="FgdModel")
46  fgdModel = rhs[i+1].toMat(CV_64F);
47  else if (key=="IterCount")
48  iterCount = rhs[i+1].toInt();
49  else if (key=="Mode")
50  mode = GrabCutType[rhs[i+1].toString()];
51  else
52  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
53  }
54 
55  // Initialize mask and rect
56  Mat mask;
57  Rect rect;
58  if (rect_variant) {
59  rect = rhs[1].toRect();
60  mode = cv::GC_INIT_WITH_RECT;
61  }
62  else
63  mask = rhs[1].toMat(CV_8U);
64 
65  // Process
66  Mat img(rhs[0].toMat(CV_8U));
67  grabCut(img, mask, rect, bgdModel, fgdModel, iterCount, mode);
68  plhs[0] = MxArray(mask);
69  if (nlhs > 1)
70  plhs[1] = MxArray(bgdModel);
71  if (nlhs > 2)
72  plhs[2] = MxArray(fgdModel);
73 }
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: grabCut.cpp:27
Global constant definitions.
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927