mexopencv  3.4.1
MEX interface for OpenCV library
ORB_.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] = createORB(rhs.begin() + 2, rhs.end());
44  plhs[0] = MxArray(last_id);
45  mexLock();
46  return;
47  }
48 
49  // Big operation switch
50  Ptr<ORB> 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<ORB>(rhs[2].toString(), objname) :
82  Algorithm::load<ORB>(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(CV_8U));
121  vector<KeyPoint> keypoints;
122  obj->detect(image, keypoints, mask);
123  plhs[0] = MxArray(keypoints);
124  }
125  else if (rhs[2].isCell()) { // second variant that accepts an image set
126  vector<Mat> masks;
127  for (int i=3; i<nrhs; i+=2) {
128  string key(rhs[i].toString());
129  if (key == "Mask") {
130  //masks = rhs[i+1].toVector<Mat>();
131  vector<MxArray> arr(rhs[i+1].toVector<MxArray>());
132  masks.clear();
133  masks.reserve(arr.size());
134  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
135  masks.push_back(it->toMat(CV_8U));
136  }
137  else
138  mexErrMsgIdAndTxt("mexopencv:error",
139  "Unrecognized option %s", key.c_str());
140  }
141  //vector<Mat> images(rhs[2].toVector<Mat>());
142  vector<Mat> images;
143  {
144  vector<MxArray> arr(rhs[2].toVector<MxArray>());
145  images.reserve(arr.size());
146  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
147  images.push_back(it->toMat(CV_8U));
148  }
149  vector<vector<KeyPoint> > keypoints;
150  obj->detect(images, keypoints, masks);
151  plhs[0] = MxArray(keypoints);
152  }
153  else
154  mexErrMsgIdAndTxt("mexopencv:error", "Invalid arguments");
155  }
156  else if (method == "compute") {
157  nargchk(nrhs==4 && nlhs<=2);
158  if (rhs[2].isNumeric()) { // first variant that accepts an image
159  Mat image(rhs[2].toMat(CV_8U)), descriptors;
160  vector<KeyPoint> keypoints(rhs[3].toVector<KeyPoint>());
161  obj->compute(image, keypoints, descriptors);
162  plhs[0] = MxArray(descriptors);
163  if (nlhs > 1)
164  plhs[1] = MxArray(keypoints);
165  }
166  else if (rhs[2].isCell()) { // second variant that accepts an image set
167  //vector<Mat> images(rhs[2].toVector<Mat>());
168  vector<Mat> images, descriptors;
169  {
170  vector<MxArray> arr(rhs[2].toVector<MxArray>());
171  images.reserve(arr.size());
172  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
173  images.push_back(it->toMat(CV_8U));
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(CV_8U)), descriptors;
204  obj->detectAndCompute(image, mask, keypoints, descriptors,
205  useProvidedKeypoints);
206  plhs[0] = MxArray(keypoints);
207  if (nlhs > 1)
208  plhs[1] = MxArray(descriptors);
209  }
210  else if (method == "get") {
211  nargchk(nrhs==3 && nlhs<=1);
212  string prop(rhs[2].toString());
213  if (prop == "EdgeThreshold")
214  plhs[0] = MxArray(obj->getEdgeThreshold());
215  else if (prop == "FastThreshold")
216  plhs[0] = MxArray(obj->getFastThreshold());
217  else if (prop == "FirstLevel")
218  plhs[0] = MxArray(obj->getFirstLevel());
219  else if (prop == "MaxFeatures")
220  plhs[0] = MxArray(obj->getMaxFeatures());
221  else if (prop == "NLevels")
222  plhs[0] = MxArray(obj->getNLevels());
223  else if (prop == "PatchSize")
224  plhs[0] = MxArray(obj->getPatchSize());
225  else if (prop == "ScaleFactor")
226  plhs[0] = MxArray(obj->getScaleFactor());
227  else if (prop == "ScoreType")
228  plhs[0] = MxArray(ORBScoreTypeInv[obj->getScoreType()]);
229  else if (prop == "WTA_K")
230  plhs[0] = MxArray(obj->getWTA_K());
231  else
232  mexErrMsgIdAndTxt("mexopencv:error",
233  "Unrecognized property %s", prop.c_str());
234  }
235  else if (method == "set") {
236  nargchk(nrhs==4 && nlhs==0);
237  string prop(rhs[2].toString());
238  if (prop == "EdgeThreshold")
239  obj->setEdgeThreshold(rhs[3].toInt());
240  else if (prop == "FastThreshold")
241  obj->setFastThreshold(rhs[3].toInt());
242  else if (prop == "FirstLevel")
243  obj->setFirstLevel(rhs[3].toInt());
244  else if (prop == "MaxFeatures")
245  obj->setMaxFeatures(rhs[3].toInt());
246  else if (prop == "NLevels")
247  obj->setNLevels(rhs[3].toInt());
248  else if (prop == "PatchSize")
249  obj->setPatchSize(rhs[3].toInt());
250  else if (prop == "ScaleFactor")
251  obj->setScaleFactor(rhs[3].toDouble());
252  else if (prop == "ScoreType")
253  obj->setScoreType(ORBScoreType[rhs[3].toString()]);
254  else if (prop == "WTA_K")
255  obj->setWTA_K(rhs[3].toInt());
256  else
257  mexErrMsgIdAndTxt("mexopencv:error",
258  "Unrecognized property %s", prop.c_str());
259  }
260  else
261  mexErrMsgIdAndTxt("mexopencv:error",
262  "Unrecognized operation %s",method.c_str());
263 }
virtual void setPatchSize(int patchSize)=0
virtual int descriptorType() const
map< int, Ptr< ORB > > obj_
Object container.
Definition: ORB_.cpp:20
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
const ConstMap< int, std::string > ORBScoreTypeInv
inverse ORB score types
virtual void setFirstLevel(int firstLevel)=0
#define CV_8U
virtual void setFastThreshold(int fastThreshold)=0
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 setMaxFeatures(int maxFeatures)=0
virtual void setNLevels(int nlevels)=0
virtual int getNLevels() const=0
virtual int getWTA_K() const=0
T push_back(T... args)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: ORB_.cpp:30
virtual int defaultNorm() const
virtual void clear()
cv::Ptr< cv::ORB > createORB(std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of ORB using options in arguments.
virtual void setWTA_K(int wta_k)=0
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...
virtual int getPatchSize() const=0
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.
virtual void setScoreType(int scoreType)=0
virtual int getEdgeThreshold() const=0
virtual void setScaleFactor(double scaleFactor)=0
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 int getMaxFeatures() const=0
Global constant definitions.
T begin(T... args)
T c_str(T... args)
virtual double getScaleFactor() const=0
virtual int getScoreType() const=0
virtual void compute(InputArray image, std::vector< KeyPoint > &keypoints, OutputArray descriptors)
virtual void setEdgeThreshold(int edgeThreshold)=0
virtual int getFastThreshold() const=0
virtual bool empty() const
virtual int getFirstLevel() const=0
virtual void save(const String &filename) const
int last_id
Last object id to allocate.
Definition: ORB_.cpp:18
virtual String getDefaultName() const
cv::Mat toMat() const
T reserve(T... args)
const ConstMap< std::string, int > ORBScoreType
ORB score types.