mexopencv  0.1
mex interface for opencv library
BriefDescriptorExtractor_.cpp
Go to the documentation of this file.
1 
8 #include <typeinfo>
9 #include "mexopencv.hpp"
10 #include "mexopencv_features2d.hpp"
11 #include "opencv2/xfeatures2d.hpp"
12 using namespace std;
13 using namespace cv;
14 using namespace cv::xfeatures2d;
15 
16 // Persistent objects
17 namespace {
19 int last_id = 0;
21 map<int,Ptr<BriefDescriptorExtractor> > obj_;
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<=2);
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>=2 && nlhs<=1);
44  obj_[++last_id] = createBriefDescriptorExtractor(
45  rhs.begin() + 2, rhs.end());
46  plhs[0] = MxArray(last_id);
47  return;
48  }
49 
50  // Big operation switch
51  Ptr<BriefDescriptorExtractor> obj = obj_[id];
52  if (method == "delete") {
53  nargchk(nrhs==2 && nlhs==0);
54  obj_.erase(id);
55  }
56  else if (method == "typeid") {
57  nargchk(nrhs==2 && nlhs<=1);
58  plhs[0] = MxArray(string(typeid(*obj).name()));
59  }
60  else if (method == "clear") {
61  nargchk(nrhs==2 && nlhs==0);
62  obj->clear();
63  }
64  else if (method == "load") {
65  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
66  string objname;
67  bool loadFromString = false;
68  for (int i=3; i<nrhs; i+=2) {
69  string key(rhs[i].toString());
70  if (key == "ObjName")
71  objname = rhs[i+1].toString();
72  else if (key == "FromString")
73  loadFromString = rhs[i+1].toBool();
74  else
75  mexErrMsgIdAndTxt("mexopencv:error",
76  "Unrecognized option %s", key.c_str());
77  }
78  obj_[id] = (loadFromString ?
79  Algorithm::loadFromString<BriefDescriptorExtractor>(rhs[2].toString(), objname) :
80  Algorithm::load<BriefDescriptorExtractor>(rhs[2].toString(), objname));
81  }
82  else if (method == "save") {
83  nargchk(nrhs==3 && nlhs==0);
84  obj->save(rhs[2].toString());
85  }
86  else if (method == "empty") {
87  nargchk(nrhs==2 && nlhs<=1);
88  plhs[0] = MxArray(obj->empty());
89  }
90  else if (method == "getDefaultName") {
91  nargchk(nrhs==2 && nlhs<=1);
92  plhs[0] = MxArray(obj->getDefaultName());
93  }
94  else if (method == "defaultNorm") {
95  nargchk(nrhs==2 && nlhs<=1);
96  plhs[0] = MxArray(NormTypeInv[obj->defaultNorm()]);
97  }
98  else if (method == "descriptorSize") {
99  nargchk(nrhs==2 && nlhs<=1);
100  plhs[0] = MxArray(obj->descriptorSize());
101  }
102  else if (method == "descriptorType") {
103  nargchk(nrhs==2 && nlhs<=1);
104  plhs[0] = MxArray(ClassNameInvMap[obj->descriptorType()]);
105  }
106  else if (method == "compute") {
107  nargchk(nrhs==4 && nlhs<=2);
108  if (rhs[2].isNumeric()) { // first variant that accepts an image
109  Mat image(rhs[2].toMat(CV_8U)), descriptors;
110  vector<KeyPoint> keypoints(rhs[3].toVector<KeyPoint>());
111  obj->compute(image, keypoints, descriptors);
112  plhs[0] = MxArray(descriptors);
113  if (nlhs > 1)
114  plhs[1] = MxArray(keypoints);
115  }
116  else if (rhs[2].isCell()) { // second variant that accepts an image set
117  //vector<Mat> images(rhs[2].toVector<Mat>());
118  vector<Mat> images, descriptors;
119  {
120  vector<MxArray> arr(rhs[2].toVector<MxArray>());
121  images.reserve(arr.size());
122  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
123  images.push_back(it->toMat(CV_8U));
124  }
125  vector<vector<KeyPoint> > keypoints(rhs[3].toVector(
126  const_mem_fun_ref_t<vector<KeyPoint>, MxArray>(
127  &MxArray::toVector<KeyPoint>)));
128  obj->compute(images, keypoints, descriptors);
129  plhs[0] = MxArray(descriptors);
130  if (nlhs > 1)
131  plhs[1] = MxArray(keypoints);
132  }
133  else
134  mexErrMsgIdAndTxt("mexopencv:error", "Invalid arguments");
135  }
136  //else if (method == "detect")
137  //else if (method == "detectAndCompute")
138  else
139  mexErrMsgIdAndTxt("mexopencv:error",
140  "Unrecognized operation %s",method.c_str());
141 }
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.