37 int id = rhs[0].toInt();
38 string method(rhs[1].toString());
41 if (method ==
"new") {
53 if (method ==
"delete") {
58 else if (method ==
"clear") {
62 else if (method ==
"load") {
63 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
65 bool loadFromString =
false;
66 for (
int i=3; i<nrhs; i+=2) {
67 string key(rhs[i].toString());
69 objname = rhs[i+1].toString();
70 else if (key ==
"FromString")
71 loadFromString = rhs[i+1].toBool();
74 "Unrecognized option %s", key.
c_str());
77 obj_[id] = (loadFromString ?
78 Algorithm::loadFromString<NormalBayesClassifier>(rhs[2].toString(), objname) :
79 Algorithm::load<NormalBayesClassifier>(rhs[2].toString(), objname));
81 else if (method ==
"save") {
83 string fname(rhs[2].toString());
86 FileStorage fs(fname, FileStorage::WRITE + FileStorage::MEMORY);
98 else if (method ==
"empty") {
102 else if (method ==
"getDefaultName") {
106 else if (method ==
"getVarCount") {
110 else if (method ==
"isClassifier") {
114 else if (method ==
"isTrained") {
118 else if (method ==
"train") {
119 nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=1);
122 for (
int i=4; i<nrhs; i+=2) {
123 string key(rhs[i].toString());
125 dataOptions = rhs[i+1].toVector<
MxArray>();
126 else if (key ==
"Flags")
127 flags = rhs[i+1].toInt();
128 else if (key ==
"UpdateModel")
129 UPDATE_FLAG(flags, rhs[i+1].toBool(), StatModel::UPDATE_MODEL);
132 "Unrecognized option %s", key.
c_str());
137 dataOptions.
begin(), dataOptions.
end());
142 dataOptions.
begin(), dataOptions.
end());
143 bool b = obj->
train(data, flags);
146 else if (method ==
"calcError") {
147 nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=2);
150 for (
int i=4; i<nrhs; i+=2) {
151 string key(rhs[i].toString());
153 dataOptions = rhs[i+1].toVector<
MxArray>();
154 else if (key ==
"TestError")
155 test = rhs[i+1].toBool();
158 "Unrecognized option %s", key.
c_str());
163 dataOptions.
begin(), dataOptions.
end());
168 dataOptions.
begin(), dataOptions.
end());
175 else if (method ==
"predict") {
176 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=2);
178 for (
int i=3; i<nrhs; i+=2) {
179 string key(rhs[i].toString());
181 flags = rhs[i+1].toInt();
182 else if (key ==
"RawOutput")
183 UPDATE_FLAG(flags, rhs[i+1].toBool(), StatModel::RAW_OUTPUT);
186 "Unrecognized option %s", key.
c_str());
190 float f = obj->
predict(samples, results, flags);
195 else if (method ==
"predictProb") {
196 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=3);
198 for (
int i=3; i<nrhs; i+=2) {
199 string key(rhs[i].toString());
201 flags = rhs[i+1].toInt();
202 else if (key ==
"RawOutput")
203 UPDATE_FLAG(flags, rhs[i+1].toBool(), StatModel::RAW_OUTPUT);
206 "Unrecognized option %s", key.
c_str());
212 (nlhs>1 ? outputProbs :
noArray()), flags);
215 plhs[1] =
MxArray(outputProbs);
221 "Unrecognized operation %s", method.
c_str());
map< int, Ptr< NormalBayesClassifier > > obj_
Object container.
virtual bool isTrained() const=0
int last_id
Last object id to allocate.
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
virtual bool train(const Ptr< TrainData > &trainData, int flags=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 String releaseAndGetString()
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
virtual void write(FileStorage &fs) const
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...
virtual float predictProb(InputArray inputs, OutputArray outputs, OutputArray outputProbs, int flags=0) const=0
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.
void nargchk(bool cond)
Alias for input/output arguments number check.
virtual String getDefaultName() const
Global constant definitions.
virtual float predict(InputArray samples, OutputArray results=noArray(), int flags=0) const=0
virtual bool empty() const
virtual bool isClassifier() const=0
virtual float calcError(const Ptr< TrainData > &data, bool test, OutputArray resp) const
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.
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