51 int id = rhs[0].toInt();
52 string method(rhs[1].toString());
55 if (method ==
"new") {
67 if (method ==
"delete") {
72 else if (method ==
"clear") {
76 else if (method ==
"load") {
77 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
79 bool loadFromString =
false;
80 for (
int i=3; i<nrhs; i+=2) {
81 string key(rhs[i].toString());
83 objname = rhs[i+1].toString();
84 else if (key ==
"FromString")
85 loadFromString = rhs[i+1].toBool();
88 "Unrecognized option %s", key.
c_str());
91 obj_[id] = (loadFromString ?
92 Algorithm::loadFromString<EM>(rhs[2].toString(), objname) :
93 Algorithm::load<EM>(rhs[2].toString(), objname));
95 else if (method ==
"save") {
97 string fname(rhs[2].toString());
100 FileStorage fs(fname, FileStorage::WRITE + FileStorage::MEMORY);
112 else if (method ==
"empty") {
116 else if (method ==
"getDefaultName") {
120 else if (method ==
"getVarCount") {
124 else if (method ==
"isClassifier") {
128 else if (method ==
"isTrained") {
132 else if (method ==
"train") {
133 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
136 for (
int i=3; i<nrhs; i+=2) {
137 string key(rhs[i].toString());
139 dataOptions = rhs[i+1].toVector<
MxArray>();
140 else if (key ==
"Flags")
141 flags = rhs[i+1].toInt();
144 "Unrecognized option %s", key.
c_str());
149 dataOptions.
begin(), dataOptions.
end());
153 dataOptions.
begin(), dataOptions.
end());
154 bool b = obj->
train(data, flags);
157 else if (method ==
"calcError") {
158 nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=2);
161 for (
int i=4; i<nrhs; i+=2) {
162 string key(rhs[i].toString());
164 dataOptions = rhs[i+1].toVector<
MxArray>();
165 else if (key ==
"TestError")
166 test = rhs[i+1].toBool();
169 "Unrecognized option %s", key.
c_str());
174 dataOptions.
begin(), dataOptions.
end());
179 dataOptions.
begin(), dataOptions.
end());
186 else if (method ==
"predict") {
187 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=2);
189 for (
int i=3; i<nrhs; i+=2) {
190 string key(rhs[i].toString());
192 flags = rhs[i+1].toInt();
195 "Unrecognized option %s", key.
c_str());
199 float f = obj->
predict(samples, results, flags);
204 else if (method ==
"trainEM") {
207 logLikelihoods, labels, probs;
209 (nlhs>0 ? logLikelihoods :
noArray()),
212 plhs[0] =
MxArray(logLikelihoods);
220 else if (method ==
"trainE") {
221 nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=4);
224 for(
int i = 4; i < nrhs; i += 2) {
225 string key(rhs[i].toString());
226 if (key ==
"Covs0") {
235 else if (key ==
"Weights0")
236 weights0 = rhs[i+1].toMat(
240 "Unrecognized option %s", key.
c_str());
244 logLikelihoods, labels, probs;
245 bool b = obj->
trainE(samples, means0, covs0, weights0,
246 (nlhs>0 ? logLikelihoods :
noArray()),
249 plhs[0] =
MxArray(logLikelihoods);
257 else if (method ==
"trainM") {
261 logLikelihoods, labels, probs;
262 bool b = obj->
trainM(samples, probs0,
263 (nlhs>0 ? logLikelihoods :
noArray()),
266 plhs[0] =
MxArray(logLikelihoods);
274 else if (method ==
"predict2") {
282 for (
size_t i = 0; i < samples.rows; ++i) {
287 plhs[0] =
MxArray(
Mat(results,
false).reshape(1,0));
291 else if (method ==
"getCovs") {
297 else if (method ==
"getMeans") {
301 else if (method ==
"getWeights") {
305 else if (method ==
"get") {
307 string prop(rhs[2].toString());
308 if (prop ==
"ClustersNumber")
310 else if (prop ==
"CovarianceMatrixType")
312 else if (prop ==
"TermCriteria")
316 "Unrecognized property %s", prop.
c_str());
318 else if (method ==
"set") {
320 string prop(rhs[2].toString());
321 if (prop ==
"ClustersNumber")
323 else if (prop ==
"CovarianceMatrixType")
325 else if (prop ==
"TermCriteria")
329 "Unrecognized property %s", prop.
c_str());
333 "Unrecognized operation %s", method.
c_str());
virtual Mat getMeans() const=0
virtual bool isTrained() const=0
virtual void getCovs(std::vector< Mat > &covs) const=0
virtual bool trainE(InputArray samples, InputArray means0, InputArray covs0=noArray(), InputArray weights0=noArray(), OutputArray logLikelihoods=noArray(), OutputArray labels=noArray(), OutputArray probs=noArray())=0
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
virtual bool isOpened() const
virtual Vec2d predict2(InputArray sample, OutputArray probs) const=0
struct mxArray_tag mxArray
Forward declaration for mxArray.
virtual void setClustersNumber(int val)=0
virtual int getCovarianceMatrixType() const=0
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()
const ConstMap< int, string > CovMatTypeInv
CovMatTypeInv map for option processing.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
virtual void write(FileStorage &fs) const
InputOutputArray noArray()
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.
map< int, Ptr< EM > > obj_
Object container.
virtual void setCovarianceMatrixType(int val)=0
virtual bool trainM(InputArray samples, InputArray probs0, OutputArray logLikelihoods=noArray(), OutputArray labels=noArray(), OutputArray probs=noArray())=0
mxArray object wrapper for data conversion and manipulation.
void nargchk(bool cond)
Alias for input/output arguments number check.
virtual void setTermCriteria(const TermCriteria &val)=0
virtual float predict(InputArray samples, OutputArray results=noArray(), int flags=0) const=0
virtual int getClustersNumber() const=0
int last_id
Last object id to allocate.
virtual String getDefaultName() const
const ConstMap< string, int > CovMatType
CovMatType map for option processing.
Global constant definitions.
virtual bool empty() const
void create(int rows, int cols, int type)
virtual bool isClassifier() const=0
virtual bool trainEM(InputArray samples, OutputArray logLikelihoods=noArray(), OutputArray labels=noArray(), OutputArray probs=noArray())=0
virtual float calcError(const Ptr< TrainData > &data, bool test, OutputArray resp) const
virtual Mat getWeights() const=0
virtual void save(const String &filename) const
virtual TermCriteria getTermCriteria() const=0
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 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