mexopencv  0.1
mex interface for opencv library
GFTTDetector_.cpp
Go to the documentation of this file.
1 
8 #include <typeinfo>
9 #include "mexopencv.hpp"
10 #include "mexopencv_features2d.hpp"
11 using namespace std;
12 using namespace cv;
13 
14 // Persistent objects
15 namespace {
17 int last_id = 0;
19 map<int,Ptr<GFTTDetector> > obj_;
20 }
21 
29 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
30 {
31  // Check the number of arguments
32  nargchk(nrhs>=2 && nlhs<=1);
33 
34  // Argument vector
35  vector<MxArray> rhs(prhs, prhs+nrhs);
36  int id = rhs[0].toInt();
37  string method(rhs[1].toString());
38 
39  // Constructor is called. Create a new object from argument
40  if (method == "new") {
41  nargchk(nrhs>=2 && nlhs<=1);
42  obj_[++last_id] = createGFTTDetector(rhs.begin() + 2, rhs.end());
43  plhs[0] = MxArray(last_id);
44  return;
45  }
46 
47  // Big operation switch
48  Ptr<GFTTDetector> obj = obj_[id];
49  if (method == "delete") {
50  nargchk(nrhs==2 && nlhs==0);
51  obj_.erase(id);
52  }
53  else if (method == "typeid") {
54  nargchk(nrhs==2 && nlhs<=1);
55  plhs[0] = MxArray(string(typeid(*obj).name()));
56  }
57  else if (method == "clear") {
58  nargchk(nrhs==2 && nlhs==0);
59  obj->clear();
60  }
61  else if (method == "load") {
62  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
63  string objname;
64  bool loadFromString = false;
65  for (int i=3; i<nrhs; i+=2) {
66  string key(rhs[i].toString());
67  if (key == "ObjName")
68  objname = rhs[i+1].toString();
69  else if (key == "FromString")
70  loadFromString = rhs[i+1].toBool();
71  else
72  mexErrMsgIdAndTxt("mexopencv:error",
73  "Unrecognized option %s", key.c_str());
74  }
75  obj_[id] = (loadFromString ?
76  Algorithm::loadFromString<GFTTDetector>(rhs[2].toString(), objname) :
77  Algorithm::load<GFTTDetector>(rhs[2].toString(), objname));
78  }
79  else if (method == "save") {
80  nargchk(nrhs==3 && nlhs==0);
81  obj->save(rhs[2].toString());
82  }
83  else if (method == "empty") {
84  nargchk(nrhs==2 && nlhs<=1);
85  plhs[0] = MxArray(obj->empty());
86  }
87  else if (method == "getDefaultName") {
88  nargchk(nrhs==2 && nlhs<=1);
89  plhs[0] = MxArray(obj->getDefaultName());
90  }
91  else if (method == "detect") {
92  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
93  if (rhs[2].isNumeric()) { // first variant that accepts an image
94  Mat mask;
95  for (int i=3; i<nrhs; i+=2) {
96  string key(rhs[i].toString());
97  if (key == "Mask")
98  mask = rhs[i+1].toMat(CV_8U);
99  else
100  mexErrMsgIdAndTxt("mexopencv:error",
101  "Unrecognized option %s", key.c_str());
102  }
103  Mat image(rhs[2].toMat(CV_8U));
104  vector<KeyPoint> keypoints;
105  obj->detect(image, keypoints, mask);
106  plhs[0] = MxArray(keypoints);
107  }
108  else if (rhs[2].isCell()) { // second variant that accepts an image set
109  vector<Mat> masks;
110  for (int i=3; i<nrhs; i+=2) {
111  string key(rhs[i].toString());
112  if (key == "Mask") {
113  //masks = rhs[i+1].toVector<Mat>();
114  vector<MxArray> arr(rhs[i+1].toVector<MxArray>());
115  masks.clear();
116  masks.reserve(arr.size());
117  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
118  masks.push_back(it->toMat(CV_8U));
119  }
120  else
121  mexErrMsgIdAndTxt("mexopencv:error",
122  "Unrecognized option %s", key.c_str());
123  }
124  //vector<Mat> images(rhs[2].toVector<Mat>());
125  vector<Mat> images;
126  {
127  vector<MxArray> arr(rhs[2].toVector<MxArray>());
128  images.reserve(arr.size());
129  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
130  images.push_back(it->toMat(CV_8U));
131  }
132  vector<vector<KeyPoint> > keypoints;
133  obj->detect(images, keypoints, masks);
134  plhs[0] = MxArray(keypoints);
135  }
136  else
137  mexErrMsgIdAndTxt("mexopencv:error", "Invalid arguments");
138  }
139  else if (method == "get") {
140  nargchk(nrhs==3 && nlhs<=1);
141  string prop(rhs[2].toString());
142  if (prop == "BlockSize")
143  plhs[0] = MxArray(obj->getBlockSize());
144  else if (prop == "HarrisDetector")
145  plhs[0] = MxArray(obj->getHarrisDetector());
146  else if (prop == "K")
147  plhs[0] = MxArray(obj->getK());
148  else if (prop == "MaxFeatures")
149  plhs[0] = MxArray(obj->getMaxFeatures());
150  else if (prop == "MinDistance")
151  plhs[0] = MxArray(obj->getMinDistance());
152  else if (prop == "QualityLevel")
153  plhs[0] = MxArray(obj->getQualityLevel());
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 == "BlockSize")
162  obj->setBlockSize(rhs[3].toInt());
163  else if (prop == "HarrisDetector")
164  obj->setHarrisDetector(rhs[3].toBool());
165  else if (prop == "K")
166  obj->setK(rhs[3].toDouble());
167  else if (prop == "MaxFeatures")
168  obj->setMaxFeatures(rhs[3].toInt());
169  else if (prop == "MinDistance")
170  obj->setMinDistance(rhs[3].toDouble());
171  else if (prop == "QualityLevel")
172  obj->setQualityLevel(rhs[3].toDouble());
173  else
174  mexErrMsgIdAndTxt("mexopencv:error",
175  "Unrecognized property %s", prop.c_str());
176  }
177  //else if (method == "defaultNorm")
178  //else if (method == "descriptorSize")
179  //else if (method == "descriptorType")
180  //else if (method == "compute")
181  //else if (method == "detectAndCompute")
182  else
183  mexErrMsgIdAndTxt("mexopencv:error",
184  "Unrecognized operation %s",method.c_str());
185 }
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.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/ouput arguments number check.
Definition: mexopencv.hpp:166
Common definitions for the features2d and xfeatures2d modules.
Global constant definitions.