mexopencv  3.4.1
MEX interface for OpenCV library
LBPHFaceRecognizer_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/face.hpp"
10 #include <typeinfo>
11 using namespace std;
12 using namespace cv;
13 using namespace cv::face;
14 
15 // Persistent objects
16 namespace {
18 int last_id = 0;
21 
30 {
31  ptrdiff_t len = std::distance(first, last);
32  nargchk((len%2)==0);
33  int radius = 1;
34  int neighbors = 8;
35  int grid_x = 8;
36  int grid_y = 8;
37  double threshold = DBL_MAX;
38  for (; first != last; first += 2) {
39  string key(first->toString());
40  const MxArray& val = *(first + 1);
41  if (key == "Radius")
42  radius = val.toInt();
43  else if (key == "Neighbors")
44  neighbors = val.toInt();
45  else if (key == "GridX")
46  grid_x = val.toInt();
47  else if (key == "GridY")
48  grid_y = val.toInt();
49  else if (key == "Threshold")
50  threshold = val.toDouble();
51  else
52  mexErrMsgIdAndTxt("mexopencv:error",
53  "Unrecognized option %s", key.c_str());
54  }
56  radius, neighbors, grid_x, grid_y, threshold);
57 }
58 
64 {
65  const char *fields[] = {"label", "distance"};
66  MxArray s = MxArray::Struct(fields, 2, 1, results.size());
67  for (mwIndex i = 0; i < results.size(); ++i) {
68  s.set("label", results[i].first, i);
69  s.set("distance", results[i].second, i);
70  }
71  return s;
72 }
73 }
74 
82 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
83 {
84  // Check the number of arguments
85  nargchk(nrhs>=2 && nlhs<=2);
86 
87  // Argument vector
88  vector<MxArray> rhs(prhs, prhs+nrhs);
89  int id = rhs[0].toInt();
90  string method(rhs[1].toString());
91 
92  // Constructor is called. Create a new object from argument
93  if (method == "new") {
94  nargchk(nrhs>=2 && nlhs<=1);
96  rhs.begin() + 2, rhs.end());
97  plhs[0] = MxArray(last_id);
98  mexLock();
99  return;
100  }
101 
102  // Big operation switch
103  Ptr<LBPHFaceRecognizer> obj = obj_[id];
104  if (obj.empty())
105  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
106  if (method == "delete") {
107  nargchk(nrhs==2 && nlhs==0);
108  obj_.erase(id);
109  mexUnlock();
110  }
111  else if (method == "typeid") {
112  nargchk(nrhs==2 && nlhs<=1);
113  plhs[0] = MxArray(string(typeid(*obj).name()));
114  }
115  else if (method == "clear") {
116  nargchk(nrhs==2 && nlhs==0);
117  obj->clear();
118  }
119  else if (method == "read") {
120  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
121  bool loadFromString = false;
122  for (int i=3; i<nrhs; i+=2) {
123  string key(rhs[i].toString());
124  if (key == "FromString")
125  loadFromString = rhs[i+1].toBool();
126  else
127  mexErrMsgIdAndTxt("mexopencv:error",
128  "Unrecognized option %s", key.c_str());
129  }
130  string fname(rhs[2].toString());
131  if (loadFromString) {
132  FileStorage fs(fname, FileStorage::READ + FileStorage::MEMORY);
133  if (!fs.isOpened())
134  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
135  obj->read(fs.getFirstTopLevelNode());
136  }
137  else
138  obj->read(fname);
139  }
140  else if (method == "write") {
141  nargchk(nrhs==3 && nlhs<=1);
142  string fname(rhs[2].toString());
143  if (nlhs > 0) {
144  // write to memory, and return string
145  FileStorage fs(fname, FileStorage::WRITE + FileStorage::MEMORY);
146  if (!fs.isOpened())
147  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
148  fs << obj->getDefaultName() << "{";
149  obj->write(fs);
150  fs << "}";
151  plhs[0] = MxArray(fs.releaseAndGetString());
152  }
153  else
154  // write to disk
155  obj->write(fname);
156  }
157  else if (method == "empty") {
158  nargchk(nrhs==2 && nlhs<=1);
159  plhs[0] = MxArray(obj->empty());
160  }
161  else if (method == "getDefaultName") {
162  nargchk(nrhs==2 && nlhs<=1);
163  plhs[0] = MxArray(obj->getDefaultName());
164  }
165  else if (method == "train") {
166  nargchk(nrhs==4 && nlhs==0);
167  vector<Mat> src(rhs[2].toVector<Mat>());
168  Mat labels(rhs[3].toMat(CV_32S));
169  obj->train(src, labels);
170  }
171  else if (method == "update") {
172  nargchk(nrhs==4 && nlhs==0);
173  vector<Mat> src(rhs[2].toVector<Mat>());
174  Mat labels(rhs[3].toMat(CV_32S));
175  obj->update(src, labels);
176  }
177  else if (method == "predict") {
178  nargchk(nrhs==3 && nlhs<=2);
179  Mat src(rhs[2].toMat());
180  int label = -1;
181  if (nlhs > 1) {
182  double confidence = 0;
183  obj->predict(src, label, confidence);
184  plhs[1] = MxArray(confidence);
185  }
186  else
187  label = obj->predict(src);
188  plhs[0] = MxArray(label);
189  }
190  else if (method == "predict_collect") {
191  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
192  bool sorted = false;
193  for (int i=3; i<nrhs; i+=2) {
194  string key(rhs[i].toString());
195  if (key == "Sorted")
196  sorted = rhs[i+1].toBool();
197  else
198  mexErrMsgIdAndTxt("mexopencv:error",
199  "Unrecognized option %s", key.c_str());
200  }
201  Mat src(rhs[2].toMat());
202  Ptr<StandardCollector> collector =
204  obj->predict(src, collector);
205  plhs[0] = toStruct(collector->getResults(sorted));
206  }
207  else if (method == "setLabelInfo") {
208  nargchk(nrhs==4 && nlhs==0);
209  int label = rhs[2].toInt();
210  string strInfo(rhs[3].toString());
211  obj->setLabelInfo(label, strInfo);
212  }
213  else if (method == "getLabelInfo") {
214  nargchk(nrhs==3 && nlhs<=1);
215  int label = rhs[2].toInt();
216  string strInfo(obj->getLabelInfo(label));
217  plhs[0] = MxArray(strInfo);
218  }
219  else if (method == "getLabelsByString") {
220  nargchk(nrhs==3 && nlhs<=1);
221  string str(rhs[2].toString());
222  vector<int> labels(obj->getLabelsByString(str));
223  plhs[0] = MxArray(labels);
224  }
225  else if (method == "getHistograms") {
226  nargchk(nrhs==2 && nlhs<=1);
227  plhs[0] = MxArray(obj->getHistograms());
228  }
229  else if (method == "getLabels") {
230  nargchk(nrhs==2 && nlhs<=1);
231  plhs[0] = MxArray(obj->getLabels());
232  }
233  else if (method == "get") {
234  nargchk(nrhs==3 && nlhs<=1);
235  string prop(rhs[2].toString());
236  if (prop == "GridX")
237  plhs[0] = MxArray(obj->getGridX());
238  else if (prop == "GridY")
239  plhs[0] = MxArray(obj->getGridY());
240  else if (prop == "Radius")
241  plhs[0] = MxArray(obj->getRadius());
242  else if (prop == "Neighbors")
243  plhs[0] = MxArray(obj->getNeighbors());
244  else if (prop == "Threshold")
245  plhs[0] = MxArray(obj->getThreshold());
246  else
247  mexErrMsgIdAndTxt("mexopencv:error",
248  "Unrecognized property %s", prop.c_str());
249  }
250  else if (method == "set") {
251  nargchk(nrhs==4 && nlhs==0);
252  string prop(rhs[2].toString());
253  if (prop == "GridX")
254  obj->setGridX(rhs[3].toInt());
255  else if (prop == "GridY")
256  obj->setGridY(rhs[3].toInt());
257  else if (prop == "Radius")
258  obj->setRadius(rhs[3].toInt());
259  else if (prop == "Neighbors")
260  obj->setNeighbors(rhs[3].toInt());
261  else if (prop == "Threshold")
262  obj->setThreshold(rhs[3].toDouble());
263  else
264  mexErrMsgIdAndTxt("mexopencv:error",
265  "Unrecognized property %s", prop.c_str());
266  }
267  else
268  mexErrMsgIdAndTxt("mexopencv:error",
269  "Unrecognized operation %s", method.c_str());
270 }
int toInt() const
Convert MxArray to int.
Definition: MxArray.cpp:489
T distance(T... args)
Ptr< LBPHFaceRecognizer > create_LBPHFaceRecognizer(vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of LBPHFaceRecognizer using options in arguments.
virtual void setLabelInfo(int label, const String &strInfo)
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
virtual std::vector< int > getLabelsByString(const String &str) const
virtual double getThreshold() const=0
virtual void setGridY(int val)=0
virtual int getGridY() const=0
STL namespace.
virtual void update(InputArrayOfArrays src, InputArray labels)
virtual int getRadius() const=0
T end(T... args)
MxArray toStruct(const vector< pair< int, double > > &results)
Convert results to struct array.
virtual bool isOpened() const
virtual String getLabelInfo(int label) const
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 void setGridX(int val)=0
virtual void read(const String &filename)
map< int, Ptr< LBPHFaceRecognizer > > obj_
Object container.
virtual void clear()
virtual String releaseAndGetString()
virtual void setRadius(int val)=0
virtual void write(const String &filename) const
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
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
FileNode getFirstTopLevelNode() const
virtual std::vector< cv::Mat > getHistograms() const=0
STL class.
bool empty() const
int predict(InputArray src) const
#define CV_32S
virtual String getDefaultName() const
Global constant definitions.
T begin(T... args)
virtual int getGridX() const=0
T c_str(T... args)
double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
virtual bool empty() const=0
std::vector< std::pair< int, double > > getResults(bool sorted=false) const
virtual void setNeighbors(int val)=0
virtual cv::Mat getLabels() const=0
virtual int getNeighbors() const=0
void create(int arows, int acols, int atype, Target target=ARRAY_BUFFER, bool autoRelease=false)
virtual void setThreshold(double val)=0
cv::Mat toMat() const
virtual void train(InputArrayOfArrays src, InputArray labels)=0