mexopencv  3.4.1
MEX interface for OpenCV library
MSER_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
10 #include "opencv2/features2d.hpp"
11 #include <typeinfo>
12 using namespace std;
13 using namespace cv;
14 
15 // Persistent objects
16 namespace {
18 int last_id = 0;
21 }
22 
30 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
31 {
32  // Check the number of arguments
33  nargchk(nrhs>=2 && nlhs<=2);
34 
35  // Argument vector
36  vector<MxArray> rhs(prhs, prhs+nrhs);
37  int id = rhs[0].toInt();
38  string method(rhs[1].toString());
39 
40  // Constructor is called. Create a new object from argument
41  if (method == "new") {
42  nargchk(nrhs>=2 && nlhs<=1);
43  obj_[++last_id] = createMSER(rhs.begin() + 2, rhs.end());
44  plhs[0] = MxArray(last_id);
45  mexLock();
46  return;
47  }
48 
49  // Big operation switch
50  Ptr<MSER> obj = obj_[id];
51  if (obj.empty())
52  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
53  if (method == "delete") {
54  nargchk(nrhs==2 && nlhs==0);
55  obj_.erase(id);
56  mexUnlock();
57  }
58  else if (method == "typeid") {
59  nargchk(nrhs==2 && nlhs<=1);
60  plhs[0] = MxArray(string(typeid(*obj).name()));
61  }
62  else if (method == "clear") {
63  nargchk(nrhs==2 && nlhs==0);
64  obj->clear();
65  }
66  else if (method == "load") {
67  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
68  string objname;
69  bool loadFromString = false;
70  for (int i=3; i<nrhs; i+=2) {
71  string key(rhs[i].toString());
72  if (key == "ObjName")
73  objname = rhs[i+1].toString();
74  else if (key == "FromString")
75  loadFromString = rhs[i+1].toBool();
76  else
77  mexErrMsgIdAndTxt("mexopencv:error",
78  "Unrecognized option %s", key.c_str());
79  }
80  obj_[id] = (loadFromString ?
81  Algorithm::loadFromString<MSER>(rhs[2].toString(), objname) :
82  Algorithm::load<MSER>(rhs[2].toString(), objname));
83  }
84  else if (method == "save") {
85  nargchk(nrhs==3 && nlhs==0);
86  obj->save(rhs[2].toString());
87  }
88  else if (method == "empty") {
89  nargchk(nrhs==2 && nlhs<=1);
90  plhs[0] = MxArray(obj->empty());
91  }
92  else if (method == "getDefaultName") {
93  nargchk(nrhs==2 && nlhs<=1);
94  plhs[0] = MxArray(obj->getDefaultName());
95  }
96  else if (method == "detect") {
97  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
98  if (rhs[2].isNumeric()) { // first variant that accepts an image
99  Mat mask;
100  for (int i=3; i<nrhs; i+=2) {
101  string key(rhs[i].toString());
102  if (key == "Mask")
103  mask = rhs[i+1].toMat(CV_8U);
104  else
105  mexErrMsgIdAndTxt("mexopencv:error",
106  "Unrecognized option %s", key.c_str());
107  }
108  Mat image(rhs[2].toMat(CV_8U));
109  vector<KeyPoint> keypoints;
110  obj->detect(image, keypoints, mask);
111  plhs[0] = MxArray(keypoints);
112  }
113  else if (rhs[2].isCell()) { // second variant that accepts an image set
114  vector<Mat> masks;
115  for (int i=3; i<nrhs; i+=2) {
116  string key(rhs[i].toString());
117  if (key == "Mask") {
118  //masks = rhs[i+1].toVector<Mat>();
119  vector<MxArray> arr(rhs[i+1].toVector<MxArray>());
120  masks.clear();
121  masks.reserve(arr.size());
122  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
123  masks.push_back(it->toMat(CV_8U));
124  }
125  else
126  mexErrMsgIdAndTxt("mexopencv:error",
127  "Unrecognized option %s", key.c_str());
128  }
129  //vector<Mat> images(rhs[2].toVector<Mat>());
130  vector<Mat> images;
131  {
132  vector<MxArray> arr(rhs[2].toVector<MxArray>());
133  images.reserve(arr.size());
134  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
135  images.push_back(it->toMat(CV_8U));
136  }
137  vector<vector<KeyPoint> > keypoints;
138  obj->detect(images, keypoints, masks);
139  plhs[0] = MxArray(keypoints);
140  }
141  else
142  mexErrMsgIdAndTxt("mexopencv:error", "Invalid arguments");
143  }
144  else if (method == "detectRegions") {
145  nargchk(nrhs==3 && nlhs<=2);
146  Mat image(rhs[2].toMat(CV_8U));
147  vector<vector<Point> > msers;
148  vector<Rect> bboxes;
149  obj->detectRegions(image, msers, bboxes);
150  vector<Mat> msers_;
151  {
152  msers_.reserve(msers.size());
153  for (vector<vector<Point> >::const_iterator it = msers.begin(); it != msers.end(); ++it)
154  msers_.push_back(Mat(*it, false).reshape(1,0));
155  }
156  //plhs[0] = MxArray(msers); // cell of cell of 2-element vectors {{[x,y], ..}, ..}
157  plhs[0] = MxArray(msers_); // cell of Nx2 matrices {[x y; ..], ..}
158  if (nlhs > 1)
159  //plhs[1] = MxArray(bboxes); // cell of 4-element vectors
160  plhs[1] = MxArray(Mat(bboxes, false).reshape(1,0)); // Nx4 matrix
161  }
162  else if (method == "get") {
163  nargchk(nrhs==3 && nlhs<=1);
164  string prop(rhs[2].toString());
165  if (prop == "Delta")
166  plhs[0] = MxArray(obj->getDelta());
167  else if (prop == "MaxArea")
168  plhs[0] = MxArray(obj->getMaxArea());
169  else if (prop == "MinArea")
170  plhs[0] = MxArray(obj->getMinArea());
171  else if (prop == "Pass2Only")
172  plhs[0] = MxArray(obj->getPass2Only());
173  else
174  mexErrMsgIdAndTxt("mexopencv:error",
175  "Unrecognized property %s", prop.c_str());
176  }
177  else if (method == "set") {
178  nargchk(nrhs==4 && nlhs==0);
179  string prop(rhs[2].toString());
180  if (prop == "Delta")
181  obj->setDelta(rhs[3].toInt());
182  else if (prop == "MaxArea")
183  obj->setMaxArea(rhs[3].toInt());
184  else if (prop == "MinArea")
185  obj->setMinArea(rhs[3].toInt());
186  else if (prop == "Pass2Only")
187  obj->setPass2Only(rhs[3].toBool());
188  else
189  mexErrMsgIdAndTxt("mexopencv:error",
190  "Unrecognized property %s", prop.c_str());
191  }
192  //else if (method == "defaultNorm")
193  //else if (method == "descriptorSize")
194  //else if (method == "descriptorType")
195  //else if (method == "compute")
196  //else if (method == "detectAndCompute")
197  else
198  mexErrMsgIdAndTxt("mexopencv:error",
199  "Unrecognized operation %s",method.c_str());
200 }
int last_id
Last object id to allocate.
Definition: MSER_.cpp:18
cv::Ptr< cv::MSER > createMSER(std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of MSER using options in arguments.
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
virtual String getDefaultName() const
virtual int getMaxArea() const=0
#define CV_8U
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: MSER_.cpp:30
STL namespace.
virtual void detectRegions(InputArray image, std::vector< std::vector< Point > > &msers, std::vector< Rect > &bboxes)=0
virtual void detect(InputArray image, std::vector< KeyPoint > &keypoints, InputArray mask=noArray())
T end(T... args)
virtual int getMinArea() const=0
virtual int getDelta() const=0
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual void setDelta(int delta)=0
virtual void setPass2Only(bool f)=0
T push_back(T... args)
virtual void setMaxArea(int maxArea)=0
virtual void clear()
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...
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
virtual void setMinArea(int minArea)=0
T clear(T... args)
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
Common definitions for the features2d and xfeatures2d modules.
T size(T... args)
STL class.
bool empty() const
Global constant definitions.
T begin(T... args)
T c_str(T... args)
map< int, Ptr< MSER > > obj_
Object container.
Definition: MSER_.cpp:20
virtual bool empty() const
virtual void save(const String &filename) const
cv::Mat toMat() const
virtual bool getPass2Only() const=0
T reserve(T... args)