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