mexopencv  3.4.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;
21 
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  mexLock();
74  return;
75  }
76 
77  // Big operation switch
78  Ptr<DPMDetector> obj = obj_[id];
79  if (obj.empty())
80  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
81  if (method == "delete") {
82  nargchk(nrhs==2 && nlhs==0);
83  obj_.erase(id);
84  mexUnlock();
85  }
86  else if (method == "isEmpty") {
87  nargchk(nrhs==2 && nlhs<=1);
88  plhs[0] = MxArray(obj->isEmpty());
89  }
90  else if (method == "detect") {
91  nargchk(nrhs==3 && nlhs<=1);
92  Mat image(rhs[2].toMat(rhs[2].isUint8() ? CV_8U : CV_64F));
94  obj->detect(image, objects);
95  plhs[0] = toStruct(objects, obj->getClassNames());
96  }
97  else if (method == "getClassNames") {
98  nargchk(nrhs==2 && nlhs<=1);
99  plhs[0] = MxArray(obj->getClassNames());
100  }
101  else if (method == "getClassCount") {
102  nargchk(nrhs==2 && nlhs<=1);
103  plhs[0] = MxArray(static_cast<int>(obj->getClassCount()));
104  }
105  else
106  mexErrMsgIdAndTxt("mexopencv:error",
107  "Unrecognized operation %s", method.c_str());
108 }
int last_id
Last object id to allocate.
map< int, Ptr< DPMDetector > > obj_
Object container.
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
#define CV_8U
MxArray toStruct(const vector< DPMDetector::ObjectDetection > &vo, const vector< string > &classNames)
Convert object detections to struct array.
STL namespace.
virtual std::vector< std::string > const & getClassNames() const=0
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
void set(mwIndex index, const T &value)
Template for numeric array element write accessor.
Definition: MxArray.hpp:1310
STL class.
virtual size_t getClassCount() const=0
virtual void detect(cv::Mat &image, std::vector< ObjectDetection > &objects)=0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
#define CV_64F
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...
T str(T... args)
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
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
T size(T... args)
STL class.
bool empty() const
Global constant definitions.
T c_str(T... args)
virtual bool isEmpty() const=0
void create(int arows, int acols, int atype, Target target=ARRAY_BUFFER, bool autoRelease=false)
cv::Mat toMat() const