mexopencv  3.4.1
MEX interface for OpenCV library
AKAZE_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
10 #include "opencv2/features2d.hpp"
11 #include <typeinfo>
12 using namespace std;
13 using namespace cv;
14 
15 // Persistent objects
16 namespace {
18 int last_id = 0;
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>=2 && nlhs<=1);
43  obj_[++last_id] = createAKAZE(rhs.begin() + 2, rhs.end());
44  plhs[0] = MxArray(last_id);
45  mexLock();
46  return;
47  }
48 
49  // Big operation switch
50  Ptr<AKAZE> obj = obj_[id];
51  if (obj.empty())
52  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
53  if (method == "delete") {
54  nargchk(nrhs==2 && nlhs==0);
55  obj_.erase(id);
56  mexUnlock();
57  }
58  else if (method == "typeid") {
59  nargchk(nrhs==2 && nlhs<=1);
60  plhs[0] = MxArray(string(typeid(*obj).name()));
61  }
62  else if (method == "clear") {
63  nargchk(nrhs==2 && nlhs==0);
64  obj->clear();
65  }
66  else if (method == "load") {
67  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
68  string objname;
69  bool loadFromString = false;
70  for (int i=3; i<nrhs; i+=2) {
71  string key(rhs[i].toString());
72  if (key == "ObjName")
73  objname = rhs[i+1].toString();
74  else if (key == "FromString")
75  loadFromString = rhs[i+1].toBool();
76  else
77  mexErrMsgIdAndTxt("mexopencv:error",
78  "Unrecognized option %s", key.c_str());
79  }
80  obj_[id] = (loadFromString ?
81  Algorithm::loadFromString<AKAZE>(rhs[2].toString(), objname) :
82  Algorithm::load<AKAZE>(rhs[2].toString(), objname));
83  }
84  else if (method == "save") {
85  nargchk(nrhs==3 && nlhs==0);
86  obj->save(rhs[2].toString());
87  }
88  else if (method == "empty") {
89  nargchk(nrhs==2 && nlhs<=1);
90  plhs[0] = MxArray(obj->empty());
91  }
92  else if (method == "getDefaultName") {
93  nargchk(nrhs==2 && nlhs<=1);
94  plhs[0] = MxArray(obj->getDefaultName());
95  }
96  else if (method == "defaultNorm") {
97  nargchk(nrhs==2 && nlhs<=1);
98  plhs[0] = MxArray(NormTypeInv[obj->defaultNorm()]);
99  }
100  else if (method == "descriptorSize") {
101  nargchk(nrhs==2 && nlhs<=1);
102  plhs[0] = MxArray(obj->descriptorSize());
103  }
104  else if (method == "descriptorType") {
105  nargchk(nrhs==2 && nlhs<=1);
106  plhs[0] = MxArray(ClassNameInvMap[obj->descriptorType()]);
107  }
108  else if (method == "detect") {
109  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
110  if (rhs[2].isNumeric()) { // first variant that accepts an image
111  Mat mask;
112  for (int i=3; i<nrhs; i+=2) {
113  string key(rhs[i].toString());
114  if (key == "Mask")
115  mask = rhs[i+1].toMat(CV_8U);
116  else
117  mexErrMsgIdAndTxt("mexopencv:error",
118  "Unrecognized option %s", key.c_str());
119  }
120  Mat image(rhs[2].toMat(rhs[2].isUint8() ? CV_8U :
121  (rhs[2].isUint16() ? CV_16U : CV_32F)));
122  vector<KeyPoint> keypoints;
123  obj->detect(image, keypoints, mask);
124  plhs[0] = MxArray(keypoints);
125  }
126  else if (rhs[2].isCell()) { // second variant that accepts an image set
127  vector<Mat> masks;
128  for (int i=3; i<nrhs; i+=2) {
129  string key(rhs[i].toString());
130  if (key == "Mask") {
131  //masks = rhs[i+1].toVector<Mat>();
132  vector<MxArray> arr(rhs[i+1].toVector<MxArray>());
133  masks.clear();
134  masks.reserve(arr.size());
135  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
136  masks.push_back(it->toMat(CV_8U));
137  }
138  else
139  mexErrMsgIdAndTxt("mexopencv:error",
140  "Unrecognized option %s", key.c_str());
141  }
142  //vector<Mat> images(rhs[2].toVector<Mat>());
143  vector<Mat> images;
144  {
145  vector<MxArray> arr(rhs[2].toVector<MxArray>());
146  images.reserve(arr.size());
147  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
148  images.push_back(it->toMat(it->isUint8() ? CV_8U :
149  (it->isUint16() ? CV_16U : CV_32F)));
150  }
151  vector<vector<KeyPoint> > keypoints;
152  obj->detect(images, keypoints, masks);
153  plhs[0] = MxArray(keypoints);
154  }
155  else
156  mexErrMsgIdAndTxt("mexopencv:error", "Invalid arguments");
157  }
158  else if (method == "compute") {
159  nargchk(nrhs==4 && nlhs<=2);
160  if (rhs[2].isNumeric()) { // first variant that accepts an image
161  Mat image(rhs[2].toMat(rhs[2].isUint8() ? CV_8U :
162  (rhs[2].isUint16() ? CV_16U : CV_32F))),
163  descriptors;
164  vector<KeyPoint> keypoints(rhs[3].toVector<KeyPoint>());
165  obj->compute(image, keypoints, descriptors);
166  plhs[0] = MxArray(descriptors);
167  if (nlhs > 1)
168  plhs[1] = MxArray(keypoints);
169  }
170  else if (rhs[2].isCell()) { // second variant that accepts an image set
171  //vector<Mat> images(rhs[2].toVector<Mat>());
172  vector<Mat> images, descriptors;
173  {
174  vector<MxArray> arr(rhs[2].toVector<MxArray>());
175  images.reserve(arr.size());
176  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
177  images.push_back(it->toMat(it->isUint8() ? CV_8U :
178  (it->isUint16() ? CV_16U : CV_32F)));
179  }
180  vector<vector<KeyPoint> > keypoints(rhs[3].toVector(
181  const_mem_fun_ref_t<vector<KeyPoint>, MxArray>(
182  &MxArray::toVector<KeyPoint>)));
183  obj->compute(images, keypoints, descriptors);
184  plhs[0] = MxArray(descriptors);
185  if (nlhs > 1)
186  plhs[1] = MxArray(keypoints);
187  }
188  else
189  mexErrMsgIdAndTxt("mexopencv:error", "Invalid arguments");
190  }
191  else if (method == "detectAndCompute") {
192  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=2);
193  Mat mask;
194  vector<KeyPoint> keypoints;
195  bool useProvidedKeypoints = false;
196  for (int i=3; i<nrhs; i+=2) {
197  string key(rhs[i].toString());
198  if (key == "Mask")
199  mask = rhs[i+1].toMat(CV_8U);
200  else if (key == "Keypoints") {
201  keypoints = rhs[i+1].toVector<KeyPoint>();
202  useProvidedKeypoints = true;
203  }
204  else
205  mexErrMsgIdAndTxt("mexopencv:error",
206  "Unrecognized option %s", key.c_str());
207  }
208  Mat image(rhs[2].toMat(rhs[2].isUint8() ? CV_8U :
209  (rhs[2].isUint16() ? CV_16U : CV_32F))),
210  descriptors;
211  obj->detectAndCompute(image, mask, keypoints, descriptors,
212  useProvidedKeypoints);
213  plhs[0] = MxArray(keypoints);
214  if (nlhs > 1)
215  plhs[1] = MxArray(descriptors);
216  }
217  else if (method == "get") {
218  nargchk(nrhs==3 && nlhs<=1);
219  string prop(rhs[2].toString());
220  if (prop == "DescriptorChannels")
221  plhs[0] = MxArray(obj->getDescriptorChannels());
222  else if (prop == "DescriptorSize")
223  plhs[0] = MxArray(obj->getDescriptorSize());
224  else if (prop == "DescriptorType")
226  else if (prop == "Diffusivity")
227  plhs[0] = MxArray(KAZEDiffusivityTypeInv[obj->getDiffusivity()]);
228  else if (prop == "NOctaveLayers")
229  plhs[0] = MxArray(obj->getNOctaveLayers());
230  else if (prop == "NOctaves")
231  plhs[0] = MxArray(obj->getNOctaves());
232  else if (prop == "Threshold")
233  plhs[0] = MxArray(obj->getThreshold());
234  else
235  mexErrMsgIdAndTxt("mexopencv:error",
236  "Unrecognized property %s", prop.c_str());
237  }
238  else if (method == "set") {
239  nargchk(nrhs==4 && nlhs==0);
240  string prop(rhs[2].toString());
241  if (prop == "DescriptorChannels")
242  obj->setDescriptorChannels(rhs[3].toInt());
243  else if (prop == "DescriptorSize")
244  obj->setDescriptorSize(rhs[3].toInt());
245  else if (prop == "DescriptorType")
246  obj->setDescriptorType(AKAZEDescriptorType[rhs[3].toString()]);
247  else if (prop == "Diffusivity")
248  obj->setDiffusivity(KAZEDiffusivityType[rhs[3].toString()]);
249  else if (prop == "NOctaveLayers")
250  obj->setNOctaveLayers(rhs[3].toInt());
251  else if (prop == "NOctaves")
252  obj->setNOctaves(rhs[3].toInt());
253  else if (prop == "Threshold")
254  obj->setThreshold(rhs[3].toDouble());
255  else
256  mexErrMsgIdAndTxt("mexopencv:error",
257  "Unrecognized property %s", prop.c_str());
258  }
259  else
260  mexErrMsgIdAndTxt("mexopencv:error",
261  "Unrecognized operation %s",method.c_str());
262 }
virtual void setNOctaves(int octaves)=0
virtual int getDescriptorType() const=0
const ConstMap< std::string, int > KAZEDiffusivityType
KAZE Diffusivity type.
virtual void setDescriptorSize(int dsize)=0
virtual int descriptorType() const
virtual int getDiffusivity() const=0
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
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.
#define CV_8U
virtual int getNOctaves() const=0
virtual int getNOctaveLayers() const=0
STL namespace.
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:160
virtual void setNOctaveLayers(int octaveLayers)=0
virtual void detect(InputArray image, std::vector< KeyPoint > &keypoints, InputArray mask=noArray())
T end(T... args)
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
map< int, Ptr< AKAZE > > obj_
Object container.
Definition: AKAZE_.cpp:20
STL class.
virtual void setDiffusivity(int diff)=0
T push_back(T... args)
virtual int defaultNorm() const
virtual void clear()
#define CV_32F
LIBMWMEX_API_EXTERN_C void mexErrMsgIdAndTxt(const char *identifier, const char *err_msg,...)
Issue formatted error message with corresponding error identifier and return to MATLAB prompt...
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
virtual void setDescriptorChannels(int dch)=0
T clear(T... args)
virtual int descriptorSize() const
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/output arguments number check.
Definition: mexopencv.hpp:181
Common definitions for the features2d and xfeatures2d modules.
#define CV_16U
T size(T... args)
const ConstMap< int, std::string > KAZEDiffusivityTypeInv
inverse KAZE Diffusivity type
virtual void detectAndCompute(InputArray image, InputArray mask, std::vector< KeyPoint > &keypoints, OutputArray descriptors, bool useProvidedKeypoints=false)
const ConstMap< int, std::string > ClassNameInvMap
Translates data type definition used in OpenCV to that of MATLAB.
Definition: mexopencv.hpp:42
STL class.
bool empty() const
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: AKAZE_.cpp:30
virtual int getDescriptorSize() const=0
Global constant definitions.
virtual void setThreshold(double threshold)=0
T begin(T... args)
int last_id
Last object id to allocate.
Definition: AKAZE_.cpp:18
T c_str(T... args)
const ConstMap< int, std::string > AKAZEDescriptorTypeInv
inverse AKAZE descriptor type
virtual void compute(InputArray image, std::vector< KeyPoint > &keypoints, OutputArray descriptors)
virtual double getThreshold() const=0
virtual bool empty() const
virtual void save(const String &filename) const
virtual void setDescriptorType(int dtype)=0
virtual int getDescriptorChannels() const=0
virtual String getDefaultName() const
cv::Mat toMat() const
T reserve(T... args)