mexopencv  0.1
mex interface for opencv library
floodFill.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 using namespace std;
10 using namespace cv;
11 
19 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
20 {
21  // Check the number of arguments
22  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=4);
23 
24  // Argument vector
25  vector<MxArray> rhs(prhs, prhs+nrhs);
26 
27  // Option processing
28  Mat mask;
29  Scalar loDiff;
30  Scalar upDiff;
31  int connectivity = 4;
32  bool fixedRange = false;
33  bool maskOnly = false;
34  int maskVal = 0x00;
35  for (int i=3; i<nrhs; i+=2) {
36  string key(rhs[i].toString());
37  if (key=="Mask")
38  mask = rhs[i+1].toMat(CV_8U);
39  else if (key=="LoDiff")
40  loDiff = rhs[i+1].toScalar();
41  else if (key=="UpDiff")
42  upDiff = rhs[i+1].toScalar();
43  else if (key=="Connectivity")
44  connectivity = rhs[i+1].toInt();
45  else if (key=="FixedRange")
46  fixedRange = rhs[i+1].toBool();
47  else if (key=="MaskOnly")
48  maskOnly = rhs[i+1].toBool();
49  else if (key=="MaskFillValue")
50  maskVal = rhs[i+1].toInt();
51  else
52  mexErrMsgIdAndTxt("mexopencv:error",
53  "Unrecognized option %s", key.c_str());
54  }
55  if (connectivity!=4 && connectivity!=8)
56  mexErrMsgIdAndTxt("mexopencv:error", "Connectivity must be 4 or 8");
57  if (maskVal<0 || maskVal>255)
58  mexErrMsgIdAndTxt("mexopencv:error", "Fill value between 1 and 255");
59  maskVal = (maskVal==0 && maskOnly) ? 1 : maskVal; // no sense in filling zeros with zeros
60  int flags = connectivity | // lower 8 bits
61  (maskVal << 8) | // middle 8 bits
62  (fixedRange ? cv::FLOODFILL_FIXED_RANGE : 0) | // higher 8 bits
63  (maskOnly ? cv::FLOODFILL_MASK_ONLY : 0); // higher 8 bits
64 
65  // Process
66  Mat img(rhs[0].toMat(rhs[0].isUint8() ? CV_8U :
67  (rhs[0].isInt32() ? CV_32S : CV_32F)));
68  Point seed(rhs[1].toPoint());
69  Scalar newVal(rhs[2].toScalar());
70  Rect rect;
71  int area = 0;
72  if (!mask.empty())
73  area = floodFill(img, mask, seed, newVal, (nlhs>1 ? &rect : NULL),
74  loDiff, upDiff, flags);
75  else
76  area = floodFill(img, seed, newVal, (nlhs>1 ? &rect : NULL),
77  loDiff, upDiff, flags);
78  plhs[0] = MxArray(img);
79  if (nlhs>1)
80  plhs[1] = MxArray(rect);
81  if (nlhs>2)
82  plhs[2] = MxArray(area);
83  if (nlhs>3)
84  plhs[3] = MxArray(mask); // keep it as uint8 for MaskFillValue to work
85 }
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
Global constant definitions.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: floodFill.cpp:19