mexopencv  0.1
mex interface for opencv library
AgastFeatureDetector_.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<AgastFeatureDetector> > 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] = createAgastFeatureDetector(
43  rhs.begin() + 2, rhs.end());
44  plhs[0] = MxArray(last_id);
45  return;
46  }
47 
48  // Big operation switch
49  Ptr<AgastFeatureDetector> obj = obj_[id];
50  if (method == "delete") {
51  nargchk(nrhs==2 && nlhs==0);
52  obj_.erase(id);
53  }
54  else if (method == "typeid") {
55  nargchk(nrhs==2 && nlhs<=1);
56  plhs[0] = MxArray(string(typeid(*obj).name()));
57  }
58  else if (method == "clear") {
59  nargchk(nrhs==2 && nlhs==0);
60  obj->clear();
61  }
62  else if (method == "load") {
63  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
64  string objname;
65  bool loadFromString = false;
66  for (int i=3; i<nrhs; i+=2) {
67  string key(rhs[i].toString());
68  if (key == "ObjName")
69  objname = rhs[i+1].toString();
70  else if (key == "FromString")
71  loadFromString = rhs[i+1].toBool();
72  else
73  mexErrMsgIdAndTxt("mexopencv:error",
74  "Unrecognized option %s", key.c_str());
75  }
76  obj_[id] = (loadFromString ?
77  Algorithm::loadFromString<AgastFeatureDetector>(rhs[2].toString(), objname) :
78  Algorithm::load<AgastFeatureDetector>(rhs[2].toString(), objname));
79  }
80  else if (method == "save") {
81  nargchk(nrhs==3 && nlhs==0);
82  obj->save(rhs[2].toString());
83  }
84  else if (method == "empty") {
85  nargchk(nrhs==2 && nlhs<=1);
86  plhs[0] = MxArray(obj->empty());
87  }
88  else if (method == "getDefaultName") {
89  nargchk(nrhs==2 && nlhs<=1);
90  plhs[0] = MxArray(obj->getDefaultName());
91  }
92  else if (method == "detect") {
93  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
94  if (rhs[2].isNumeric()) { // first variant that accepts an image
95  Mat mask;
96  for (int i=3; i<nrhs; i+=2) {
97  string key(rhs[i].toString());
98  if (key == "Mask")
99  mask = rhs[i+1].toMat(CV_8U);
100  else
101  mexErrMsgIdAndTxt("mexopencv:error",
102  "Unrecognized option %s", key.c_str());
103  }
104  Mat image(rhs[2].toMat(CV_8U));
105  vector<KeyPoint> keypoints;
106  obj->detect(image, keypoints, mask);
107  plhs[0] = MxArray(keypoints);
108  }
109  else if (rhs[2].isCell()) { // second variant that accepts an image set
110  vector<Mat> masks;
111  for (int i=3; i<nrhs; i+=2) {
112  string key(rhs[i].toString());
113  if (key == "Mask") {
114  //masks = rhs[i+1].toVector<Mat>();
115  vector<MxArray> arr(rhs[i+1].toVector<MxArray>());
116  masks.clear();
117  masks.reserve(arr.size());
118  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
119  masks.push_back(it->toMat(CV_8U));
120  }
121  else
122  mexErrMsgIdAndTxt("mexopencv:error",
123  "Unrecognized option %s", key.c_str());
124  }
125  //vector<Mat> images(rhs[2].toVector<Mat>());
126  vector<Mat> images;
127  {
128  vector<MxArray> arr(rhs[2].toVector<MxArray>());
129  images.reserve(arr.size());
130  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
131  images.push_back(it->toMat(CV_8U));
132  }
133  vector<vector<KeyPoint> > keypoints;
134  obj->detect(images, keypoints, masks);
135  plhs[0] = MxArray(keypoints);
136  }
137  else
138  mexErrMsgIdAndTxt("mexopencv:error", "Invalid arguments");
139  }
140  else if (method == "get") {
141  nargchk(nrhs==3 && nlhs<=1);
142  string prop(rhs[2].toString());
143  if (prop == "NonmaxSuppression")
144  plhs[0] = MxArray(obj->getNonmaxSuppression());
145  else if (prop == "Threshold")
146  plhs[0] = MxArray(obj->getThreshold());
147  else if (prop == "Type")
148  plhs[0] = MxArray(AgastTypeInvMap[obj->getType()]);
149  else
150  mexErrMsgIdAndTxt("mexopencv:error",
151  "Unrecognized property %s", prop.c_str());
152  }
153  else if (method == "set") {
154  nargchk(nrhs==4 && nlhs==0);
155  string prop(rhs[2].toString());
156  if (prop == "NonmaxSuppression")
157  obj->setNonmaxSuppression(rhs[3].toBool());
158  else if (prop == "Threshold")
159  obj->setThreshold(rhs[3].toInt());
160  else if (prop == "Type")
161  obj->setType(AgastTypeMap[rhs[3].toString()]);
162  else
163  mexErrMsgIdAndTxt("mexopencv:error",
164  "Unrecognized property %s", prop.c_str());
165  }
166  //else if (method == "defaultNorm")
167  //else if (method == "descriptorSize")
168  //else if (method == "descriptorType")
169  //else if (method == "compute")
170  //else if (method == "detectAndCompute")
171  else
172  mexErrMsgIdAndTxt("mexopencv:error",
173  "Unrecognized operation %s",method.c_str());
174 }
const ConstMap< std::string, int > AgastTypeMap
AGAST neighborhood types.
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.
cv::Ptr< cv::AgastFeatureDetector > createAgastFeatureDetector(std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of AgastFeatureDetector using options in arguments.
Global constant definitions.
const ConstMap< int, std::string > AgastTypeInvMap
inverse AGAST neighborhood types