mexopencv  3.4.1
MEX interface for OpenCV library
BinaryDescriptorMatcher_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
10 using namespace std;
11 using namespace cv;
12 using namespace cv::line_descriptor;
13 
14 // Persistent objects
15 namespace {
17 int last_id = 0;
20 }
21 
29 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
30 {
31  // Check the number of arguments
32  nargchk(nrhs>=2 && nlhs<=1);
33 
34  // Argument vector
35  vector<MxArray> rhs(prhs, prhs+nrhs);
36  int id = rhs[0].toInt();
37  string method(rhs[1].toString());
38 
39  // Constructor is called. Create a new object from argument
40  if (method == "new") {
41  nargchk(nrhs==2 && nlhs<=1);
42  obj_[++last_id] = BinaryDescriptorMatcher::createBinaryDescriptorMatcher();
43  plhs[0] = MxArray(last_id);
44  mexLock();
45  return;
46  }
47 
48  // Big operation switch
50  if (obj.empty())
51  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
52  if (method == "delete") {
53  nargchk(nrhs==2 && nlhs==0);
54  obj_.erase(id);
55  mexUnlock();
56  }
57  else if (method == "clear") {
58  nargchk(nrhs==2 && nlhs==0);
59  obj->clear();
60  }
61  else if (method == "load") {
62  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
63  string objname;
64  bool loadFromString = false;
65  for (int i=3; i<nrhs; i+=2) {
66  string key(rhs[i].toString());
67  if (key == "ObjName")
68  objname = rhs[i+1].toString();
69  else if (key == "FromString")
70  loadFromString = rhs[i+1].toBool();
71  else
72  mexErrMsgIdAndTxt("mexopencv:error",
73  "Unrecognized option %s", key.c_str());
74  }
75  /*
76  obj_[id] = (loadFromString ?
77  Algorithm::loadFromString<BinaryDescriptorMatcher>(rhs[2].toString(), objname) :
78  Algorithm::load<BinaryDescriptorMatcher>(rhs[2].toString(), objname));
79  */
81  // HACK: workaround for missing BinaryDescriptorMatcher::create()
82  FileStorage fs(rhs[2].toString(), FileStorage::READ +
83  (loadFromString ? FileStorage::MEMORY : 0));
84  if (!fs.isOpened())
85  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
86  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
87  if (fn.empty())
88  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
89  obj->read(fn);
90  //*/
91  }
92  else if (method == "save") {
93  nargchk(nrhs==3 && nlhs==0);
94  obj->save(rhs[2].toString());
95  }
96  else if (method == "empty") {
97  nargchk(nrhs==2 && nlhs<=1);
98  plhs[0] = MxArray(obj->empty());
99  }
100  else if (method == "getDefaultName") {
101  nargchk(nrhs==2 && nlhs<=1);
102  plhs[0] = MxArray(obj->getDefaultName());
103  }
104  else if (method == "add") {
105  nargchk(nrhs==3 && nlhs==0);
106  vector<Mat> descriptors;
107  {
108  vector<MxArray> va(rhs[2].toVector<MxArray>());
109  descriptors.reserve(va.size());
110  for (vector<MxArray>::const_iterator it = va.begin(); it != va.end(); ++it)
111  descriptors.push_back(it->toMat(CV_8U));
112  }
113  obj->add(descriptors);
114  }
115  else if (method == "train") {
116  nargchk(nrhs==2 && nlhs==0);
117  obj->train();
118  }
119  else if (method == "match") {
120  nargchk(nrhs>=3 && nlhs<=1);
121  Mat queryDescriptors(rhs[2].toMat(CV_8U));
122  vector<DMatch> matches;
123  if (nrhs>=4 && rhs[3].isNumeric()) { // first variant
124  nargchk((nrhs%2)==0);
125  Mat trainDescriptors(rhs[3].toMat(CV_8U));
126  Mat mask;
127  for (int i=4; i<nrhs; i+=2) {
128  string key(rhs[i].toString());
129  if (key == "Mask")
130  mask = rhs[i+1].toMat(CV_8U);
131  else
132  mexErrMsgIdAndTxt("mexopencv:error",
133  "Unrecognized option %s", key.c_str());
134  }
135  obj->match(queryDescriptors, trainDescriptors, matches, mask);
136  }
137  else { // second variant
138  nargchk((nrhs%2)==1);
139  vector<Mat> masks;
140  for (int i=3; i<nrhs; i+=2) {
141  string key(rhs[i].toString());
142  if (key == "Mask") {
143  //masks = rhs[i+1].toVector<Mat>();
144  vector<MxArray> va(rhs[i+1].toVector<MxArray>());
145  masks.clear();
146  masks.reserve(va.size());
147  for (vector<MxArray>::const_iterator it = va.begin(); it != va.end(); ++it)
148  masks.push_back(it->toMat(CV_8U));
149  }
150  else
151  mexErrMsgIdAndTxt("mexopencv:error",
152  "Unrecognized option %s", key.c_str());
153  }
154  obj->match(queryDescriptors, matches, masks);
155  }
156  plhs[0] = MxArray(matches);
157  }
158  else if (method == "knnMatch") {
159  nargchk(nrhs>=4 && nlhs<=1);
160  Mat queryDescriptors(rhs[2].toMat(CV_8U));
161  vector<vector<DMatch> > matches;
162  if (nrhs>=5 && rhs[3].isNumeric() && rhs[4].isNumeric()) { // first variant
163  nargchk((nrhs%2)==1);
164  Mat trainDescriptors(rhs[3].toMat(CV_8U));
165  int k = rhs[4].toInt();
166  Mat mask;
167  bool compactResult = false;
168  for (int i=5; i<nrhs; i+=2) {
169  string key(rhs[i].toString());
170  if (key == "Mask")
171  mask = rhs[i+1].toMat(CV_8U);
172  else if (key == "CompactResult")
173  compactResult = rhs[i+1].toBool();
174  else
175  mexErrMsgIdAndTxt("mexopencv:error",
176  "Unrecognized option %s", key.c_str());
177  }
178  obj->knnMatch(queryDescriptors, trainDescriptors, matches,
179  k, mask, compactResult);
180  }
181  else { // second variant
182  nargchk((nrhs%2)==0);
183  int k = rhs[3].toInt();
184  vector<Mat> masks;
185  bool compactResult = false;
186  for (int i=4; i<nrhs; i+=2) {
187  string key(rhs[i].toString());
188  if (key == "Mask") {
189  //masks = rhs[i+1].toVector<Mat>();
190  vector<MxArray> va(rhs[i+1].toVector<MxArray>());
191  masks.clear();
192  masks.reserve(va.size());
193  for (vector<MxArray>::const_iterator it = va.begin(); it != va.end(); ++it)
194  masks.push_back(it->toMat(CV_8U));
195  }
196  else if (key == "CompactResult")
197  compactResult = rhs[i+1].toBool();
198  else
199  mexErrMsgIdAndTxt("mexopencv:error",
200  "Unrecognized option %s", key.c_str());
201  }
202  obj->knnMatch(queryDescriptors, matches, k, masks, compactResult);
203  }
204  plhs[0] = MxArray(matches);
205  }
206  else if (method == "radiusMatch") {
207  nargchk(nrhs>=4 && nlhs<=1);
208  Mat queryDescriptors(rhs[2].toMat(CV_8U));
209  vector<vector<DMatch> > matches;
210  if (nrhs>=5 && rhs[3].isNumeric() && rhs[4].isNumeric()) { // first variant
211  nargchk((nrhs%2)==1);
212  Mat trainDescriptors(rhs[3].toMat(CV_8U));
213  float maxDistance = rhs[4].toFloat();
214  Mat mask;
215  bool compactResult = false;
216  for (int i=5; i<nrhs; i+=2) {
217  string key(rhs[i].toString());
218  if (key == "Mask")
219  mask = rhs[i+1].toMat(CV_8U);
220  else if (key == "CompactResult")
221  compactResult = rhs[i+1].toBool();
222  else
223  mexErrMsgIdAndTxt("mexopencv:error",
224  "Unrecognized option %s", key.c_str());
225  }
226  obj->radiusMatch(queryDescriptors, trainDescriptors, matches,
227  maxDistance, mask, compactResult);
228  }
229  else { // second variant
230  nargchk((nrhs%2)==0);
231  float maxDistance = rhs[3].toFloat();
232  vector<Mat> masks;
233  bool compactResult = false;
234  for (int i=4; i<nrhs; i+=2) {
235  string key(rhs[i].toString());
236  if (key == "Mask") {
237  //masks = rhs[i+1].toVector<Mat>();
238  vector<MxArray> va(rhs[i+1].toVector<MxArray>());
239  masks.clear();
240  masks.reserve(va.size());
241  for (vector<MxArray>::const_iterator it = va.begin(); it != va.end(); ++it)
242  masks.push_back(it->toMat(CV_8U));
243  }
244  else if (key == "CompactResult")
245  compactResult = rhs[i+1].toBool();
246  else
247  mexErrMsgIdAndTxt("mexopencv:error",
248  "Unrecognized option %s", key.c_str());
249  }
250  obj->radiusMatch(queryDescriptors, matches,
251  maxDistance, masks, compactResult);
252  }
253  plhs[0] = MxArray(matches);
254  }
255  else
256  mexErrMsgIdAndTxt("mexopencv:error",
257  "Unrecognized operation %s",method.c_str());
258 }
T empty(T... args)
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
#define CV_8U
void knnMatch(const Mat &queryDescriptors, const Mat &trainDescriptors, std::vector< std::vector< DMatch > > &matches, int k, const Mat &mask=Mat(), bool compactResult=false) const
STL namespace.
T end(T... args)
virtual bool isOpened() const
void add(const std::vector< Mat > &descriptors)
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
T push_back(T... args)
virtual void read(const FileNode &fn)
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.
T clear(T... args)
void radiusMatch(const Mat &queryDescriptors, const Mat &trainDescriptors, std::vector< std::vector< DMatch > > &matches, float maxDistance, const Mat &mask=Mat(), bool compactResult=false) 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
FileNode getFirstTopLevelNode() const
T size(T... args)
STL class.
bool empty() const
virtual String getDefaultName() const
Global constant definitions.
T begin(T... args)
map< int, Ptr< BinaryDescriptorMatcher > > obj_
Object container.
T c_str(T... args)
void match(const Mat &queryDescriptors, const Mat &trainDescriptors, std::vector< DMatch > &matches, const Mat &mask=Mat()) const
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
virtual void save(const String &filename) const
virtual bool empty() const
cv::Mat toMat() const
T reserve(T... args)