mexopencv  0.1
mex interface for opencv library
DAISY_.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<DAISY> > 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] = createDAISY(rhs.begin() + 2, rhs.end());
45  plhs[0] = MxArray(last_id);
46  return;
47  }
48 
49  // Big operation switch
50  Ptr<DAISY> 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  obj_[id] = (loadFromString ?
78  Algorithm::loadFromString<DAISY>(rhs[2].toString(), objname) :
79  Algorithm::load<DAISY>(rhs[2].toString(), objname));
80  }
81  else if (method == "save") {
82  nargchk(nrhs==3 && nlhs==0);
83  obj->save(rhs[2].toString());
84  }
85  else if (method == "empty") {
86  nargchk(nrhs==2 && nlhs<=1);
87  plhs[0] = MxArray(obj->empty());
88  }
89  else if (method == "getDefaultName") {
90  nargchk(nrhs==2 && nlhs<=1);
91  plhs[0] = MxArray(obj->getDefaultName());
92  }
93  else if (method == "defaultNorm") {
94  nargchk(nrhs==2 && nlhs<=1);
95  plhs[0] = MxArray(NormTypeInv[obj->defaultNorm()]);
96  }
97  else if (method == "descriptorSize") {
98  nargchk(nrhs==2 && nlhs<=1);
99  plhs[0] = MxArray(obj->descriptorSize());
100  }
101  else if (method == "descriptorType") {
102  nargchk(nrhs==2 && nlhs<=1);
103  plhs[0] = MxArray(ClassNameInvMap[obj->descriptorType()]);
104  }
105  else if (method == "compute") {
106  nargchk(nrhs==4 && nlhs<=2);
107  if (rhs[2].isNumeric()) { // first variant that accepts an image
108  Mat image(rhs[2].toMat(rhs[2].isSingle() ? CV_32F : CV_8U)),
109  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(it->isSingle() ? CV_32F : 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 == "compute_all") {
137  nargchk((nrhs==3 || nrhs==4) && nlhs<=1);
138  Mat image(rhs[2].toMat(rhs[2].isSingle() ? CV_32F : CV_8U)),
139  descriptors;
140  if (nrhs == 4) {
141  Rect roi(rhs[3].toRect());
142  obj->compute(image, roi, descriptors);
143  }
144  else
145  obj->compute(image, descriptors);
146  plhs[0] = MxArray(descriptors);
147  }
148  else if (method == "GetDescriptor") {
149  nargchk(nrhs>=5 && (nrhs%2)==1 && nlhs<=2);
150  double y = rhs[2].toDouble(),
151  x = rhs[3].toDouble();
152  int orientation = rhs[4].toInt();
153  bool unnormalized = false;
154  bool useHomography = false;
155  Matx33d H;
156  for (int i=5; i<nrhs; i+=2) {
157  string key(rhs[i].toString());
158  if (key=="Unnormalized")
159  unnormalized = rhs[i+1].toBool();
160  else if (key=="H") {
161  H = rhs[i+1].toMatx<double,3,3>();
162  useHomography = true;
163  }
164  else
165  mexErrMsgIdAndTxt("mexopencv:error",
166  "Unrecognized option %s", key.c_str());
167  }
168  vector<float> descriptor(obj->descriptorSize());
169  bool ret = true;
170  if (unnormalized) {
171  if (useHomography)
172  ret = obj->GetUnnormalizedDescriptor(y, x, orientation,
173  &descriptor[0], H.val);
174  else
175  obj->GetUnnormalizedDescriptor(y, x, orientation,
176  &descriptor[0]);
177  }
178  else {
179  if (useHomography)
180  ret = obj->GetDescriptor(y, x, orientation,
181  &descriptor[0], H.val);
182  else
183  obj->GetDescriptor(y, x, orientation, &descriptor[0]);
184  }
185  plhs[0] = MxArray(descriptor);
186  if (nlhs>1)
187  plhs[1] = MxArray(ret);
188  }
189  //else if (method == "detect")
190  //else if (method == "detectAndCompute")
191  else
192  mexErrMsgIdAndTxt("mexopencv:error",
193  "Unrecognized operation %s",method.c_str());
194 }
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.
Definition: DAISY_.cpp:31