mexopencv  0.1
mex interface for opencv library
DPMDetector_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/dpm.hpp"
10 #include <sstream>
11 using namespace std;
12 using namespace cv;
13 using namespace cv::dpm;
14 
15 namespace {
16 
18 int last_id = 0;
20 map<int,Ptr<DPMDetector> > obj_;
21 
27 MxArray toStruct(const vector<DPMDetector::ObjectDetection>& vo,
28  const vector<string>& classNames)
29 {
30  const char* fields[] = {"rect", "score", "class"};
31  MxArray s = MxArray::Struct(fields, 3, 1, vo.size());
32  for (size_t i=0; i<vo.size(); ++i) {
33  s.set("rect", vo[i].rect, i);
34  s.set("score", vo[i].score, i);
35  if (vo[i].classID >= 0 && vo[i].classID < classNames.size())
36  s.set("class", classNames[vo[i].classID], i);
37  else {
38  ostringstream ss;
39  ss << vo[i].classID;
40  s.set("class", ss.str(), i);
41  }
42  }
43  return s;
44 }
45 
46 } // local scope
47 
55 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
56 {
57  // Check the number of arguments
58  nargchk(nrhs>=2 && nlhs<=1);
59 
60  // Argument vector
61  vector<MxArray> rhs(prhs, prhs+nrhs);
62  int id = rhs[0].toInt();
63  string method(rhs[1].toString());
64 
65  // Constructor call
66  if (method == "new") {
67  nargchk((nrhs==3 || nrhs==4) && nlhs<=1);
68  obj_[++last_id] = (nrhs == 3) ?
69  DPMDetector::create(rhs[2].toVector<string>()) :
70  DPMDetector::create(rhs[2].toVector<string>(),
71  rhs[3].toVector<string>());
72  plhs[0] = MxArray(last_id);
73  return;
74  }
75 
76  // Big operation switch
77  Ptr<DPMDetector> obj = obj_[id];
78  if (method == "delete") {
79  nargchk(nrhs==2 && nlhs==0);
80  obj_.erase(id);
81  }
82  else if (method == "isEmpty") {
83  nargchk(nrhs==2 && nlhs<=1);
84  plhs[0] = MxArray(obj->isEmpty());
85  }
86  else if (method == "detect") {
87  nargchk(nrhs==3 && nlhs<=1);
88  Mat image(rhs[2].toMat(rhs[2].isUint8() ? CV_8U : CV_64F));
89  vector<DPMDetector::ObjectDetection> objects;
90  obj->detect(image, objects);
91  plhs[0] = toStruct(objects, obj->getClassNames());
92  }
93  else if (method == "getClassNames") {
94  nargchk(nrhs==2 && nlhs<=1);
95  plhs[0] = MxArray(obj->getClassNames());
96  }
97  else if (method == "getClassCount") {
98  nargchk(nrhs==2 && nlhs<=1);
99  plhs[0] = MxArray(static_cast<int>(obj->getClassCount()));
100  }
101  else
102  mexErrMsgIdAndTxt("mexopencv:error",
103  "Unrecognized operation %s", method.c_str());
104 }
void set(mwIndex index, const T &value)
Template for numeric array element write accessor.
Definition: MxArray.hpp:1310
MxArray toStruct(const std::vector< cv::ml::DTrees::Node > &nodes)
Convert tree nodes to struct array.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
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
static MxArray Struct(const char **fields=NULL, int nfields=0, mwSize m=1, mwSize n=1)
Create a new struct array.
Definition: MxArray.hpp:312
Global constant definitions.