mexopencv  0.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;
19 map<int,Ptr<WBDetector> > obj_;
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);
42  obj_[++last_id] = WBDetector::create();
43  plhs[0] = MxArray(last_id);
44  return;
45  }
46 
47  // Big operation switch
48  Ptr<WBDetector> obj = obj_[id];
49  if (method == "delete") {
50  nargchk(nrhs==2 && nlhs==0);
51  obj_.erase(id);
52  }
53  else if (method == "read") {
54  nargchk(nrhs==3 && nlhs==0);
55  FileStorage fs(rhs[2].toString(), FileStorage::READ);
56  obj->read(fs.getFirstTopLevelNode());
57  if (obj.empty())
58  mexErrMsgIdAndTxt("mexopencv:error", "Failed to read detector");
59  }
60  else if (method == "write") {
61  nargchk(nrhs==3 && nlhs==0);
62  FileStorage fs(rhs[2].toString(), FileStorage::WRITE);
63  fs << "waldboost";
64  obj->write(fs);
65  }
66  else if (method == "train") {
67  nargchk(nrhs==4 && nlhs==0);
68  string pos_samples(rhs[2].toString()), neg_imgs(rhs[3].toString());
69  obj->train(pos_samples, neg_imgs);
70  }
71  else if (method == "detect") {
72  nargchk(nrhs==3 && nlhs<=2);
73  Mat img(rhs[2].toMat());
74  vector<Rect> bboxes;
75  vector<double> confidences;
76  obj->detect(img, bboxes, confidences);
77  plhs[0] = MxArray(bboxes);
78  if (nlhs>1)
79  plhs[1] = MxArray(confidences);
80  }
81  else
82  mexErrMsgIdAndTxt("mexopencv:error",
83  "Unrecognized operation %s", method.c_str());
84 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: WBDetector_.cpp:29
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
Global constant definitions.