81 g.
minVal = m.
at(
"minVal").toDouble();
83 g.
maxVal = m.
at(
"maxVal").toDouble();
91 "Invalid argument to grid parameter");
126 void calc(
int vcount,
int n,
const float* vecs,
127 const float* another,
float* results)
147 std::fill(results, results + vcount, 0.0f);
171 return makePtr<MatlabFunction>(
func);
193 int id = rhs[0].toInt();
194 string method(rhs[1].toString());
197 if (method ==
"new") {
209 if (method ==
"delete") {
214 else if (method ==
"clear") {
218 else if (method ==
"load") {
219 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
221 bool loadFromString =
false;
222 for (
int i=3; i<nrhs; i+=2) {
223 string key(rhs[i].toString());
224 if (key ==
"ObjName")
225 objname = rhs[i+1].toString();
226 else if (key ==
"FromString")
227 loadFromString = rhs[i+1].toBool();
230 "Unrecognized option %s", key.
c_str());
233 obj_[id] = (loadFromString ?
234 Algorithm::loadFromString<SVM>(rhs[2].toString(), objname) :
235 Algorithm::load<SVM>(rhs[2].toString(), objname));
237 else if (method ==
"save") {
239 string fname(rhs[2].toString());
242 FileStorage fs(fname, FileStorage::WRITE + FileStorage::MEMORY);
254 else if (method ==
"empty") {
258 else if (method ==
"getDefaultName") {
262 else if (method ==
"getVarCount") {
266 else if (method ==
"isClassifier") {
270 else if (method ==
"isTrained") {
274 else if (method ==
"train") {
275 nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=1);
278 for (
int i=4; i<nrhs; i+=2) {
279 string key(rhs[i].toString());
281 dataOptions = rhs[i+1].toVector<
MxArray>();
282 else if (key ==
"Flags")
283 flags = rhs[i+1].toInt();
286 "Unrecognized option %s", key.
c_str());
291 dataOptions.
begin(), dataOptions.
end());
296 dataOptions.
begin(), dataOptions.
end());
297 bool b = obj->
train(data, flags);
300 else if (method ==
"calcError") {
301 nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=2);
304 for (
int i=4; i<nrhs; i+=2) {
305 string key(rhs[i].toString());
307 dataOptions = rhs[i+1].toVector<
MxArray>();
308 else if (key ==
"TestError")
309 test = rhs[i+1].toBool();
312 "Unrecognized option %s", key.
c_str());
317 dataOptions.
begin(), dataOptions.
end());
322 dataOptions.
begin(), dataOptions.
end());
329 else if (method ==
"predict") {
330 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=2);
332 for (
int i=3; i<nrhs; i+=2) {
333 string key(rhs[i].toString());
335 flags = rhs[i+1].toInt();
336 else if (key ==
"RawOutput")
337 UPDATE_FLAG(flags, rhs[i+1].toBool(), StatModel::RAW_OUTPUT);
340 "Unrecognized option %s", key.
c_str());
344 float f = obj->
predict(samples, results, flags);
349 else if (method ==
"trainAuto") {
350 nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=1);
353 bool balanced =
false;
354 ParamGrid CGrid = SVM::getDefaultGrid(SVM::C),
355 gammaGrid = SVM::getDefaultGrid(SVM::GAMMA),
356 pGrid = SVM::getDefaultGrid(SVM::P),
357 nuGrid = SVM::getDefaultGrid(SVM::NU),
358 coeffGrid = SVM::getDefaultGrid(SVM::COEF),
359 degreeGrid = SVM::getDefaultGrid(SVM::DEGREE);
360 for (
int i=4; i<nrhs; i+=2) {
361 string key(rhs[i].toString());
363 dataOptions = rhs[i+1].toVector<
MxArray>();
364 else if (key ==
"KFold")
365 kFold = rhs[i+1].toInt();
366 else if (key ==
"Balanced")
367 balanced = rhs[i+1].toBool();
368 else if (key ==
"CGrid")
370 else if (key ==
"GammaGrid")
372 else if (key ==
"PGrid")
374 else if (key ==
"NuGrid")
376 else if (key ==
"CoeffGrid")
378 else if (key ==
"DegreeGrid")
382 "Unrecognized option %s", key.
c_str());
387 dataOptions.
begin(), dataOptions.
end());
392 dataOptions.
begin(), dataOptions.
end());
394 CGrid, gammaGrid, pGrid, nuGrid, coeffGrid, degreeGrid, balanced);
397 else if (method ==
"getDecisionFunction") {
399 int index = rhs[2].toInt();
408 else if (method ==
"getSupportVectors") {
412 else if (method ==
"getUncompressedSupportVectors") {
416 else if (method ==
"setCustomKernel") {
420 else if (method ==
"get") {
422 string prop(rhs[2].toString());
425 else if (prop ==
"KernelType")
427 else if (prop ==
"Degree")
429 else if (prop ==
"Gamma")
431 else if (prop ==
"Coef0")
433 else if (prop ==
"C")
435 else if (prop ==
"Nu")
437 else if (prop ==
"P")
439 else if (prop ==
"ClassWeights")
441 else if (prop ==
"TermCriteria")
445 "Unrecognized property %s", prop.
c_str());
447 else if (method ==
"set") {
449 string prop(rhs[2].toString());
452 else if (prop ==
"KernelType")
454 else if (prop ==
"Degree")
456 else if (prop ==
"Gamma")
458 else if (prop ==
"Coef0")
460 else if (prop ==
"C")
461 obj->
setC(rhs[3].toDouble());
462 else if (prop ==
"Nu")
463 obj->
setNu(rhs[3].toDouble());
464 else if (prop ==
"P")
465 obj->
setP(rhs[3].toDouble());
466 else if (prop ==
"ClassWeights")
468 else if (prop ==
"TermCriteria")
472 "Unrecognized property %s", prop.
c_str());
476 "Unrecognized operation %s", method.
c_str());
virtual Mat getSupportVectors() const=0
virtual int getType() const=0
mwSize ndims() const
Number of dimensions.
virtual bool isTrained() const=0
virtual void setKernel(int kernelType)=0
virtual void setTermCriteria(const cv::TermCriteria &val)=0
const ConstMap< string, int > SVMKernelType
Option values for SVM Kernel types.
MatlabFunction(const string &func)
Constructor.
mwSize numel() const
Number of elements in an array.
T at(mwIndex index) const
Template for numeric array element accessor.
void calc(int vcount, int n, const float *vecs, const float *another, float *results)
Evaluates MATLAB kernel function.
bool isSingle() const
Determine whether array represents data as single-precision, floating-point numbers.
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
virtual void setDegree(double val)=0
const ConstMap< int, string > InvSVMType
Option values for inverse SVM types.
std::string toString() const
Convert MxArray to std::string.
virtual void setCustomKernel(const Ptr< Kernel > &_kernel)=0
virtual cv::Mat getClassWeights() const=0
virtual void setGamma(double val)=0
bool isChar() const
Determine whether input is string array.
LIBMMWMATRIX_PUBLISHED_API_EXTERN_C void mxDestroyArray(mxArray *pa)
mxArray destructor
Represents custom kernel implemented as a MATLAB function.
virtual bool isOpened() const
virtual cv::TermCriteria getTermCriteria() const=0
virtual void setNu(double val)=0
struct mxArray_tag mxArray
Forward declaration for mxArray.
virtual void setCoef0(double val)=0
virtual double getDegree() const=0
static Ptr< MatlabFunction > create(const string &func)
Factory function.
string func
name of MATLAB function to evaluate (custom face detector)
virtual bool train(const Ptr< TrainData > &trainData, int flags=0)
virtual bool trainAuto(const Ptr< TrainData > &data, int kFold=10, ParamGrid Cgrid=getDefaultGrid(C), ParamGrid gammaGrid=getDefaultGrid(GAMMA), ParamGrid pGrid=getDefaultGrid(P), ParamGrid nuGrid=getDefaultGrid(NU), ParamGrid coeffGrid=getDefaultGrid(COEF), ParamGrid degreeGrid=getDefaultGrid(DEGREE), bool balanced=false)=0
cv::Ptr< cv::ml::TrainData > loadTrainData(const std::string &filename, std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Read a dataset from a CSV file.
virtual double getDecisionFunction(int i, OutputArray alpha, OutputArray svidx) const=0
virtual String releaseAndGetString()
bool isNumeric() const
Determine whether array is numeric.
bool isComplex() const
Determine whether data is complex.
virtual void write(FileStorage &fs) const
virtual void setType(int val)=0
InputOutputArray noArray()
#define UPDATE_FLAG(NUM, TF, BIT)
set or clear a bit in flag depending on bool value
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 void setClassWeights(const cv::Mat &val)=0
bool isField(const std::string &fieldName) const
Determine whether a struct array has a specified field.
mxArray object wrapper for data conversion and manipulation.
void nargchk(bool cond)
Alias for input/output arguments number check.
Mat getUncompressedSupportVectors() const
bool isStruct() const
Determine whether input is structure array.
virtual double getNu() const=0
virtual double getGamma() const=0
std::vector< T > toVector() const
Convert MxArray to std::vector<T> of primitive types.
map< int, Ptr< SVM > > obj_
Object container.
virtual String getDefaultName() const
virtual void setP(double val)=0
Global constant definitions.
virtual double getC() const=0
int last_id
Last object id to allocate.
virtual float predict(InputArray samples, OutputArray results=noArray(), int flags=0) const=0
virtual bool empty() const
ParamGrid toParamGrid(const MxArray &m)
Obtain ParamGrid object from MxArray.
const ConstMap< string, int > SVMParamType
Option values for SVM params grid types.
virtual int getKernelType() const=0
const ConstMap< string, int > SVMType
Option values for SVM types.
int getType() const
Return type of SVM formulation.
virtual bool isClassifier() const=0
virtual float calcError(const Ptr< TrainData > &data, bool test, OutputArray resp) const
virtual double getP() const=0
virtual void save(const String &filename) const
cv::Ptr< cv::ml::TrainData > createTrainData(const cv::Mat &samples, const cv::Mat &responses, std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of TrainData using options in arguments.
std::map wrapper with one-line initialization and lookup method.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
LIBMWMEX_API_EXTERN_C int mexCallMATLAB(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[], const char *fcn_name)
call MATLAB function
virtual double getCoef0() const=0
void create(int arows, int acols, int atype, Target target=ARRAY_BUFFER, bool autoRelease=false)
Common definitions for the ml module.
virtual int getVarCount() const=0
virtual void setC(double val)=0
const ConstMap< int, string > InvSVMKernelType
Option values for inverse SVM Kernel types.