mexopencv  3.4.1
MEX interface for OpenCV library
WBDetector_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/xobjdetect.hpp"
10 using namespace std;
11 using namespace cv;
12 using namespace cv::xobjdetect;
13 
14 // Persistent objects
15 namespace {
17 int last_id = 0;
20 }
21 
29 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
30 {
31  // Check the number of arguments
32  nargchk(nrhs>=2 && nlhs<=2);
33 
34  // Argument vector
35  vector<MxArray> rhs(prhs, prhs+nrhs);
36  int id = rhs[0].toInt();
37  string method(rhs[1].toString());
38 
39  // Constructor call
40  if (method == "new") {
41  nargchk(nrhs==2 && nlhs<=1);
43  plhs[0] = MxArray(last_id);
44  mexLock();
45  return;
46  }
47 
48  // Big operation switch
49  Ptr<WBDetector> obj = obj_[id];
50  if (obj.empty())
51  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
52  if (method == "delete") {
53  nargchk(nrhs==2 && nlhs==0);
54  obj_.erase(id);
55  mexUnlock();
56  }
57  else if (method == "read") {
58  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
59  string objname;
60  bool loadFromString = false;
61  for (int i=3; i<nrhs; i+=2) {
62  string key(rhs[i].toString());
63  if (key == "ObjName")
64  objname = rhs[i+1].toString();
65  else if (key == "FromString")
66  loadFromString = rhs[i+1].toBool();
67  else
68  mexErrMsgIdAndTxt("mexopencv:error",
69  "Unrecognized option %s", key.c_str());
70  }
71  FileStorage fs(rhs[2].toString(), FileStorage::READ +
72  (loadFromString ? FileStorage::MEMORY : 0));
73  if (!fs.isOpened())
74  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
75  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
76  if (fn.empty())
77  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
78  obj->read(fn);
79  }
80  else if (method == "write") {
81  nargchk(nrhs==3 && nlhs<=1);
82  FileStorage fs(rhs[2].toString(), FileStorage::WRITE +
83  ((nlhs > 0) ? FileStorage::MEMORY : 0));
84  if (!fs.isOpened())
85  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
86  fs << "waldboost";
87  obj->write(fs);
88  if (nlhs > 0)
89  plhs[0] = MxArray(fs.releaseAndGetString());
90  }
91  else if (method == "train") {
92  nargchk(nrhs==4 && nlhs==0);
93  string pos_samples(rhs[2].toString()), neg_imgs(rhs[3].toString());
94  obj->train(pos_samples, neg_imgs);
95  }
96  else if (method == "detect") {
97  nargchk(nrhs==3 && nlhs<=2);
98  Mat img(rhs[2].toMat());
99  vector<Rect> bboxes;
100  vector<double> confidences;
101  obj->detect(img, bboxes, confidences);
102  plhs[0] = MxArray(bboxes);
103  if (nlhs>1)
104  plhs[1] = MxArray(confidences);
105  }
106  else
107  mexErrMsgIdAndTxt("mexopencv:error",
108  "Unrecognized operation %s", method.c_str());
109 }
T empty(T... args)
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
STL namespace.
virtual void read(const FileNode &node)=0
virtual bool isOpened() const
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: WBDetector_.cpp:29
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual void write(FileStorage &fs) const=0
virtual String releaseAndGetString()
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.
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
virtual void detect(const Mat &img, std::vector< Rect > &bboxes, std::vector< double > &confidences)=0
FileNode getFirstTopLevelNode() const
virtual void train(const std::string &pos_samples, const std::string &neg_imgs)=0
STL class.
bool empty() const
Global constant definitions.
T c_str(T... args)
map< int, Ptr< WBDetector > > obj_
Object container.
Definition: WBDetector_.cpp:19
void create(int arows, int acols, int atype, Target target=ARRAY_BUFFER, bool autoRelease=false)
cv::Mat toMat() const
int last_id
Last object id to allocate.
Definition: WBDetector_.cpp:17