mexopencv  0.1
mex interface for opencv library
DescriptorMatcher_.cpp
Go to the documentation of this file.
1 
9 #include <typeinfo>
10 #include "mexopencv.hpp"
11 #include "mexopencv_features2d.hpp"
12 using namespace std;
13 using namespace cv;
14 
15 // Persistent objects
16 namespace {
18 int last_id = 0;
20 map<int,Ptr<DescriptorMatcher> > obj_;
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<=1);
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>=3 && nlhs<=1);
43  obj_[++last_id] = createDescriptorMatcher(
44  rhs[2].toString(), rhs.begin() + 3, rhs.end());
45  plhs[0] = MxArray(last_id);
46  return;
47  }
48 
49  // Big operation switch
50  Ptr<DescriptorMatcher> obj = obj_[id];
51  if (method == "delete") {
52  nargchk(nrhs==2 && nlhs==0);
53  obj_.erase(id);
54  }
55  else if (method == "typeid") {
56  nargchk(nrhs==2 && nlhs<=1);
57  plhs[0] = MxArray(string(typeid(*obj).name()));
58  }
59  else if (method == "clear") {
60  nargchk(nrhs==2 && nlhs==0);
61  obj->clear();
62  }
63  else if (method == "load") {
64  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
65  string objname;
66  bool loadFromString = false;
67  for (int i=3; i<nrhs; i+=2) {
68  string key(rhs[i].toString());
69  if (key == "ObjName")
70  objname = rhs[i+1].toString();
71  else if (key == "FromString")
72  loadFromString = rhs[i+1].toBool();
73  else
74  mexErrMsgIdAndTxt("mexopencv:error",
75  "Unrecognized option %s", key.c_str());
76  }
77  FileStorage fs(rhs[2].toString(), FileStorage::READ +
78  (loadFromString ? FileStorage::MEMORY : 0));
79  obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
80  if (obj.empty())
81  mexErrMsgIdAndTxt("mexopencv:error", "Failed to load algorithm");
82  }
83  else if (method == "save") {
84  nargchk(nrhs==3 && nlhs==0);
85  obj->save(rhs[2].toString());
86  }
87  else if (method == "empty") {
88  nargchk(nrhs==2 && nlhs<=1);
89  plhs[0] = MxArray(obj->empty());
90  }
91  else if (method == "getDefaultName") {
92  nargchk(nrhs==2 && nlhs<=1);
93  plhs[0] = MxArray(obj->getDefaultName());
94  }
95  else if (method == "isMaskSupported") {
96  nargchk(nrhs==2 && nlhs<=1);
97  plhs[0] = MxArray(obj->isMaskSupported());
98  }
99  else if (method == "getTrainDescriptors") {
100  nargchk(nrhs==2 && nlhs<=1);
101  plhs[0] = MxArray(obj->getTrainDescriptors());
102  }
103  else if (method == "add") {
104  nargchk(nrhs==3 && nlhs==0);
105  vector<Mat> descriptors;
106  {
107  vector<MxArray> va(rhs[2].toVector<MxArray>());
108  descriptors.reserve(va.size());
109  for (vector<MxArray>::const_iterator it = va.begin(); it != va.end(); ++it)
110  descriptors.push_back(it->toMat(
111  it->isUint8() ? CV_8U : CV_32F));
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(rhs[2].isUint8() ? CV_8U : CV_32F));
122  vector<DMatch> matches;
123  if (nrhs>=4 && rhs[3].isNumeric()) { // first variant
124  nargchk((nrhs%2)==0);
125  Mat trainDescriptors(rhs[3].toMat(rhs[3].isUint8() ? CV_8U : CV_32F));
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(rhs[2].isUint8() ? CV_8U : CV_32F));
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(rhs[3].isUint8() ? CV_8U : CV_32F));
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(rhs[2].isUint8() ? CV_8U : CV_32F));
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(rhs[3].isUint8() ? CV_8U : CV_32F));
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 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/ouput arguments number check.
Definition: mexopencv.hpp:166
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.
Global constant definitions.