mexopencv  0.1
mex interface for opencv library
DescriptorExtractor_.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<DescriptorExtractor> > 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<=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>=3 && nlhs<=1);
43  obj_[++last_id] = createDescriptorExtractor(
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<DescriptorExtractor> 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 == "defaultNorm") {
96  nargchk(nrhs==2 && nlhs<=1);
97  plhs[0] = MxArray(NormTypeInv[obj->defaultNorm()]);
98  }
99  else if (method == "descriptorSize") {
100  nargchk(nrhs==2 && nlhs<=1);
101  plhs[0] = MxArray(obj->descriptorSize());
102  }
103  else if (method == "descriptorType") {
104  nargchk(nrhs==2 && nlhs<=1);
105  plhs[0] = MxArray(ClassNameInvMap[obj->descriptorType()]);
106  }
107  else if (method == "compute") {
108  nargchk(nrhs==4 && nlhs<=2);
109  if (rhs[2].isNumeric()) { // first variant that accepts an image
110  Mat image(rhs[2].toMat(CV_8U)), descriptors;
111  vector<KeyPoint> keypoints(rhs[3].toVector<KeyPoint>());
112  obj->compute(image, keypoints, descriptors);
113  plhs[0] = MxArray(descriptors);
114  if (nlhs > 1)
115  plhs[1] = MxArray(keypoints);
116  }
117  else if (rhs[2].isCell()) { // second variant that accepts an image set
118  //vector<Mat> images(rhs[2].toVector<Mat>());
119  vector<Mat> images, descriptors;
120  {
121  vector<MxArray> arr(rhs[2].toVector<MxArray>());
122  images.reserve(arr.size());
123  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
124  images.push_back(it->toMat(CV_8U));
125  }
126  vector<vector<KeyPoint> > keypoints(rhs[3].toVector(
127  const_mem_fun_ref_t<vector<KeyPoint>, MxArray>(
128  &MxArray::toVector<KeyPoint>)));
129  obj->compute(images, keypoints, descriptors);
130  plhs[0] = MxArray(descriptors);
131  if (nlhs > 1)
132  plhs[1] = MxArray(keypoints);
133  }
134  else
135  mexErrMsgIdAndTxt("mexopencv:error", "Invalid arguments");
136  }
137  //else if (method == "detect")
138  //else if (method == "detectAndCompute")
139  else
140  mexErrMsgIdAndTxt("mexopencv:error",
141  "Unrecognized operation %s",method.c_str());
142 }
cv::Ptr< cv::DescriptorExtractor > createDescriptorExtractor(const std::string &type, std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Factory function for DescriptorExtractor creation.
const ConstMap< int, std::string > NormTypeInv
Inverse norm type map for option processing.
Definition: mexopencv.hpp:145
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.
const ConstMap< int, std::string > ClassNameInvMap
Translates data type definition used in OpenCV to that of MATLAB.
Definition: mexopencv.hpp:42
Global constant definitions.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.