mexopencv  0.1
mex interface for opencv library
BRISK_.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<BRISK> > 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<=2);
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] = createBRISK(rhs.begin() + 2, rhs.end());
43  plhs[0] = MxArray(last_id);
44  return;
45  }
46 
47  // Big operation switch
48  Ptr<BRISK> 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<BRISK>(rhs[2].toString(), objname) :
77  Algorithm::load<BRISK>(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 == "defaultNorm") {
92  nargchk(nrhs==2 && nlhs<=1);
93  plhs[0] = MxArray(NormTypeInv[obj->defaultNorm()]);
94  }
95  else if (method == "descriptorSize") {
96  nargchk(nrhs==2 && nlhs<=1);
97  plhs[0] = MxArray(obj->descriptorSize());
98  }
99  else if (method == "descriptorType") {
100  nargchk(nrhs==2 && nlhs<=1);
101  plhs[0] = MxArray(ClassNameInvMap[obj->descriptorType()]);
102  }
103  else if (method == "detect") {
104  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
105  if (rhs[2].isNumeric()) { // first variant that accepts an image
106  Mat mask;
107  for (int i=3; i<nrhs; i+=2) {
108  string key(rhs[i].toString());
109  if (key == "Mask")
110  mask = rhs[i+1].toMat(CV_8U);
111  else
112  mexErrMsgIdAndTxt("mexopencv:error",
113  "Unrecognized option %s", key.c_str());
114  }
115  Mat image(rhs[2].toMat(CV_8U));
116  vector<KeyPoint> keypoints;
117  obj->detect(image, keypoints, mask);
118  plhs[0] = MxArray(keypoints);
119  }
120  else if (rhs[2].isCell()) { // second variant that accepts an image set
121  vector<Mat> masks;
122  for (int i=3; i<nrhs; i+=2) {
123  string key(rhs[i].toString());
124  if (key == "Mask") {
125  //masks = rhs[i+1].toVector<Mat>();
126  vector<MxArray> arr(rhs[i+1].toVector<MxArray>());
127  masks.clear();
128  masks.reserve(arr.size());
129  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
130  masks.push_back(it->toMat(CV_8U));
131  }
132  else
133  mexErrMsgIdAndTxt("mexopencv:error",
134  "Unrecognized option %s", key.c_str());
135  }
136  //vector<Mat> images(rhs[2].toVector<Mat>());
137  vector<Mat> images;
138  {
139  vector<MxArray> arr(rhs[2].toVector<MxArray>());
140  images.reserve(arr.size());
141  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
142  images.push_back(it->toMat(CV_8U));
143  }
144  vector<vector<KeyPoint> > keypoints;
145  obj->detect(images, keypoints, masks);
146  plhs[0] = MxArray(keypoints);
147  }
148  else
149  mexErrMsgIdAndTxt("mexopencv:error", "Invalid arguments");
150  }
151  else if (method == "compute") {
152  nargchk(nrhs==4 && nlhs<=2);
153  if (rhs[2].isNumeric()) { // first variant that accepts an image
154  Mat image(rhs[2].toMat(CV_8U)), descriptors;
155  vector<KeyPoint> keypoints(rhs[3].toVector<KeyPoint>());
156  obj->compute(image, keypoints, descriptors);
157  plhs[0] = MxArray(descriptors);
158  if (nlhs > 1)
159  plhs[1] = MxArray(keypoints);
160  }
161  else if (rhs[2].isCell()) { // second variant that accepts an image set
162  //vector<Mat> images(rhs[2].toVector<Mat>());
163  vector<Mat> images, descriptors;
164  {
165  vector<MxArray> arr(rhs[2].toVector<MxArray>());
166  images.reserve(arr.size());
167  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
168  images.push_back(it->toMat(CV_8U));
169  }
170  vector<vector<KeyPoint> > keypoints(rhs[3].toVector(
171  const_mem_fun_ref_t<vector<KeyPoint>, MxArray>(
172  &MxArray::toVector<KeyPoint>)));
173  obj->compute(images, keypoints, descriptors);
174  plhs[0] = MxArray(descriptors);
175  if (nlhs > 1)
176  plhs[1] = MxArray(keypoints);
177  }
178  else
179  mexErrMsgIdAndTxt("mexopencv:error", "Invalid arguments");
180  }
181  else if (method == "detectAndCompute") {
182  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=2);
183  Mat mask;
184  vector<KeyPoint> keypoints;
185  bool useProvidedKeypoints = false;
186  for (int i=3; i<nrhs; i+=2) {
187  string key(rhs[i].toString());
188  if (key == "Mask")
189  mask = rhs[i+1].toMat(CV_8U);
190  else if (key == "Keypoints") {
191  keypoints = rhs[i+1].toVector<KeyPoint>();
192  useProvidedKeypoints = true;
193  }
194  else
195  mexErrMsgIdAndTxt("mexopencv:error",
196  "Unrecognized option %s", key.c_str());
197  }
198  Mat image(rhs[2].toMat(CV_8U)), descriptors;
199  obj->detectAndCompute(image, mask, keypoints, descriptors,
200  useProvidedKeypoints);
201  plhs[0] = MxArray(keypoints);
202  if (nlhs > 1)
203  plhs[1] = MxArray(descriptors);
204  }
205  else
206  mexErrMsgIdAndTxt("mexopencv:error",
207  "Unrecognized operation %s",method.c_str());
208 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: BRISK_.cpp:29
const ConstMap< int, std::string > NormTypeInv
Inverse norm type map for option processing.
Definition: mexopencv.hpp:145
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.
const ConstMap< int, std::string > ClassNameInvMap
Translates data type definition used in OpenCV to that of MATLAB.
Definition: mexopencv.hpp:42
Global constant definitions.
cv::Ptr< cv::BRISK > createBRISK(std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of BRISK using options in arguments.