mexopencv  3.4.1
MEX interface for OpenCV library
HarrisLaplaceFeatureDetector_.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);
45  rhs.begin() + 2, rhs.end());
46  plhs[0] = MxArray(last_id);
47  mexLock();
48  return;
49  }
50 
51  // Big operation switch
53  if (obj.empty())
54  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
55  if (method == "delete") {
56  nargchk(nrhs==2 && nlhs==0);
57  obj_.erase(id);
58  mexUnlock();
59  }
60  else if (method == "typeid") {
61  nargchk(nrhs==2 && nlhs<=1);
62  plhs[0] = MxArray(string(typeid(*obj).name()));
63  }
64  else if (method == "clear") {
65  nargchk(nrhs==2 && nlhs==0);
66  obj->clear();
67  }
68  else if (method == "load") {
69  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
70  string objname;
71  bool loadFromString = false;
72  for (int i=3; i<nrhs; i+=2) {
73  string key(rhs[i].toString());
74  if (key == "ObjName")
75  objname = rhs[i+1].toString();
76  else if (key == "FromString")
77  loadFromString = rhs[i+1].toBool();
78  else
79  mexErrMsgIdAndTxt("mexopencv:error",
80  "Unrecognized option %s", key.c_str());
81  }
82  obj_[id] = (loadFromString ?
83  Algorithm::loadFromString<HarrisLaplaceFeatureDetector>(rhs[2].toString(), objname) :
84  Algorithm::load<HarrisLaplaceFeatureDetector>(rhs[2].toString(), objname));
85  }
86  else if (method == "save") {
87  nargchk(nrhs==3 && nlhs==0);
88  obj->save(rhs[2].toString());
89  }
90  else if (method == "empty") {
91  nargchk(nrhs==2 && nlhs<=1);
92  plhs[0] = MxArray(obj->empty());
93  }
94  else if (method == "getDefaultName") {
95  nargchk(nrhs==2 && nlhs<=1);
96  plhs[0] = MxArray(obj->getDefaultName());
97  }
98  else if (method == "detect") {
99  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
100  if (rhs[2].isNumeric()) { // first variant that accepts an image
101  Mat mask;
102  for (int i=3; i<nrhs; i+=2) {
103  string key(rhs[i].toString());
104  if (key == "Mask")
105  mask = rhs[i+1].toMat(CV_8U);
106  else
107  mexErrMsgIdAndTxt("mexopencv:error",
108  "Unrecognized option %s", key.c_str());
109  }
110  Mat image(rhs[2].toMat(CV_8U));
111  vector<KeyPoint> keypoints;
112  obj->detect(image, keypoints, mask);
113  plhs[0] = MxArray(keypoints);
114  }
115  else if (rhs[2].isCell()) { // second variant that accepts an image set
116  vector<Mat> masks;
117  for (int i=3; i<nrhs; i+=2) {
118  string key(rhs[i].toString());
119  if (key == "Mask") {
120  //masks = rhs[i+1].toVector<Mat>();
121  vector<MxArray> arr(rhs[i+1].toVector<MxArray>());
122  masks.clear();
123  masks.reserve(arr.size());
124  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
125  masks.push_back(it->toMat(CV_8U));
126  }
127  else
128  mexErrMsgIdAndTxt("mexopencv:error",
129  "Unrecognized option %s", key.c_str());
130  }
131  //vector<Mat> images(rhs[2].toVector<Mat>());
132  vector<Mat> images;
133  {
134  vector<MxArray> arr(rhs[2].toVector<MxArray>());
135  images.reserve(arr.size());
136  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
137  images.push_back(it->toMat(CV_8U));
138  }
139  vector<vector<KeyPoint> > keypoints;
140  obj->detect(images, keypoints, masks);
141  plhs[0] = MxArray(keypoints);
142  }
143  else
144  mexErrMsgIdAndTxt("mexopencv:error", "Invalid arguments");
145  }
146  //else if (method == "defaultNorm")
147  //else if (method == "descriptorSize")
148  //else if (method == "descriptorType")
149  //else if (method == "compute")
150  //else if (method == "detectAndCompute")
151  else
152  mexErrMsgIdAndTxt("mexopencv:error",
153  "Unrecognized operation %s",method.c_str());
154 }
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
#define CV_8U
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
STL namespace.
virtual void detect(InputArray image, std::vector< KeyPoint > &keypoints, InputArray mask=noArray())
T end(T... args)
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
map< int, Ptr< HarrisLaplaceFeatureDetector > > obj_
Object container.
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.
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::Ptr< cv::xfeatures2d::HarrisLaplaceFeatureDetector > createHarrisLaplaceFeatureDetector(std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of HarrisLaplaceFeatureDetector using options in arguments.
cv::Mat toMat() const
T reserve(T... args)