mexopencv  3.4.1
MEX interface for OpenCV library
SURF_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
10 #include "opencv2/xfeatures2d.hpp"
11 #include <typeinfo>
12 using namespace std;
13 using namespace cv;
14 using namespace cv::xfeatures2d;
15 
16 // Persistent objects
17 namespace {
19 int last_id = 0;
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] = createSURF(rhs.begin() + 2, rhs.end());
45  plhs[0] = MxArray(last_id);
46  mexLock();
47  return;
48  }
49 
50  // Big operation switch
51  Ptr<SURF> obj = obj_[id];
52  if (obj.empty())
53  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
54  if (method == "delete") {
55  nargchk(nrhs==2 && nlhs==0);
56  obj_.erase(id);
57  mexUnlock();
58  }
59  else if (method == "typeid") {
60  nargchk(nrhs==2 && nlhs<=1);
61  plhs[0] = MxArray(string(typeid(*obj).name()));
62  }
63  else if (method == "clear") {
64  nargchk(nrhs==2 && nlhs==0);
65  obj->clear();
66  }
67  else if (method == "load") {
68  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
69  string objname;
70  bool loadFromString = false;
71  for (int i=3; i<nrhs; i+=2) {
72  string key(rhs[i].toString());
73  if (key == "ObjName")
74  objname = rhs[i+1].toString();
75  else if (key == "FromString")
76  loadFromString = rhs[i+1].toBool();
77  else
78  mexErrMsgIdAndTxt("mexopencv:error",
79  "Unrecognized option %s", key.c_str());
80  }
81  obj_[id] = (loadFromString ?
82  Algorithm::loadFromString<SURF>(rhs[2].toString(), objname) :
83  Algorithm::load<SURF>(rhs[2].toString(), objname));
84  }
85  else if (method == "save") {
86  nargchk(nrhs==3 && nlhs==0);
87  obj->save(rhs[2].toString());
88  }
89  else if (method == "empty") {
90  nargchk(nrhs==2 && nlhs<=1);
91  plhs[0] = MxArray(obj->empty());
92  }
93  else if (method == "getDefaultName") {
94  nargchk(nrhs==2 && nlhs<=1);
95  plhs[0] = MxArray(obj->getDefaultName());
96  }
97  else if (method == "defaultNorm") {
98  nargchk(nrhs==2 && nlhs<=1);
99  plhs[0] = MxArray(NormTypeInv[obj->defaultNorm()]);
100  }
101  else if (method == "descriptorSize") {
102  nargchk(nrhs==2 && nlhs<=1);
103  plhs[0] = MxArray(obj->descriptorSize());
104  }
105  else if (method == "descriptorType") {
106  nargchk(nrhs==2 && nlhs<=1);
107  plhs[0] = MxArray(ClassNameInvMap[obj->descriptorType()]);
108  }
109  else if (method == "detect") {
110  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
111  if (rhs[2].isNumeric()) { // first variant that accepts an image
112  Mat mask;
113  for (int i=3; i<nrhs; i+=2) {
114  string key(rhs[i].toString());
115  if (key == "Mask")
116  mask = rhs[i+1].toMat(CV_8U);
117  else
118  mexErrMsgIdAndTxt("mexopencv:error",
119  "Unrecognized option %s", key.c_str());
120  }
121  Mat image(rhs[2].toMat(CV_8U));
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(CV_8U));
149  }
150  vector<vector<KeyPoint> > keypoints;
151  obj->detect(images, keypoints, masks);
152  plhs[0] = MxArray(keypoints);
153  }
154  else
155  mexErrMsgIdAndTxt("mexopencv:error", "Invalid arguments");
156  }
157  else if (method == "compute") {
158  nargchk(nrhs==4 && nlhs<=2);
159  if (rhs[2].isNumeric()) { // first variant that accepts an image
160  Mat image(rhs[2].toMat(CV_8U)), descriptors;
161  vector<KeyPoint> keypoints(rhs[3].toVector<KeyPoint>());
162  obj->compute(image, keypoints, descriptors);
163  plhs[0] = MxArray(descriptors);
164  if (nlhs > 1)
165  plhs[1] = MxArray(keypoints);
166  }
167  else if (rhs[2].isCell()) { // second variant that accepts an image set
168  //vector<Mat> images(rhs[2].toVector<Mat>());
169  vector<Mat> images, descriptors;
170  {
171  vector<MxArray> arr(rhs[2].toVector<MxArray>());
172  images.reserve(arr.size());
173  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
174  images.push_back(it->toMat(CV_8U));
175  }
176  vector<vector<KeyPoint> > keypoints(rhs[3].toVector(
177  const_mem_fun_ref_t<vector<KeyPoint>, MxArray>(
178  &MxArray::toVector<KeyPoint>)));
179  obj->compute(images, keypoints, descriptors);
180  plhs[0] = MxArray(descriptors);
181  if (nlhs > 1)
182  plhs[1] = MxArray(keypoints);
183  }
184  else
185  mexErrMsgIdAndTxt("mexopencv:error", "Invalid arguments");
186  }
187  else if (method == "detectAndCompute") {
188  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=2);
189  Mat mask;
190  vector<KeyPoint> keypoints;
191  bool useProvidedKeypoints = false;
192  for (int i=3; i<nrhs; i+=2) {
193  string key(rhs[i].toString());
194  if (key == "Mask")
195  mask = rhs[i+1].toMat(CV_8U);
196  else if (key == "Keypoints") {
197  keypoints = rhs[i+1].toVector<KeyPoint>();
198  useProvidedKeypoints = true;
199  }
200  else
201  mexErrMsgIdAndTxt("mexopencv:error",
202  "Unrecognized option %s", key.c_str());
203  }
204  Mat image(rhs[2].toMat(CV_8U)), descriptors;
205  obj->detectAndCompute(image, mask, keypoints, descriptors,
206  useProvidedKeypoints);
207  plhs[0] = MxArray(keypoints);
208  if (nlhs > 1)
209  plhs[1] = MxArray(descriptors);
210  }
211  else if (method == "get") {
212  nargchk(nrhs==3 && nlhs<=1);
213  string prop(rhs[2].toString());
214  if (prop == "Extended")
215  plhs[0] = MxArray(obj->getExtended());
216  else if (prop == "HessianThreshold")
217  plhs[0] = MxArray(obj->getHessianThreshold());
218  else if (prop == "NOctaveLayers")
219  plhs[0] = MxArray(obj->getNOctaveLayers());
220  else if (prop == "NOctaves")
221  plhs[0] = MxArray(obj->getNOctaves());
222  else if (prop == "Upright")
223  plhs[0] = MxArray(obj->getUpright());
224  else
225  mexErrMsgIdAndTxt("mexopencv:error",
226  "Unrecognized property %s", prop.c_str());
227  }
228  else if (method == "set") {
229  nargchk(nrhs==4 && nlhs==0);
230  string prop(rhs[2].toString());
231  if (prop == "Extended")
232  obj->setExtended(rhs[3].toBool());
233  else if (prop == "HessianThreshold")
234  obj->setHessianThreshold(rhs[3].toDouble());
235  else if (prop == "NOctaveLayers")
236  obj->setNOctaveLayers(rhs[3].toInt());
237  else if (prop == "NOctaves")
238  obj->setNOctaves(rhs[3].toInt());
239  else if (prop == "Upright")
240  obj->setUpright(rhs[3].toBool());
241  else
242  mexErrMsgIdAndTxt("mexopencv:error",
243  "Unrecognized property %s", prop.c_str());
244  }
245  else
246  mexErrMsgIdAndTxt("mexopencv:error",
247  "Unrecognized operation %s",method.c_str());
248 }
virtual void setHessianThreshold(double hessianThreshold)=0
virtual int descriptorType() const
virtual void setExtended(bool extended)=0
virtual bool getUpright() const=0
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
#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)
virtual bool getExtended() const=0
cv::Ptr< cv::xfeatures2d::SURF > createSURF(std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of SURF using options in arguments.
virtual int getNOctaves() const=0
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
int last_id
Last object id to allocate.
Definition: SURF_.cpp:19
STL class.
virtual double getHessianThreshold() const=0
virtual void setNOctaveLayers(int nOctaveLayers)=0
T push_back(T... args)
virtual int defaultNorm() const
virtual void clear()
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...
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: SURF_.cpp:31
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
map< int, Ptr< SURF > > obj_
Object container.
Definition: SURF_.cpp:21
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.
virtual String getDefaultName() const
T size(T... args)
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 void setNOctaves(int nOctaves)=0
Global constant definitions.
T begin(T... args)
T c_str(T... args)
virtual void compute(InputArray image, std::vector< KeyPoint > &keypoints, OutputArray descriptors)
virtual bool empty() const
virtual void save(const String &filename) const
virtual int getNOctaveLayers() const=0
cv::Mat toMat() const
T reserve(T... args)
virtual void setUpright(bool upright)=0