mexopencv  0.1
mex interface for opencv library
AKAZE_.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<AKAZE> > 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] = createAKAZE(rhs.begin() + 2, rhs.end());
43  plhs[0] = MxArray(last_id);
44  return;
45  }
46 
47  // Big operation switch
48  Ptr<AKAZE> 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<AKAZE>(rhs[2].toString(), objname) :
77  Algorithm::load<AKAZE>(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 == "DescriptorChannels")
216  plhs[0] = MxArray(obj->getDescriptorChannels());
217  else if (prop == "DescriptorSize")
218  plhs[0] = MxArray(obj->getDescriptorSize());
219  else if (prop == "DescriptorType")
220  plhs[0] = MxArray(AKAZEDescriptorTypeInv[obj->getDescriptorType()]);
221  else if (prop == "Diffusivity")
222  plhs[0] = MxArray(KAZEDiffusivityTypeInv[obj->getDiffusivity()]);
223  else if (prop == "NOctaveLayers")
224  plhs[0] = MxArray(obj->getNOctaveLayers());
225  else if (prop == "NOctaves")
226  plhs[0] = MxArray(obj->getNOctaves());
227  else if (prop == "Threshold")
228  plhs[0] = MxArray(obj->getThreshold());
229  else
230  mexErrMsgIdAndTxt("mexopencv:error",
231  "Unrecognized property %s", prop.c_str());
232  }
233  else if (method == "set") {
234  nargchk(nrhs==4 && nlhs==0);
235  string prop(rhs[2].toString());
236  if (prop == "DescriptorChannels")
237  obj->setDescriptorChannels(rhs[3].toInt());
238  else if (prop == "DescriptorSize")
239  obj->setDescriptorSize(rhs[3].toInt());
240  else if (prop == "DescriptorType")
241  obj->setDescriptorType(AKAZEDescriptorType[rhs[3].toString()]);
242  else if (prop == "Diffusivity")
243  obj->setDiffusivity(KAZEDiffusivityType[rhs[3].toString()]);
244  else if (prop == "NOctaveLayers")
245  obj->setNOctaveLayers(rhs[3].toInt());
246  else if (prop == "NOctaves")
247  obj->setNOctaves(rhs[3].toInt());
248  else if (prop == "Threshold")
249  obj->setThreshold(rhs[3].toDouble());
250  else
251  mexErrMsgIdAndTxt("mexopencv:error",
252  "Unrecognized property %s", prop.c_str());
253  }
254  else
255  mexErrMsgIdAndTxt("mexopencv:error",
256  "Unrecognized operation %s",method.c_str());
257 }
const ConstMap< std::string, int > KAZEDiffusivityType
KAZE Diffusivity type.
cv::Ptr< cv::AKAZE > createAKAZE(std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of AKAZE using options in arguments.
const ConstMap< std::string, int > AKAZEDescriptorType
AKAZE descriptor type.
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 > 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
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: AKAZE_.cpp:29
Global constant definitions.
const ConstMap< int, std::string > AKAZEDescriptorTypeInv
inverse AKAZE descriptor type