mexopencv  3.4.1
MEX interface for OpenCV library
LineIterator.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 //NOTE: LineIterator is exposed as a function rather than a class because it
14 // it stores a reference to the input matrix from the constructor, so the mat
15 // must exist and remain for the duration of the iterator object lifetime.
16 
24 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
25 {
26  // Check the number of arguments
27  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=2);
28 
29  // Argument vector
30  vector<MxArray> rhs(prhs, prhs+nrhs);
31 
32  // Option processing
33  int connectivity = 8;
34  bool leftToRight = false;
35  for (int i=3; i<nrhs; i+=2) {
36  string key(rhs[i].toString());
37  if (key == "Connectivity")
38  connectivity = rhs[i+1].toInt();
39  else if (key == "LeftToRight")
40  leftToRight = rhs[i+1].toBool();
41  else
42  mexErrMsgIdAndTxt("mexopencv:error",
43  "Unrecognized option %s", key.c_str());
44  }
45 
46  // Process
47  Mat img(rhs[0].toMat());
48  Point pt1(rhs[1].toPoint()), pt2(rhs[2].toPoint());
49  LineIterator it(img, pt1, pt2, connectivity, leftToRight);
50  vector<Point> pts;
51  pts.reserve(it.count);
52  for (int i = 0; i < it.count; ++i) {
53  pts.push_back(it.pos());
54  ++it;
55  }
56  plhs[0] = MxArray(Mat(pts,false).reshape(1,0)); // Nx2 numeric matrix
57  if (nlhs > 1)
58  plhs[1] = MxArray(it.count);
59 }
STL namespace.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
T push_back(T... args)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
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...
Point pos() const
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.
Global constant definitions.
T c_str(T... args)
cv::Mat toMat() const
T reserve(T... args)