mexopencv  3.4.1
MEX interface for OpenCV library
GFTTDetector_.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);
43  obj_[++last_id] = createGFTTDetector(rhs.begin() + 2, rhs.end());
44  plhs[0] = MxArray(last_id);
45  mexLock();
46  return;
47  }
48 
49  // Big operation switch
50  Ptr<GFTTDetector> 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<GFTTDetector>(rhs[2].toString(), objname) :
82  Algorithm::load<GFTTDetector>(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 == "detect") {
97  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
98  if (rhs[2].isNumeric()) { // first variant that accepts an image
99  Mat mask;
100  for (int i=3; i<nrhs; i+=2) {
101  string key(rhs[i].toString());
102  if (key == "Mask")
103  mask = rhs[i+1].toMat(CV_8U);
104  else
105  mexErrMsgIdAndTxt("mexopencv:error",
106  "Unrecognized option %s", key.c_str());
107  }
108  Mat image(rhs[2].toMat(CV_8U));
109  vector<KeyPoint> keypoints;
110  obj->detect(image, keypoints, mask);
111  plhs[0] = MxArray(keypoints);
112  }
113  else if (rhs[2].isCell()) { // second variant that accepts an image set
114  vector<Mat> masks;
115  for (int i=3; i<nrhs; i+=2) {
116  string key(rhs[i].toString());
117  if (key == "Mask") {
118  //masks = rhs[i+1].toVector<Mat>();
119  vector<MxArray> arr(rhs[i+1].toVector<MxArray>());
120  masks.clear();
121  masks.reserve(arr.size());
122  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
123  masks.push_back(it->toMat(CV_8U));
124  }
125  else
126  mexErrMsgIdAndTxt("mexopencv:error",
127  "Unrecognized option %s", key.c_str());
128  }
129  //vector<Mat> images(rhs[2].toVector<Mat>());
130  vector<Mat> images;
131  {
132  vector<MxArray> arr(rhs[2].toVector<MxArray>());
133  images.reserve(arr.size());
134  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
135  images.push_back(it->toMat(CV_8U));
136  }
137  vector<vector<KeyPoint> > keypoints;
138  obj->detect(images, keypoints, masks);
139  plhs[0] = MxArray(keypoints);
140  }
141  else
142  mexErrMsgIdAndTxt("mexopencv:error", "Invalid arguments");
143  }
144  else if (method == "get") {
145  nargchk(nrhs==3 && nlhs<=1);
146  string prop(rhs[2].toString());
147  if (prop == "BlockSize")
148  plhs[0] = MxArray(obj->getBlockSize());
149  else if (prop == "HarrisDetector")
150  plhs[0] = MxArray(obj->getHarrisDetector());
151  else if (prop == "K")
152  plhs[0] = MxArray(obj->getK());
153  else if (prop == "MaxFeatures")
154  plhs[0] = MxArray(obj->getMaxFeatures());
155  else if (prop == "MinDistance")
156  plhs[0] = MxArray(obj->getMinDistance());
157  else if (prop == "QualityLevel")
158  plhs[0] = MxArray(obj->getQualityLevel());
159  else
160  mexErrMsgIdAndTxt("mexopencv:error",
161  "Unrecognized property %s", prop.c_str());
162  }
163  else if (method == "set") {
164  nargchk(nrhs==4 && nlhs==0);
165  string prop(rhs[2].toString());
166  if (prop == "BlockSize")
167  obj->setBlockSize(rhs[3].toInt());
168  else if (prop == "HarrisDetector")
169  obj->setHarrisDetector(rhs[3].toBool());
170  else if (prop == "K")
171  obj->setK(rhs[3].toDouble());
172  else if (prop == "MaxFeatures")
173  obj->setMaxFeatures(rhs[3].toInt());
174  else if (prop == "MinDistance")
175  obj->setMinDistance(rhs[3].toDouble());
176  else if (prop == "QualityLevel")
177  obj->setQualityLevel(rhs[3].toDouble());
178  else
179  mexErrMsgIdAndTxt("mexopencv:error",
180  "Unrecognized property %s", prop.c_str());
181  }
182  //else if (method == "defaultNorm")
183  //else if (method == "descriptorSize")
184  //else if (method == "descriptorType")
185  //else if (method == "compute")
186  //else if (method == "detectAndCompute")
187  else
188  mexErrMsgIdAndTxt("mexopencv:error",
189  "Unrecognized operation %s",method.c_str());
190 }
virtual void setK(double k)=0
map< int, Ptr< GFTTDetector > > obj_
Object container.
virtual void setHarrisDetector(bool val)=0
virtual double getQualityLevel() 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.
virtual void detect(InputArray image, std::vector< KeyPoint > &keypoints, InputArray mask=noArray())
T end(T... args)
cv::Ptr< cv::GFTTDetector > createGFTTDetector(std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of GFTTDetector using options in arguments.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
T push_back(T... args)
virtual void setBlockSize(int blockSize)=0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
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...
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 bool getHarrisDetector() const=0
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)
STL class.
bool empty() const
virtual void setQualityLevel(double qlevel)=0
Global constant definitions.
T begin(T... args)
T c_str(T... args)
virtual void setMaxFeatures(int maxFeatures)=0
virtual void setMinDistance(double minDistance)=0
virtual bool empty() const
virtual void save(const String &filename) const
virtual int getMaxFeatures() const=0
int last_id
Last object id to allocate.
virtual int getBlockSize() const=0
virtual double getMinDistance() const=0
virtual double getK() const=0
cv::Mat toMat() const
T reserve(T... args)