mexopencv  0.1
mex interface for opencv library
SIFT_.cpp
Go to the documentation of this file.
1 
8 #include <typeinfo>
9 #include "mexopencv.hpp"
10 #include "mexopencv_features2d.hpp"
11 #include "opencv2/xfeatures2d.hpp"
12 using namespace std;
13 using namespace cv;
14 using namespace cv::xfeatures2d;
15 
16 // Persistent objects
17 namespace {
19 int last_id = 0;
21 map<int,Ptr<SIFT> > obj_;
22 }
23 
31 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
32 {
33  // Check the number of arguments
34  nargchk(nrhs>=2 && nlhs<=2);
35 
36  // Argument vector
37  vector<MxArray> rhs(prhs, prhs+nrhs);
38  int id = rhs[0].toInt();
39  string method(rhs[1].toString());
40 
41  // Constructor is called. Create a new object from argument
42  if (method == "new") {
43  nargchk(nrhs>=2 && nlhs<=1);
44  obj_[++last_id] = createSIFT(rhs.begin() + 2, rhs.end());
45  plhs[0] = MxArray(last_id);
46  return;
47  }
48 
49  // Big operation switch
50  Ptr<SIFT> obj = obj_[id];
51  if (method == "delete") {
52  nargchk(nrhs==2 && nlhs==0);
53  obj_.erase(id);
54  }
55  else if (method == "typeid") {
56  nargchk(nrhs==2 && nlhs<=1);
57  plhs[0] = MxArray(string(typeid(*obj).name()));
58  }
59  else if (method == "clear") {
60  nargchk(nrhs==2 && nlhs==0);
61  obj->clear();
62  }
63  else if (method == "load") {
64  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
65  string objname;
66  bool loadFromString = false;
67  for (int i=3; i<nrhs; i+=2) {
68  string key(rhs[i].toString());
69  if (key == "ObjName")
70  objname = rhs[i+1].toString();
71  else if (key == "FromString")
72  loadFromString = rhs[i+1].toBool();
73  else
74  mexErrMsgIdAndTxt("mexopencv:error",
75  "Unrecognized option %s", key.c_str());
76  }
77  obj_[id] = (loadFromString ?
78  Algorithm::loadFromString<SIFT>(rhs[2].toString(), objname) :
79  Algorithm::load<SIFT>(rhs[2].toString(), objname));
80  }
81  else if (method == "save") {
82  nargchk(nrhs==3 && nlhs==0);
83  obj->save(rhs[2].toString());
84  }
85  else if (method == "empty") {
86  nargchk(nrhs==2 && nlhs<=1);
87  plhs[0] = MxArray(obj->empty());
88  }
89  else if (method == "getDefaultName") {
90  nargchk(nrhs==2 && nlhs<=1);
91  plhs[0] = MxArray(obj->getDefaultName());
92  }
93  else if (method == "defaultNorm") {
94  nargchk(nrhs==2 && nlhs<=1);
95  plhs[0] = MxArray(NormTypeInv[obj->defaultNorm()]);
96  }
97  else if (method == "descriptorSize") {
98  nargchk(nrhs==2 && nlhs<=1);
99  plhs[0] = MxArray(obj->descriptorSize());
100  }
101  else if (method == "descriptorType") {
102  nargchk(nrhs==2 && nlhs<=1);
103  plhs[0] = MxArray(ClassNameInvMap[obj->descriptorType()]);
104  }
105  else if (method == "detect") {
106  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
107  if (rhs[2].isNumeric()) { // first variant that accepts an image
108  Mat mask;
109  for (int i=3; i<nrhs; i+=2) {
110  string key(rhs[i].toString());
111  if (key == "Mask")
112  mask = rhs[i+1].toMat(CV_8U);
113  else
114  mexErrMsgIdAndTxt("mexopencv:error",
115  "Unrecognized option %s", key.c_str());
116  }
117  Mat image(rhs[2].toMat(CV_8U));
118  vector<KeyPoint> keypoints;
119  obj->detect(image, keypoints, mask);
120  plhs[0] = MxArray(keypoints);
121  }
122  else if (rhs[2].isCell()) { // second variant that accepts an image set
123  vector<Mat> masks;
124  for (int i=3; i<nrhs; i+=2) {
125  string key(rhs[i].toString());
126  if (key == "Mask") {
127  //masks = rhs[i+1].toVector<Mat>();
128  vector<MxArray> arr(rhs[i+1].toVector<MxArray>());
129  masks.clear();
130  masks.reserve(arr.size());
131  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
132  masks.push_back(it->toMat(CV_8U));
133  }
134  else
135  mexErrMsgIdAndTxt("mexopencv:error",
136  "Unrecognized option %s", key.c_str());
137  }
138  //vector<Mat> images(rhs[2].toVector<Mat>());
139  vector<Mat> images;
140  {
141  vector<MxArray> arr(rhs[2].toVector<MxArray>());
142  images.reserve(arr.size());
143  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
144  images.push_back(it->toMat(CV_8U));
145  }
146  vector<vector<KeyPoint> > keypoints;
147  obj->detect(images, keypoints, masks);
148  plhs[0] = MxArray(keypoints);
149  }
150  else
151  mexErrMsgIdAndTxt("mexopencv:error", "Invalid arguments");
152  }
153  else if (method == "compute") {
154  nargchk(nrhs==4 && nlhs<=2);
155  if (rhs[2].isNumeric()) { // first variant that accepts an image
156  Mat image(rhs[2].toMat(CV_8U)), descriptors;
157  vector<KeyPoint> keypoints(rhs[3].toVector<KeyPoint>());
158  obj->compute(image, keypoints, descriptors);
159  plhs[0] = MxArray(descriptors);
160  if (nlhs > 1)
161  plhs[1] = MxArray(keypoints);
162  }
163  else if (rhs[2].isCell()) { // second variant that accepts an image set
164  //vector<Mat> images(rhs[2].toVector<Mat>());
165  vector<Mat> images, descriptors;
166  {
167  vector<MxArray> arr(rhs[2].toVector<MxArray>());
168  images.reserve(arr.size());
169  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
170  images.push_back(it->toMat(CV_8U));
171  }
172  vector<vector<KeyPoint> > keypoints(rhs[3].toVector(
173  const_mem_fun_ref_t<vector<KeyPoint>, MxArray>(
174  &MxArray::toVector<KeyPoint>)));
175  obj->compute(images, keypoints, descriptors);
176  plhs[0] = MxArray(descriptors);
177  if (nlhs > 1)
178  plhs[1] = MxArray(keypoints);
179  }
180  else
181  mexErrMsgIdAndTxt("mexopencv:error", "Invalid arguments");
182  }
183  else if (method == "detectAndCompute") {
184  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=2);
185  Mat mask;
186  vector<KeyPoint> keypoints;
187  bool useProvidedKeypoints = false;
188  for (int i=3; i<nrhs; i+=2) {
189  string key(rhs[i].toString());
190  if (key == "Mask")
191  mask = rhs[i+1].toMat(CV_8U);
192  else if (key == "Keypoints") {
193  keypoints = rhs[i+1].toVector<KeyPoint>();
194  useProvidedKeypoints = true;
195  }
196  else
197  mexErrMsgIdAndTxt("mexopencv:error",
198  "Unrecognized option %s", key.c_str());
199  }
200  Mat image(rhs[2].toMat(CV_8U)), descriptors;
201  obj->detectAndCompute(image, mask, keypoints, descriptors,
202  useProvidedKeypoints);
203  plhs[0] = MxArray(keypoints);
204  if (nlhs > 1)
205  plhs[1] = MxArray(descriptors);
206  }
207  else
208  mexErrMsgIdAndTxt("mexopencv:error",
209  "Unrecognized operation %s",method.c_str());
210 }
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.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: SIFT_.cpp:31