mexopencv  3.4.1
MEX interface for OpenCV library
RotatedRect_.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>=1 && nlhs<=1);
23 
24  // Argument vector
25  vector<MxArray> rhs(prhs, prhs+nrhs);
26  string method(rhs[0].toString());
27 
28  if (method == "from3points") {
29  nargchk(nrhs==4 && nlhs<=1);
30  if (rhs[1].isNumeric() && rhs[1].numel() == 2 &&
31  rhs[2].isNumeric() && rhs[2].numel() == 2 &&
32  rhs[3].isNumeric() && rhs[3].numel() == 2) {
33  Point2f pt1(rhs[1].toPoint2f()),
34  pt2(rhs[2].toPoint2f()),
35  pt3(rhs[3].toPoint2f());
36  plhs[0] = MxArray(RotatedRect(pt1, pt2, pt3));
37  }
38  else {
39  vector<Point2f> pts1(rhs[1].toVector<Point2f>()),
40  pts2(rhs[2].toVector<Point2f>()),
41  pts3(rhs[3].toVector<Point2f>());
42  if (pts1.size() != pts2.size() || pts1.size() != pts3.size())
43  mexErrMsgIdAndTxt("mexopencv:error", "Length mismatch");
44  vector<RotatedRect> rrects;
45  rrects.reserve(pts1.size());
46  for (size_t i = 0; i < pts1.size(); ++i)
47  rrects.push_back(RotatedRect(pts1[i], pts2[i], pts3[i]));
48  plhs[0] = MxArray(rrects);
49  }
50  }
51  else if (method == "points") {
52  nargchk(nrhs==2 && nlhs<=1);
53  Point2f pts[4];
54  if (rhs[1].numel() == 1) {
55  RotatedRect rrect(rhs[1].toRotatedRect());
56  rrect.points(pts);
57  plhs[0] = MxArray(Mat(4, 2, CV_32F, pts)); // 4x2 matrix
58  }
59  else {
60  vector<RotatedRect> rrects(rhs[1].toVector<RotatedRect>());
61  vector<Mat> vvp;
62  vvp.reserve(rrects.size());
63  for (size_t i = 0; i < rrects.size(); ++i) {
64  rrects[i].points(pts);
65  vvp.push_back(Mat(4, 2, CV_32F, pts).clone());
66  }
67  plhs[0] = MxArray(vvp); // cell array of 4x2 matrices
68  }
69  }
70  else if (method == "boundingRect") {
71  nargchk(nrhs==2 && nlhs<=1);
72  if (rhs[1].numel() == 1) {
73  RotatedRect rrect(rhs[1].toRotatedRect());
74  Rect r = rrect.boundingRect();
75  plhs[0] = MxArray(r);
76  }
77  else {
78  vector<RotatedRect> rrects(rhs[1].toVector<RotatedRect>());
79  vector<Rect> vr;
80  vr.reserve(rrects.size());
81  for (size_t i = 0; i < rrects.size(); ++i)
82  vr.push_back(rrects[i].boundingRect());
83  plhs[0] = MxArray(Mat(vr, false).reshape(1, 0));
84  }
85  }
86  else if (method == "boundingRect2f") {
87  nargchk(nrhs==2 && nlhs<=1);
88  if (rhs[1].numel() == 1) {
89  RotatedRect rrect(rhs[1].toRotatedRect());
90  Rect2f r = rrect.boundingRect2f();
91  plhs[0] = MxArray(r);
92  }
93  else {
94  vector<RotatedRect> rrects(rhs[1].toVector<RotatedRect>());
95  vector<Rect2f> vr;
96  vr.reserve(rrects.size());
97  for (size_t i = 0; i < rrects.size(); ++i)
98  vr.push_back(rrects[i].boundingRect2f());
99  plhs[0] = MxArray(Mat(vr, false).reshape(1, 0));
100  }
101  }
102  else
103  mexErrMsgIdAndTxt("mexopencv:error",
104  "Unrecognized operation %s", method.c_str());
105 }
Rect boundingRect() const
STL namespace.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
T push_back(T... args)
#define CV_32F
void points(Point2f pts[]) const
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
T size(T... args)
STL class.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Global constant definitions.
T c_str(T... args)
Buffer clone(Target target=ARRAY_BUFFER, bool autoRelease=false) const
T reserve(T... args)
Rect_< float > boundingRect2f() const