mexopencv  3.4.1
MEX interface for OpenCV library
KAZE_.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] = createKAZE(rhs.begin() + 2, rhs.end());
44  plhs[0] = MxArray(last_id);
45  mexLock();
46  return;
47  }
48 
49  // Big operation switch
50  Ptr<KAZE> 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<KAZE>(rhs[2].toString(), objname) :
82  Algorithm::load<KAZE>(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 == "Diffusivity")
221  plhs[0] = MxArray(KAZEDiffusivityTypeInv[obj->getDiffusivity()]);
222  else if (prop == "Extended")
223  plhs[0] = MxArray(obj->getExtended());
224  else if (prop == "NOctaveLayers")
225  plhs[0] = MxArray(obj->getNOctaveLayers());
226  else if (prop == "NOctaves")
227  plhs[0] = MxArray(obj->getNOctaves());
228  else if (prop == "Threshold")
229  plhs[0] = MxArray(obj->getThreshold());
230  else if (prop == "Upright")
231  plhs[0] = MxArray(obj->getUpright());
232  else
233  mexErrMsgIdAndTxt("mexopencv:error",
234  "Unrecognized property %s", prop.c_str());
235  }
236  else if (method == "set") {
237  nargchk(nrhs==4 && nlhs==0);
238  string prop(rhs[2].toString());
239  if (prop == "Diffusivity")
240  obj->setDiffusivity(KAZEDiffusivityType[rhs[3].toString()]);
241  else if (prop == "Extended")
242  obj->setExtended(rhs[3].toBool());
243  else if (prop == "NOctaveLayers")
244  obj->setNOctaveLayers(rhs[3].toInt());
245  else if (prop == "NOctaves")
246  obj->setNOctaves(rhs[3].toInt());
247  else if (prop == "Threshold")
248  obj->setThreshold(rhs[3].toDouble());
249  else if (prop == "Upright")
250  obj->setUpright(rhs[3].toBool());
251  else
252  mexErrMsgIdAndTxt("mexopencv:error",
253  "Unrecognized property %s", prop.c_str());
254  }
255  else
256  mexErrMsgIdAndTxt("mexopencv:error",
257  "Unrecognized operation %s",method.c_str());
258 }
virtual void setUpright(bool upright)=0
const ConstMap< std::string, int > KAZEDiffusivityType
KAZE Diffusivity type.
virtual int descriptorType() const
int last_id
Last object id to allocate.
Definition: KAZE_.cpp:18
virtual String getDefaultName() const
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
virtual bool getUpright() const=0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: KAZE_.cpp:30
#define CV_8U
STL namespace.
const ConstMap< int, std::string > NormTypeInv
Inverse norm type map for option processing.
Definition: mexopencv.hpp:160
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
STL class.
virtual void setDiffusivity(int diff)=0
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.
virtual int getNOctaves() const=0
virtual void setNOctaves(int octaves)=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.
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
map< int, Ptr< KAZE > > obj_
Object container.
Definition: KAZE_.cpp:20
T size(T... args)
virtual int getNOctaveLayers() const=0
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
virtual double getThreshold() const=0
Global constant definitions.
T begin(T... args)
virtual int getDiffusivity() const=0
T c_str(T... args)
virtual void setExtended(bool extended)=0
virtual void compute(InputArray image, std::vector< KeyPoint > &keypoints, OutputArray descriptors)
virtual void setNOctaveLayers(int octaveLayers)=0
virtual bool empty() const
virtual void save(const String &filename) const
virtual void setThreshold(double threshold)=0
cv::Mat toMat() const
T reserve(T... args)
virtual bool getExtended() const=0