mexopencv  3.4.1
MEX interface for OpenCV library
BIF_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/face/bif.hpp"
10 using namespace std;
11 using namespace cv;
12 using namespace cv::face;
13 
14 namespace {
15 // Persistent objects
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 is called. Create a new object from argument
40  if (method == "new") {
41  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
42  int num_bands = 8;
43  int num_rotations = 12;
44  for (int i=2; i<nrhs; i+=2) {
45  string key(rhs[i].toString());
46  if (key == "NumBands")
47  num_bands = rhs[i+1].toInt();
48  else if (key == "NumRotations")
49  num_rotations = rhs[i+1].toInt();
50  else
51  mexErrMsgIdAndTxt("mexopencv:error",
52  "Unrecognized option %s", key.c_str());
53  }
54  obj_[++last_id] = BIF::create(num_bands, num_rotations);
55  plhs[0] = MxArray(last_id);
56  mexLock();
57  return;
58  }
59 
60  // Big operation switch
61  Ptr<BIF> obj = obj_[id];
62  if (obj.empty())
63  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
64  if (method == "delete") {
65  nargchk(nrhs==2 && nlhs==0);
66  obj_.erase(id);
67  mexUnlock();
68  }
69  else if (method == "clear") {
70  nargchk(nrhs==2 && nlhs==0);
71  obj->clear();
72  }
73  else if (method == "load") {
74  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
75  string objname;
76  bool loadFromString = false;
77  for (int i=3; i<nrhs; i+=2) {
78  string key(rhs[i].toString());
79  if (key == "ObjName")
80  objname = rhs[i+1].toString();
81  else if (key == "FromString")
82  loadFromString = rhs[i+1].toBool();
83  else
84  mexErrMsgIdAndTxt("mexopencv:error",
85  "Unrecognized option %s", key.c_str());
86  }
87  obj_[id] = (loadFromString ?
88  Algorithm::loadFromString<BIF>(rhs[2].toString(), objname) :
89  Algorithm::load<BIF>(rhs[2].toString(), objname));
90  }
91  else if (method == "save") {
92  nargchk(nrhs==3 && nlhs==0);
93  obj->save(rhs[2].toString());
94  }
95  else if (method == "empty") {
96  nargchk(nrhs==2 && nlhs<=1);
97  plhs[0] = MxArray(obj->empty());
98  }
99  else if (method == "getDefaultName") {
100  nargchk(nrhs==2 && nlhs<=1);
101  plhs[0] = MxArray(obj->getDefaultName());
102  }
103  else if (method == "compute") {
104  nargchk(nrhs==3 && nlhs<=1);
105  Mat image(rhs[2].toMat(CV_32F)),
106  features;
107  obj->compute(image, features);
108  plhs[0] = MxArray(features);
109  }
110  else if (method == "get") {
111  nargchk(nrhs==3 && nlhs<=1);
112  string prop(rhs[2].toString());
113  if (prop == "NumBands")
114  plhs[0] = MxArray(obj->getNumBands());
115  else if (prop == "NumRotations")
116  plhs[0] = MxArray(obj->getNumRotations());
117  else
118  mexErrMsgIdAndTxt("mexopencv:error",
119  "Unrecognized property %s", prop.c_str());
120  }
121  else
122  mexErrMsgIdAndTxt("mexopencv:error",
123  "Unrecognized operation %s", method.c_str());
124 }
virtual void compute(InputArray image, OutputArray features) const=0
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: BIF_.cpp:29
STL namespace.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual int getNumRotations() const=0
virtual void clear()
#define CV_32F
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.
virtual int getNumBands() const=0
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
STL class.
bool empty() const
virtual String getDefaultName() const
Global constant definitions.
T c_str(T... args)
virtual void save(const String &filename) const
virtual bool empty() const
map< int, Ptr< BIF > > obj_
Object container.
Definition: BIF_.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: BIF_.cpp:17