mexopencv  3.4.1
MEX interface for OpenCV library
FeatureDetector_.cpp
Go to the documentation of this file.
1 
9 #include "mexopencv.hpp"
10 #include "mexopencv_features2d.hpp"
11 #include "opencv2/features2d.hpp"
12 #include <typeinfo>
13 using namespace std;
14 using namespace cv;
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<=1);
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>=3 && nlhs<=1);
45  rhs[2].toString(), rhs.begin() + 3, rhs.end());
46  plhs[0] = MxArray(last_id);
47  mexLock();
48  return;
49  }
50 
51  // Big operation switch
52  Ptr<FeatureDetector> obj = obj_[id];
53  if (obj.empty())
54  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
55  if (method == "delete") {
56  nargchk(nrhs==2 && nlhs==0);
57  obj_.erase(id);
58  mexUnlock();
59  }
60  else if (method == "typeid") {
61  nargchk(nrhs==2 && nlhs<=1);
62  plhs[0] = MxArray(string(typeid(*obj).name()));
63  }
64  else if (method == "clear") {
65  nargchk(nrhs==2 && nlhs==0);
66  obj->clear();
67  }
68  else if (method == "load") {
69  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
70  string objname;
71  bool loadFromString = false;
72  for (int i=3; i<nrhs; i+=2) {
73  string key(rhs[i].toString());
74  if (key == "ObjName")
75  objname = rhs[i+1].toString();
76  else if (key == "FromString")
77  loadFromString = rhs[i+1].toBool();
78  else
79  mexErrMsgIdAndTxt("mexopencv:error",
80  "Unrecognized option %s", key.c_str());
81  }
82  /*
83  obj_[id] = (loadFromString ?
84  Algorithm::loadFromString<FeatureDetector>(rhs[2].toString(), objname) :
85  Algorithm::load<FeatureDetector>(rhs[2].toString(), objname));
86  */
88  // HACK: workaround for missing FeatureDetector::create()
89  FileStorage fs(rhs[2].toString(), FileStorage::READ +
90  (loadFromString ? FileStorage::MEMORY : 0));
91  if (!fs.isOpened())
92  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
93  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
94  if (fn.empty())
95  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
96  obj->read(fn);
97  //*/
98  }
99  else if (method == "save") {
100  //TODO: crashes due to bug in opencv
101  // (getDefaultName contains "." which is not allowed in serialized mapping name)
102  nargchk(nrhs==3 && nlhs==0);
103  obj->save(rhs[2].toString());
104  }
105  else if (method == "empty") {
106  nargchk(nrhs==2 && nlhs<=1);
107  plhs[0] = MxArray(obj->empty());
108  }
109  else if (method == "getDefaultName") {
110  nargchk(nrhs==2 && nlhs<=1);
111  plhs[0] = MxArray(obj->getDefaultName());
112  }
113  else if (method == "detect") {
114  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
115  if (rhs[2].isNumeric()) { // first variant that accepts an image
116  Mat mask;
117  for (int i=3; i<nrhs; i+=2) {
118  string key(rhs[i].toString());
119  if (key == "Mask")
120  mask = rhs[i+1].toMat(CV_8U);
121  else
122  mexErrMsgIdAndTxt("mexopencv:error",
123  "Unrecognized option %s", key.c_str());
124  }
125  Mat image(rhs[2].toMat(CV_8U));
126  vector<KeyPoint> keypoints;
127  obj->detect(image, keypoints, mask);
128  plhs[0] = MxArray(keypoints);
129  }
130  else if (rhs[2].isCell()) { // second variant that accepts an image set
131  vector<Mat> masks;
132  for (int i=3; i<nrhs; i+=2) {
133  string key(rhs[i].toString());
134  if (key == "Mask") {
135  //masks = rhs[i+1].toVector<Mat>();
136  vector<MxArray> arr(rhs[i+1].toVector<MxArray>());
137  masks.clear();
138  masks.reserve(arr.size());
139  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
140  masks.push_back(it->toMat(CV_8U));
141  }
142  else
143  mexErrMsgIdAndTxt("mexopencv:error",
144  "Unrecognized option %s", key.c_str());
145  }
146  //vector<Mat> images(rhs[2].toVector<Mat>());
147  vector<Mat> images;
148  {
149  vector<MxArray> arr(rhs[2].toVector<MxArray>());
150  images.reserve(arr.size());
151  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
152  images.push_back(it->toMat(CV_8U));
153  }
154  vector<vector<KeyPoint> > keypoints;
155  obj->detect(images, keypoints, masks);
156  plhs[0] = MxArray(keypoints);
157  }
158  else
159  mexErrMsgIdAndTxt("mexopencv:error", "Invalid arguments");
160  }
161  //else if (method == "defaultNorm")
162  //else if (method == "descriptorSize")
163  //else if (method == "descriptorType")
164  //else if (method == "compute")
165  //else if (method == "detectAndCompute")
166  else
167  mexErrMsgIdAndTxt("mexopencv:error",
168  "Unrecognized operation %s",method.c_str());
169 }
T empty(T... args)
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
#define CV_8U
STL namespace.
void read(const String &fileName)
virtual void detect(InputArray image, std::vector< KeyPoint > &keypoints, InputArray mask=noArray())
T end(T... args)
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
int last_id
Last object id to allocate.
T push_back(T... args)
virtual void clear()
cv::Ptr< cv::FeatureDetector > createFeatureDetector(const std::string &type, std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Factory function for FeatureDetector creation.
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)
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
FileNode getFirstTopLevelNode() const
T size(T... args)
STL class.
bool empty() const
Global constant definitions.
T begin(T... args)
T c_str(T... args)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
virtual bool empty() const
virtual void save(const String &filename) const
map< int, Ptr< FeatureDetector > > obj_
Object container.
cv::Mat toMat() const
T reserve(T... args)