mexopencv  3.4.1
MEX interface for OpenCV library
Rect_.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 == "from2points") {
29  nargchk(nrhs==3 && nlhs<=1);
30  if (rhs[1].isNumeric() && rhs[1].numel() == 2 &&
31  rhs[2].isNumeric() && rhs[2].numel() == 2) {
32  Point2d pt1(rhs[1].toPoint_<double>()),
33  pt2(rhs[2].toPoint_<double>());
34  Rect2d rect(pt1, pt2);
35  plhs[0] = MxArray(rect);
36  }
37  else {
38  vector<Point2d> pts1(rhs[1].toVector<Point2d>()),
39  pts2(rhs[2].toVector<Point2d>());
40  if (pts1.size() != pts2.size())
41  mexErrMsgIdAndTxt("mexopencv:error", "Length mismatch");
42  vector<Rect2d> rects;
43  rects.reserve(pts1.size());
44  for (size_t i = 0; i < pts1.size(); ++i)
45  rects.push_back(Rect2d(pts1[i], pts2[i]));
46  plhs[0] = (rhs[1].isCell() && rhs[2].isCell()) ?
47  MxArray(rects) : MxArray(Mat(rects, false).reshape(1, 0));
48  }
49  }
50  else if (method == "tl") {
51  nargchk(nrhs==2 && nlhs<=1);
52  if (rhs[1].isNumeric() && rhs[1].numel() == 4) {
53  Rect2d rect(rhs[1].toRect_<double>());
54  Point2d pt = rect.tl();
55  plhs[0] = MxArray(pt); // 1x2 vector [x,y]
56  }
57  else {
58  vector<Rect2d> rects(MxArrayToVectorRect<double>(rhs[1]));
59  vector<Point2d> pts;
60  pts.reserve(rects.size());
61  for (size_t i = 0; i < rects.size(); ++i)
62  pts.push_back(rects[i].tl());
63  plhs[0] = (rhs[1].isCell()) ?
64  MxArray(pts) : MxArray(Mat(pts, false).reshape(1, 0));
65  }
66  }
67  else if (method == "br") {
68  nargchk(nrhs==2 && nlhs<=1);
69  if (rhs[1].isNumeric() && rhs[1].numel() == 4) {
70  Rect2d rect(rhs[1].toRect_<double>());
71  Point2d pt = rect.br();
72  plhs[0] = MxArray(pt); // 1x2 vector [x,y]
73  }
74  else {
75  vector<Rect2d> rects(MxArrayToVectorRect<double>(rhs[1]));
76  vector<Point2d> pts;
77  pts.reserve(rects.size());
78  for (size_t i = 0; i < rects.size(); ++i)
79  pts.push_back(rects[i].br());
80  plhs[0] = (rhs[1].isCell()) ?
81  MxArray(pts) : MxArray(Mat(pts, false).reshape(1, 0));
82  }
83  }
84  else if (method == "size") {
85  nargchk(nrhs==2 && nlhs<=1);
86  if (rhs[1].isNumeric() && rhs[1].numel() == 4) {
87  Rect2d rect(rhs[1].toRect_<double>());
88  Size2d sz = rect.size();
89  plhs[0] = MxArray(sz); // 1x2 vector [w,h]
90  }
91  else {
92  vector<Rect2d> rects(MxArrayToVectorRect<double>(rhs[1]));
93  vector<Size2d> sz;
94  sz.reserve(rects.size());
95  for (size_t i = 0; i < rects.size(); ++i)
96  sz.push_back(rects[i].size());
97  plhs[0] = (rhs[1].isCell()) ?
98  MxArray(sz) : MxArray(Mat(sz, false).reshape(1, 0));
99  }
100  }
101  else if (method == "area") {
102  nargchk(nrhs==2 && nlhs<=1);
103  if (rhs[1].isNumeric() && rhs[1].numel() == 4) {
104  Rect2d rect(rhs[1].toRect_<double>());
105  double a = rect.area();
106  plhs[0] = MxArray(a);
107  }
108  else {
109  vector<Rect2d> rects(MxArrayToVectorRect<double>(rhs[1]));
110  vector<double> va;
111  va.reserve(rects.size());
112  for (size_t i = 0; i < rects.size(); ++i)
113  va.push_back(rects[i].area());
114  plhs[0] = MxArray(va);
115  }
116  }
117  else if (method == "contains") {
118  nargchk(nrhs==3 && nlhs<=1);
119  Rect2d rect(rhs[1].toRect_<double>());
120  if (rhs[2].isNumeric() && rhs[2].numel() == 2) {
121  Point2d pt(rhs[2].toPoint_<double>());
122  plhs[0] = MxArray(rect.contains(pt));
123  }
124  else {
125  vector<Point2d> pts(rhs[2].toVector<Point2d>());
126  vector<bool> vb;
127  vb.reserve(pts.size());
128  for (size_t i = 0; i < pts.size(); ++i)
129  vb.push_back(rect.contains(pts[i]));
130  plhs[0] = MxArray(vb);
131  }
132  }
133  else if (method == "adjustPosition") {
134  nargchk(nrhs==3 && nlhs<=1);
135  if (rhs[1].isNumeric() && rhs[1].numel() == 4 &&
136  rhs[2].isNumeric() && rhs[2].numel() == 2) {
137  Rect2d rect(rhs[1].toRect_<double>());
138  Point2d pt(rhs[2].toPoint_<double>());
139  rect += pt;
140  plhs[0] = MxArray(rect);
141  }
142  else {
143  vector<Rect2d> rects(MxArrayToVectorRect<double>(rhs[1]));
144  if (rhs[2].isNumeric() && rhs[2].numel() == 2) {
145  Point2d pt(rhs[2].toPoint_<double>());
146  for (size_t i = 0; i < rects.size(); ++i)
147  rects[i] += pt;
148  }
149  else {
150  vector<Point2d> pts(rhs[2].toVector<Point2d>());
151  if (rects.size() != pts.size())
152  mexErrMsgIdAndTxt("mexopencv:error", "Length mismatch");
153  for (size_t i = 0; i < rects.size(); ++i)
154  rects[i] += pts[i];
155  }
156  plhs[0] = (rhs[1].isCell()) ?
157  MxArray(rects) : MxArray(Mat(rects, false).reshape(1, 0));
158  }
159  }
160  else if (method == "adjustSize") {
161  nargchk(nrhs==3 && nlhs<=1);
162  if (rhs[1].isNumeric() && rhs[1].numel() == 4 &&
163  rhs[2].isNumeric() && rhs[2].numel() == 2) {
164  Rect2d rect(rhs[1].toRect_<double>());
165  Size2d sz(rhs[2].toSize_<double>());
166  rect += sz;
167  plhs[0] = MxArray(rect);
168  }
169  else {
170  vector<Rect2d> rects(MxArrayToVectorRect<double>(rhs[1]));
171  if (rhs[2].isNumeric() && rhs[2].numel() == 2) {
172  Size2d sz(rhs[2].toSize_<double>());
173  for (size_t i = 0; i < rects.size(); ++i)
174  rects[i] += sz;
175  }
176  else {
177  vector<Size2d> sz(MxArrayToVectorSize<double>(rhs[2]));
178  if (rects.size() != sz.size())
179  mexErrMsgIdAndTxt("mexopencv:error", "Length mismatch");
180  for (size_t i = 0; i < rects.size(); ++i)
181  rects[i] += sz[i];
182  }
183  plhs[0] = (rhs[1].isCell()) ?
184  MxArray(rects) : MxArray(Mat(rects, false).reshape(1, 0));
185  }
186  }
187  else if (method == "intersect") {
188  nargchk(nrhs==3 && nlhs<=1);
189  if (rhs[1].isNumeric() && rhs[1].numel() == 4 &&
190  rhs[2].isNumeric() && rhs[2].numel() == 4) {
191  Rect2d rect1(rhs[1].toRect_<double>()),
192  rect2(rhs[2].toRect_<double>());
193  rect1 &= rect2;
194  plhs[0] = MxArray(rect1);
195  }
196  else {
197  vector<Rect2d> rects1(MxArrayToVectorRect<double>(rhs[1]));
198  if (rhs[2].isNumeric() && rhs[2].numel() == 4) {
199  Rect2d rect2(rhs[2].toRect_<double>());
200  for (size_t i = 0; i < rects1.size(); ++i)
201  rects1[i] &= rect2;
202  }
203  else {
204  vector<Rect2d> rects2(MxArrayToVectorRect<double>(rhs[2]));
205  if (rects1.size() != rects2.size())
206  mexErrMsgIdAndTxt("mexopencv:error", "Length mismatch");
207  for (size_t i = 0; i < rects1.size(); ++i)
208  rects1[i] &= rects2[i];
209  }
210  plhs[0] = (rhs[1].isCell()) ?
211  MxArray(rects1) : MxArray(Mat(rects1, false).reshape(1, 0));
212  }
213  }
214  else if (method == "union") {
215  nargchk(nrhs==3 && nlhs<=1);
216  if (rhs[1].isNumeric() && rhs[1].numel() == 4 &&
217  rhs[2].isNumeric() && rhs[2].numel() == 4) {
218  Rect2d rect1(rhs[1].toRect_<double>()),
219  rect2(rhs[2].toRect_<double>());
220  rect1 |= rect2;
221  plhs[0] = MxArray(rect1);
222  }
223  else {
224  vector<Rect2d> rects1(MxArrayToVectorRect<double>(rhs[1]));
225  if (rhs[2].isNumeric() && rhs[2].numel() == 4) {
226  Rect2d rect2(rhs[2].toRect_<double>());
227  for (size_t i = 0; i < rects1.size(); ++i)
228  rects1[i] |= rect2;
229  }
230  else {
231  vector<Rect2d> rects2(MxArrayToVectorRect<double>(rhs[2]));
232  if (rects1.size() != rects2.size())
233  mexErrMsgIdAndTxt("mexopencv:error", "Length mismatch");
234  for (size_t i = 0; i < rects1.size(); ++i)
235  rects1[i] |= rects2[i];
236  }
237  plhs[0] = (rhs[1].isCell()) ?
238  MxArray(rects1) : MxArray(Mat(rects1, false).reshape(1, 0));
239  }
240  }
241  else if (method == "crop") {
242  nargchk((nrhs==3 || nrhs==4) && nlhs<=1);
243  Mat img(rhs[1].toMat());
244  Rect rect(rhs[2].toRect());
245  Mat roi(img, rect);
246  if (nrhs == 3) {
247  // get ROI
248  plhs[0] = MxArray(roi.clone()); //HACK: clone was needed here!
249  }
250  else {
251  // set ROI
252  Mat roi_new(rhs[3].toMat(img.depth()));
253  CV_Assert(roi_new.size() == rect.size() &&
254  roi_new.type() == img.type());
255  roi_new.copyTo(roi);
256  plhs[0] = MxArray(img);
257  }
258  }
259  else
260  mexErrMsgIdAndTxt("mexopencv:error",
261  "Unrecognized operation %s", method.c_str());
262 }
#define CV_Assert(...)
void copyTo(OutputArray m) const
STL namespace.
Point_< _Tp > tl() const
int type() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
Point_< _Tp > br() const
bool contains(const Point_< _Tp > &pt) const
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: Rect_.cpp:19
T push_back(T... args)
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...
MatSize size
Size_< _Tp > size() 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
int depth() const
T size(T... args)
STL class.
Global constant definitions.
T c_str(T... args)
_Tp area() const
Mat clone() const
cv::Mat toMat() const
T reserve(T... args)