mexopencv  0.1
mex interface for opencv library
KAZE_.cpp
Go to the documentation of this file.
1 
8 #include <typeinfo>
9 #include "mexopencv.hpp"
10 #include "mexopencv_features2d.hpp"
11 using namespace std;
12 using namespace cv;
13 
14 // Persistent objects
15 namespace {
17 int last_id = 0;
19 map<int,Ptr<KAZE> > obj_;
20 }
21 
29 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
30 {
31  // Check the number of arguments
32  nargchk(nrhs>=2 && nlhs<=2);
33 
34  // Argument vector
35  vector<MxArray> rhs(prhs, prhs+nrhs);
36  int id = rhs[0].toInt();
37  string method(rhs[1].toString());
38 
39  // Constructor is called. Create a new object from argument
40  if (method == "new") {
41  nargchk(nrhs>=2 && nlhs<=1);
42  obj_[++last_id] = createKAZE(rhs.begin() + 2, rhs.end());
43  plhs[0] = MxArray(last_id);
44  return;
45  }
46 
47  // Big operation switch
48  Ptr<KAZE> obj = obj_[id];
49  if (method == "delete") {
50  nargchk(nrhs==2 && nlhs==0);
51  obj_.erase(id);
52  }
53  else if (method == "typeid") {
54  nargchk(nrhs==2 && nlhs<=1);
55  plhs[0] = MxArray(string(typeid(*obj).name()));
56  }
57  else if (method == "clear") {
58  nargchk(nrhs==2 && nlhs==0);
59  obj->clear();
60  }
61  else if (method == "load") {
62  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
63  string objname;
64  bool loadFromString = false;
65  for (int i=3; i<nrhs; i+=2) {
66  string key(rhs[i].toString());
67  if (key == "ObjName")
68  objname = rhs[i+1].toString();
69  else if (key == "FromString")
70  loadFromString = rhs[i+1].toBool();
71  else
72  mexErrMsgIdAndTxt("mexopencv:error",
73  "Unrecognized option %s", key.c_str());
74  }
75  obj_[id] = (loadFromString ?
76  Algorithm::loadFromString<KAZE>(rhs[2].toString(), objname) :
77  Algorithm::load<KAZE>(rhs[2].toString(), objname));
78  }
79  else if (method == "save") {
80  nargchk(nrhs==3 && nlhs==0);
81  obj->save(rhs[2].toString());
82  }
83  else if (method == "empty") {
84  nargchk(nrhs==2 && nlhs<=1);
85  plhs[0] = MxArray(obj->empty());
86  }
87  else if (method == "getDefaultName") {
88  nargchk(nrhs==2 && nlhs<=1);
89  plhs[0] = MxArray(obj->getDefaultName());
90  }
91  else if (method == "defaultNorm") {
92  nargchk(nrhs==2 && nlhs<=1);
93  plhs[0] = MxArray(NormTypeInv[obj->defaultNorm()]);
94  }
95  else if (method == "descriptorSize") {
96  nargchk(nrhs==2 && nlhs<=1);
97  plhs[0] = MxArray(obj->descriptorSize());
98  }
99  else if (method == "descriptorType") {
100  nargchk(nrhs==2 && nlhs<=1);
101  plhs[0] = MxArray(ClassNameInvMap[obj->descriptorType()]);
102  }
103  else if (method == "detect") {
104  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
105  if (rhs[2].isNumeric()) { // first variant that accepts an image
106  Mat mask;
107  for (int i=3; i<nrhs; i+=2) {
108  string key(rhs[i].toString());
109  if (key == "Mask")
110  mask = rhs[i+1].toMat(CV_8U);
111  else
112  mexErrMsgIdAndTxt("mexopencv:error",
113  "Unrecognized option %s", key.c_str());
114  }
115  Mat image(rhs[2].toMat(rhs[2].isUint8() ? CV_8U :
116  (rhs[2].isUint16() ? CV_16U : CV_32F)));
117  vector<KeyPoint> keypoints;
118  obj->detect(image, keypoints, mask);
119  plhs[0] = MxArray(keypoints);
120  }
121  else if (rhs[2].isCell()) { // second variant that accepts an image set
122  vector<Mat> masks;
123  for (int i=3; i<nrhs; i+=2) {
124  string key(rhs[i].toString());
125  if (key == "Mask") {
126  //masks = rhs[i+1].toVector<Mat>();
127  vector<MxArray> arr(rhs[i+1].toVector<MxArray>());
128  masks.clear();
129  masks.reserve(arr.size());
130  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
131  masks.push_back(it->toMat(CV_8U));
132  }
133  else
134  mexErrMsgIdAndTxt("mexopencv:error",
135  "Unrecognized option %s", key.c_str());
136  }
137  //vector<Mat> images(rhs[2].toVector<Mat>());
138  vector<Mat> images;
139  {
140  vector<MxArray> arr(rhs[2].toVector<MxArray>());
141  images.reserve(arr.size());
142  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
143  images.push_back(it->toMat(it->isUint8() ? CV_8U :
144  (it->isUint16() ? CV_16U : CV_32F)));
145  }
146  vector<vector<KeyPoint> > keypoints;
147  obj->detect(images, keypoints, masks);
148  plhs[0] = MxArray(keypoints);
149  }
150  else
151  mexErrMsgIdAndTxt("mexopencv:error", "Invalid arguments");
152  }
153  else if (method == "compute") {
154  nargchk(nrhs==4 && nlhs<=2);
155  if (rhs[2].isNumeric()) { // first variant that accepts an image
156  Mat image(rhs[2].toMat(rhs[2].isUint8() ? CV_8U :
157  (rhs[2].isUint16() ? CV_16U : CV_32F))),
158  descriptors;
159  vector<KeyPoint> keypoints(rhs[3].toVector<KeyPoint>());
160  obj->compute(image, keypoints, descriptors);
161  plhs[0] = MxArray(descriptors);
162  if (nlhs > 1)
163  plhs[1] = MxArray(keypoints);
164  }
165  else if (rhs[2].isCell()) { // second variant that accepts an image set
166  //vector<Mat> images(rhs[2].toVector<Mat>());
167  vector<Mat> images, descriptors;
168  {
169  vector<MxArray> arr(rhs[2].toVector<MxArray>());
170  images.reserve(arr.size());
171  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
172  images.push_back(it->toMat(it->isUint8() ? CV_8U :
173  (it->isUint16() ? CV_16U : CV_32F)));
174  }
175  vector<vector<KeyPoint> > keypoints(rhs[3].toVector(
176  const_mem_fun_ref_t<vector<KeyPoint>, MxArray>(
177  &MxArray::toVector<KeyPoint>)));
178  obj->compute(images, keypoints, descriptors);
179  plhs[0] = MxArray(descriptors);
180  if (nlhs > 1)
181  plhs[1] = MxArray(keypoints);
182  }
183  else
184  mexErrMsgIdAndTxt("mexopencv:error", "Invalid arguments");
185  }
186  else if (method == "detectAndCompute") {
187  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=2);
188  Mat mask;
189  vector<KeyPoint> keypoints;
190  bool useProvidedKeypoints = false;
191  for (int i=3; i<nrhs; i+=2) {
192  string key(rhs[i].toString());
193  if (key == "Mask")
194  mask = rhs[i+1].toMat(CV_8U);
195  else if (key == "Keypoints") {
196  keypoints = rhs[i+1].toVector<KeyPoint>();
197  useProvidedKeypoints = true;
198  }
199  else
200  mexErrMsgIdAndTxt("mexopencv:error",
201  "Unrecognized option %s", key.c_str());
202  }
203  Mat image(rhs[2].toMat(rhs[2].isUint8() ? CV_8U :
204  (rhs[2].isUint16() ? CV_16U : CV_32F))),
205  descriptors;
206  obj->detectAndCompute(image, mask, keypoints, descriptors,
207  useProvidedKeypoints);
208  plhs[0] = MxArray(keypoints);
209  if (nlhs > 1)
210  plhs[1] = MxArray(descriptors);
211  }
212  else if (method == "get") {
213  nargchk(nrhs==3 && nlhs<=1);
214  string prop(rhs[2].toString());
215  if (prop == "Diffusivity")
216  plhs[0] = MxArray(KAZEDiffusivityTypeInv[obj->getDiffusivity()]);
217  else if (prop == "Extended")
218  plhs[0] = MxArray(obj->getExtended());
219  else if (prop == "NOctaveLayers")
220  plhs[0] = MxArray(obj->getNOctaveLayers());
221  else if (prop == "NOctaves")
222  plhs[0] = MxArray(obj->getNOctaves());
223  else if (prop == "Threshold")
224  plhs[0] = MxArray(obj->getThreshold());
225  else if (prop == "Upright")
226  plhs[0] = MxArray(obj->getUpright());
227  else
228  mexErrMsgIdAndTxt("mexopencv:error",
229  "Unrecognized property %s", prop.c_str());
230  }
231  else if (method == "set") {
232  nargchk(nrhs==4 && nlhs==0);
233  string prop(rhs[2].toString());
234  if (prop == "Diffusivity")
235  obj->setDiffusivity(KAZEDiffusivityType[rhs[3].toString()]);
236  else if (prop == "Extended")
237  obj->setExtended(rhs[3].toBool());
238  else if (prop == "NOctaveLayers")
239  obj->setNOctaveLayers(rhs[3].toInt());
240  else if (prop == "NOctaves")
241  obj->setNOctaves(rhs[3].toInt());
242  else if (prop == "Threshold")
243  obj->setThreshold(rhs[3].toDouble());
244  else if (prop == "Upright")
245  obj->setUpright(rhs[3].toBool());
246  else
247  mexErrMsgIdAndTxt("mexopencv:error",
248  "Unrecognized property %s", prop.c_str());
249  }
250  else
251  mexErrMsgIdAndTxt("mexopencv:error",
252  "Unrecognized operation %s",method.c_str());
253 }
const ConstMap< std::string, int > KAZEDiffusivityType
KAZE Diffusivity type.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: KAZE_.cpp:29
const ConstMap< int, std::string > NormTypeInv
Inverse norm type map for option processing.
Definition: mexopencv.hpp:145
cv::Ptr< cv::KAZE > createKAZE(std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of KAZE using options in arguments.
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 > KAZEDiffusivityTypeInv
inverse KAZE Diffusivity type
const ConstMap< int, std::string > ClassNameInvMap
Translates data type definition used in OpenCV to that of MATLAB.
Definition: mexopencv.hpp:42
Global constant definitions.