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