mexopencv  3.4.1
MEX interface for OpenCV library
DAISY_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
10 #include "opencv2/xfeatures2d.hpp"
11 #include <typeinfo>
12 using namespace std;
13 using namespace cv;
14 using namespace cv::xfeatures2d;
15 
16 // Persistent objects
17 namespace {
19 int last_id = 0;
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] = createDAISY(rhs.begin() + 2, rhs.end());
45  plhs[0] = MxArray(last_id);
46  mexLock();
47  return;
48  }
49 
50  // Big operation switch
51  Ptr<DAISY> obj = obj_[id];
52  if (obj.empty())
53  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
54  if (method == "delete") {
55  nargchk(nrhs==2 && nlhs==0);
56  obj_.erase(id);
57  mexUnlock();
58  }
59  else if (method == "typeid") {
60  nargchk(nrhs==2 && nlhs<=1);
61  plhs[0] = MxArray(string(typeid(*obj).name()));
62  }
63  else if (method == "clear") {
64  nargchk(nrhs==2 && nlhs==0);
65  obj->clear();
66  }
67  else if (method == "load") {
68  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
69  string objname;
70  bool loadFromString = false;
71  for (int i=3; i<nrhs; i+=2) {
72  string key(rhs[i].toString());
73  if (key == "ObjName")
74  objname = rhs[i+1].toString();
75  else if (key == "FromString")
76  loadFromString = rhs[i+1].toBool();
77  else
78  mexErrMsgIdAndTxt("mexopencv:error",
79  "Unrecognized option %s", key.c_str());
80  }
81  obj_[id] = (loadFromString ?
82  Algorithm::loadFromString<DAISY>(rhs[2].toString(), objname) :
83  Algorithm::load<DAISY>(rhs[2].toString(), objname));
84  }
85  else if (method == "save") {
86  nargchk(nrhs==3 && nlhs==0);
87  obj->save(rhs[2].toString());
88  }
89  else if (method == "empty") {
90  nargchk(nrhs==2 && nlhs<=1);
91  plhs[0] = MxArray(obj->empty());
92  }
93  else if (method == "getDefaultName") {
94  nargchk(nrhs==2 && nlhs<=1);
95  plhs[0] = MxArray(obj->getDefaultName());
96  }
97  else if (method == "defaultNorm") {
98  nargchk(nrhs==2 && nlhs<=1);
99  plhs[0] = MxArray(NormTypeInv[obj->defaultNorm()]);
100  }
101  else if (method == "descriptorSize") {
102  nargchk(nrhs==2 && nlhs<=1);
103  plhs[0] = MxArray(obj->descriptorSize());
104  }
105  else if (method == "descriptorType") {
106  nargchk(nrhs==2 && nlhs<=1);
107  plhs[0] = MxArray(ClassNameInvMap[obj->descriptorType()]);
108  }
109  else if (method == "compute") {
110  nargchk(nrhs==4 && nlhs<=2);
111  if (rhs[2].isNumeric()) { // first variant that accepts an image
112  Mat image(rhs[2].toMat(rhs[2].isFloat() ? CV_32F : CV_8U)),
113  descriptors;
114  vector<KeyPoint> keypoints(rhs[3].toVector<KeyPoint>());
115  obj->compute(image, keypoints, descriptors);
116  plhs[0] = MxArray(descriptors);
117  if (nlhs > 1)
118  plhs[1] = MxArray(keypoints);
119  }
120  else if (rhs[2].isCell()) { // second variant that accepts an image set
121  //vector<Mat> images(rhs[2].toVector<Mat>());
122  vector<Mat> images, descriptors;
123  {
124  vector<MxArray> arr(rhs[2].toVector<MxArray>());
125  images.reserve(arr.size());
126  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
127  images.push_back(it->toMat(it->isFloat() ? CV_32F : CV_8U));
128  }
129  vector<vector<KeyPoint> > keypoints(rhs[3].toVector(
130  const_mem_fun_ref_t<vector<KeyPoint>, MxArray>(
131  &MxArray::toVector<KeyPoint>)));
132  obj->compute(images, keypoints, descriptors);
133  plhs[0] = MxArray(descriptors);
134  if (nlhs > 1)
135  plhs[1] = MxArray(keypoints);
136  }
137  else
138  mexErrMsgIdAndTxt("mexopencv:error", "Invalid arguments");
139  }
140  else if (method == "compute_all") {
141  nargchk((nrhs==3 || nrhs==4) && nlhs<=1);
142  Mat image(rhs[2].toMat(rhs[2].isFloat() ? CV_32F : CV_8U)),
143  descriptors;
144  if (nrhs == 4) {
145  Rect roi(rhs[3].toRect());
146  obj->compute(image, roi, descriptors);
147  }
148  else
149  obj->compute(image, descriptors);
150  plhs[0] = MxArray(descriptors);
151  }
152  else if (method == "GetDescriptor") {
153  nargchk(nrhs>=5 && (nrhs%2)==1 && nlhs<=2);
154  double y = rhs[2].toDouble(),
155  x = rhs[3].toDouble();
156  int orientation = rhs[4].toInt();
157  bool unnormalized = false;
158  bool useHomography = false;
159  Matx33d H;
160  for (int i=5; i<nrhs; i+=2) {
161  string key(rhs[i].toString());
162  if (key == "Unnormalized")
163  unnormalized = rhs[i+1].toBool();
164  else if (key == "H") {
165  H = rhs[i+1].toMatx<double,3,3>();
166  useHomography = true;
167  }
168  else
169  mexErrMsgIdAndTxt("mexopencv:error",
170  "Unrecognized option %s", key.c_str());
171  }
172  vector<float> descriptor(obj->descriptorSize());
173  bool ret = true;
174  if (unnormalized) {
175  if (useHomography)
176  ret = obj->GetUnnormalizedDescriptor(y, x, orientation,
177  &descriptor[0], H.val);
178  else
179  obj->GetUnnormalizedDescriptor(y, x, orientation,
180  &descriptor[0]);
181  }
182  else {
183  if (useHomography)
184  ret = obj->GetDescriptor(y, x, orientation,
185  &descriptor[0], H.val);
186  else
187  obj->GetDescriptor(y, x, orientation, &descriptor[0]);
188  }
189  plhs[0] = MxArray(descriptor);
190  if (nlhs>1)
191  plhs[1] = MxArray(ret);
192  }
193  //else if (method == "detect")
194  //else if (method == "detectAndCompute")
195  else
196  mexErrMsgIdAndTxt("mexopencv:error",
197  "Unrecognized operation %s",method.c_str());
198 }
virtual int descriptorType() const
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
#define CV_8U
virtual void GetDescriptor(double y, double x, int orientation, float *descriptor) const=0
STL namespace.
virtual void compute(InputArray image, std::vector< KeyPoint > &keypoints, OutputArray descriptors)=0
const ConstMap< int, std::string > NormTypeInv
Inverse norm type map for option processing.
Definition: mexopencv.hpp:160
T end(T... args)
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
cv::Ptr< cv::xfeatures2d::DAISY > createDAISY(std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of DAISY using options in arguments.
T push_back(T... args)
virtual int defaultNorm() const
_Tp val[m *n]
virtual void clear()
#define CV_32F
LIBMWMEX_API_EXTERN_C void mexErrMsgIdAndTxt(const char *identifier, const char *err_msg,...)
Issue formatted error message with corresponding error identifier and return to MATLAB prompt...
int last_id
Last object id to allocate.
Definition: DAISY_.cpp:19
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
virtual int descriptorSize() const
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/output arguments number check.
Definition: mexopencv.hpp:181
Common definitions for the features2d and xfeatures2d modules.
virtual String getDefaultName() const
T size(T... args)
const ConstMap< int, std::string > ClassNameInvMap
Translates data type definition used in OpenCV to that of MATLAB.
Definition: mexopencv.hpp:42
STL class.
bool empty() const
Global constant definitions.
T begin(T... args)
T c_str(T... args)
virtual bool empty() const
virtual void save(const String &filename) const
map< int, Ptr< DAISY > > obj_
Object container.
Definition: DAISY_.cpp:21
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: DAISY_.cpp:31
cv::Mat toMat() const
virtual void GetUnnormalizedDescriptor(double y, double x, int orientation, float *descriptor) const=0
T reserve(T... args)