mexopencv  3.4.1
MEX interface for OpenCV library
FastFeatureDetector_.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<=1);
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);
44  rhs.begin() + 2, rhs.end());
45  plhs[0] = MxArray(last_id);
46  mexLock();
47  return;
48  }
49 
50  // Big operation switch
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<FastFeatureDetector>(rhs[2].toString(), objname) :
83  Algorithm::load<FastFeatureDetector>(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 == "detect") {
98  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
99  if (rhs[2].isNumeric()) { // first variant that accepts an image
100  Mat mask;
101  for (int i=3; i<nrhs; i+=2) {
102  string key(rhs[i].toString());
103  if (key == "Mask")
104  mask = rhs[i+1].toMat(CV_8U);
105  else
106  mexErrMsgIdAndTxt("mexopencv:error",
107  "Unrecognized option %s", key.c_str());
108  }
109  Mat image(rhs[2].toMat(CV_8U));
110  vector<KeyPoint> keypoints;
111  obj->detect(image, keypoints, mask);
112  plhs[0] = MxArray(keypoints);
113  }
114  else if (rhs[2].isCell()) { // second variant that accepts an image set
115  vector<Mat> masks;
116  for (int i=3; i<nrhs; i+=2) {
117  string key(rhs[i].toString());
118  if (key == "Mask") {
119  //masks = rhs[i+1].toVector<Mat>();
120  vector<MxArray> arr(rhs[i+1].toVector<MxArray>());
121  masks.clear();
122  masks.reserve(arr.size());
123  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
124  masks.push_back(it->toMat(CV_8U));
125  }
126  else
127  mexErrMsgIdAndTxt("mexopencv:error",
128  "Unrecognized option %s", key.c_str());
129  }
130  //vector<Mat> images(rhs[2].toVector<Mat>());
131  vector<Mat> images;
132  {
133  vector<MxArray> arr(rhs[2].toVector<MxArray>());
134  images.reserve(arr.size());
135  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
136  images.push_back(it->toMat(CV_8U));
137  }
138  vector<vector<KeyPoint> > keypoints;
139  obj->detect(images, keypoints, masks);
140  plhs[0] = MxArray(keypoints);
141  }
142  else
143  mexErrMsgIdAndTxt("mexopencv:error", "Invalid arguments");
144  }
145  else if (method == "get") {
146  nargchk(nrhs==3 && nlhs<=1);
147  string prop(rhs[2].toString());
148  if (prop == "NonmaxSuppression")
149  plhs[0] = MxArray(obj->getNonmaxSuppression());
150  else if (prop == "Threshold")
151  plhs[0] = MxArray(obj->getThreshold());
152  else if (prop == "Type")
153  plhs[0] = MxArray(FASTTypeMapInv[obj->getType()]);
154  else
155  mexErrMsgIdAndTxt("mexopencv:error",
156  "Unrecognized property %s", prop.c_str());
157  }
158  else if (method == "set") {
159  nargchk(nrhs==4 && nlhs==0);
160  string prop(rhs[2].toString());
161  if (prop == "NonmaxSuppression")
162  obj->setNonmaxSuppression(rhs[3].toBool());
163  else if (prop == "Threshold")
164  obj->setThreshold(rhs[3].toInt());
165  else if (prop == "Type")
166  obj->setType(FASTTypeMap[rhs[3].toString()]);
167  else
168  mexErrMsgIdAndTxt("mexopencv:error",
169  "Unrecognized property %s", prop.c_str());
170  }
171  //else if (method == "defaultNorm")
172  //else if (method == "descriptorSize")
173  //else if (method == "descriptorType")
174  //else if (method == "compute")
175  //else if (method == "detectAndCompute")
176  else
177  mexErrMsgIdAndTxt("mexopencv:error",
178  "Unrecognized operation %s",method.c_str());
179 }
virtual void setNonmaxSuppression(bool f)=0
virtual void setType(int type)=0
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
virtual int getThreshold() const=0
#define CV_8U
STL namespace.
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.
cv::Ptr< cv::FastFeatureDetector > createFastFeatureDetector(std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of FastFeatureDetector using options in arguments.
virtual int getType() const=0
virtual bool getNonmaxSuppression() const=0
T push_back(T... args)
virtual void clear()
virtual String getDefaultName() const
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.
T size(T... args)
STL class.
bool empty() const
Global constant definitions.
T begin(T... args)
virtual void setThreshold(int threshold)=0
T c_str(T... args)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
map< int, Ptr< FastFeatureDetector > > obj_
Object container.
virtual bool empty() const
virtual void save(const String &filename) const
const ConstMap< int, std::string > FASTTypeMapInv
inverse FAST types
cv::Mat toMat() const
T reserve(T... args)
const ConstMap< std::string, int > FASTTypeMap
FAST types.