mexopencv  0.1
mex interface for opencv library
LUCID_.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<LUCID> > 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] = createLUCID(rhs.begin() + 2, rhs.end());
45  plhs[0] = MxArray(last_id);
46  return;
47  }
48 
49  // Big operation switch
50  Ptr<LUCID> 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  /*
78  obj_[id] = (loadFromString ?
79  Algorithm::loadFromString<LUCID>(rhs[2].toString(), objname) :
80  Algorithm::load<LUCID>(rhs[2].toString(), objname));
81  */
83  // HACK: workaround because LUCID::create() doesnt accept zero arguments
84  FileStorage fs(rhs[2].toString(), FileStorage::READ +
85  (loadFromString ? FileStorage::MEMORY : 0));
86  obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
87  if (obj.empty())
88  mexErrMsgIdAndTxt("mexopencv:error", "Failed to load algorithm");
89  //*/
90  }
91  else if (method == "save") {
92  nargchk(nrhs==3 && nlhs==0);
93  obj->save(rhs[2].toString());
94  }
95  else if (method == "empty") {
96  nargchk(nrhs==2 && nlhs<=1);
97  plhs[0] = MxArray(obj->empty());
98  }
99  else if (method == "getDefaultName") {
100  nargchk(nrhs==2 && nlhs<=1);
101  plhs[0] = MxArray(obj->getDefaultName());
102  }
103  else if (method == "defaultNorm") {
104  nargchk(nrhs==2 && nlhs<=1);
105  plhs[0] = MxArray(NormTypeInv[obj->defaultNorm()]);
106  }
107  else if (method == "descriptorSize") {
108  nargchk(nrhs==2 && nlhs<=1);
109  plhs[0] = MxArray(obj->descriptorSize());
110  }
111  else if (method == "descriptorType") {
112  nargchk(nrhs==2 && nlhs<=1);
113  plhs[0] = MxArray(ClassNameInvMap[obj->descriptorType()]);
114  }
115  else if (method == "compute") {
116  nargchk(nrhs==4 && nlhs<=2);
117  if (rhs[2].isNumeric()) { // first variant that accepts an image
118  Mat image(rhs[2].toMat(CV_8U)), descriptors;
119  if (image.channels() == 1)
120  // LUCID requires CV_8UC3
121  cvtColor(image, image, cv::COLOR_GRAY2BGR);
122  vector<KeyPoint> keypoints(rhs[3].toVector<KeyPoint>());
123  obj->compute(image, keypoints, descriptors);
124  plhs[0] = MxArray(descriptors);
125  if (nlhs > 1)
126  plhs[1] = MxArray(keypoints);
127  }
128  else if (rhs[2].isCell()) { // second variant that accepts an image set
129  //vector<Mat> images(rhs[2].toVector<Mat>());
130  vector<Mat> images, descriptors;
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  Mat img(it->toMat(CV_8U));
136  if (img.channels() == 1)
137  cvtColor(img, img, cv::COLOR_GRAY2BGR);
138  images.push_back(img);
139  }
140  }
141  vector<vector<KeyPoint> > keypoints(rhs[3].toVector(
142  const_mem_fun_ref_t<vector<KeyPoint>, MxArray>(
143  &MxArray::toVector<KeyPoint>)));
144  obj->compute(images, keypoints, descriptors);
145  plhs[0] = MxArray(descriptors);
146  if (nlhs > 1)
147  plhs[1] = MxArray(keypoints);
148  }
149  else
150  mexErrMsgIdAndTxt("mexopencv:error", "Invalid arguments");
151  }
152  //else if (method == "detect")
153  //else if (method == "detectAndCompute")
154  else
155  mexErrMsgIdAndTxt("mexopencv:error",
156  "Unrecognized operation %s",method.c_str());
157 }
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: LUCID_.cpp:31