mexopencv  3.4.1
MEX interface for OpenCV library
MSDDetector_.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<=1);
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] = createMSDDetector(rhs.begin() + 2, rhs.end());
45  plhs[0] = MxArray(last_id);
46  mexLock();
47  return;
48  }
49 
50  // Big operation switch
51  Ptr<MSDDetector> 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<MSDDetector>(rhs[2].toString(), objname) :
83  Algorithm::load<MSDDetector>(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 == "detect") {
98  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
99  if (rhs[2].isNumeric()) { // first variant that accepts an image
100  Mat mask;
101  for (int i=3; i<nrhs; i+=2) {
102  string key(rhs[i].toString());
103  if (key == "Mask")
104  mask = rhs[i+1].toMat(CV_8U);
105  else
106  mexErrMsgIdAndTxt("mexopencv:error",
107  "Unrecognized option %s", key.c_str());
108  }
109  Mat image(rhs[2].toMat(CV_8U));
110  vector<KeyPoint> keypoints;
111  obj->detect(image, keypoints, mask);
112  plhs[0] = MxArray(keypoints);
113  }
114  else if (rhs[2].isCell()) { // second variant that accepts an image set
115  vector<Mat> masks;
116  for (int i=3; i<nrhs; i+=2) {
117  string key(rhs[i].toString());
118  if (key == "Mask") {
119  //masks = rhs[i+1].toVector<Mat>();
120  vector<MxArray> arr(rhs[i+1].toVector<MxArray>());
121  masks.clear();
122  masks.reserve(arr.size());
123  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
124  masks.push_back(it->toMat(CV_8U));
125  }
126  else
127  mexErrMsgIdAndTxt("mexopencv:error",
128  "Unrecognized option %s", key.c_str());
129  }
130  //vector<Mat> images(rhs[2].toVector<Mat>());
131  vector<Mat> images;
132  {
133  vector<MxArray> arr(rhs[2].toVector<MxArray>());
134  images.reserve(arr.size());
135  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
136  images.push_back(it->toMat(CV_8U));
137  }
138  vector<vector<KeyPoint> > keypoints;
139  obj->detect(images, keypoints, masks);
140  plhs[0] = MxArray(keypoints);
141  }
142  else
143  mexErrMsgIdAndTxt("mexopencv:error", "Invalid arguments");
144  }
145  //else if (method == "defaultNorm")
146  //else if (method == "descriptorSize")
147  //else if (method == "descriptorType")
148  //else if (method == "compute")
149  //else if (method == "detectAndCompute")
150  else
151  mexErrMsgIdAndTxt("mexopencv:error",
152  "Unrecognized operation %s",method.c_str());
153 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
int last_id
Last object id to allocate.
#define CV_8U
STL namespace.
virtual void detect(InputArray image, std::vector< KeyPoint > &keypoints, InputArray mask=noArray())
T end(T... args)
cv::Ptr< cv::xfeatures2d::MSDDetector > createMSDDetector(std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of MSDDetector using options in arguments.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
T push_back(T... args)
virtual void clear()
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...
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
T clear(T... args)
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.
map< int, Ptr< MSDDetector > > obj_
Object container.
virtual String getDefaultName() const
T size(T... args)
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
cv::Mat toMat() const
T reserve(T... args)