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<DTrees>(rhs[2].toString(), objname) :
79 Algorithm::load<DTrees>(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();
130 "Unrecognized option %s", key.
c_str());
135 dataOptions.
begin(), dataOptions.
end());
140 dataOptions.
begin(), dataOptions.
end());
141 bool b = obj->
train(data, flags);
144 else if (method ==
"calcError") {
145 nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=2);
148 for (
int i=4; i<nrhs; i+=2) {
149 string key(rhs[i].toString());
151 dataOptions = rhs[i+1].toVector<
MxArray>();
152 else if (key ==
"TestError")
153 test = rhs[i+1].toBool();
156 "Unrecognized option %s", key.
c_str());
161 dataOptions.
begin(), dataOptions.
end());
166 dataOptions.
begin(), dataOptions.
end());
173 else if (method ==
"predict") {
174 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=2);
176 for (
int i=3; i<nrhs; i+=2) {
177 string key(rhs[i].toString());
179 flags = rhs[i+1].toInt();
180 else if (key ==
"RawOutput")
181 UPDATE_FLAG(flags, rhs[i+1].toBool(), StatModel::RAW_OUTPUT);
182 else if (key ==
"CompressedInput")
183 UPDATE_FLAG(flags, rhs[i+1].toBool(), StatModel::COMPRESSED_INPUT);
184 else if (key ==
"PreprocessedInput")
185 UPDATE_FLAG(flags, rhs[i+1].toBool(), StatModel::PREPROCESSED_INPUT);
186 else if (key ==
"PredictAuto") {
188 UPDATE_FLAG(flags, !rhs[i+1].toBool(), DTrees::PREDICT_SUM);
189 UPDATE_FLAG(flags, !rhs[i+1].toBool(), DTrees::PREDICT_MAX_VOTE);
191 else if (key ==
"PredictSum")
192 UPDATE_FLAG(flags, rhs[i+1].toBool(), DTrees::PREDICT_SUM);
193 else if (key ==
"PredictMaxVote")
194 UPDATE_FLAG(flags, rhs[i+1].toBool(), DTrees::PREDICT_MAX_VOTE);
197 "Unrecognized option %s", key.
c_str());
201 float f = obj->
predict(samples, results, flags);
206 else if (method ==
"getNodes") {
210 else if (method ==
"getRoots") {
214 else if (method ==
"getSplits") {
218 else if (method ==
"getSubsets") {
222 else if (method ==
"get") {
224 string prop(rhs[2].toString());
225 if (prop ==
"CVFolds")
227 else if (prop ==
"MaxCategories")
229 else if (prop ==
"MaxDepth")
231 else if (prop ==
"MinSampleCount")
233 else if (prop ==
"Priors")
235 else if (prop ==
"RegressionAccuracy")
237 else if (prop ==
"TruncatePrunedTree")
239 else if (prop ==
"Use1SERule")
241 else if (prop ==
"UseSurrogates")
245 "Unrecognized property %s", prop.
c_str());
247 else if (method ==
"set") {
249 string prop(rhs[2].toString());
250 if (prop ==
"CVFolds")
252 else if (prop ==
"MaxCategories")
254 else if (prop ==
"MaxDepth")
256 else if (prop ==
"MinSampleCount")
258 else if (prop ==
"Priors")
260 else if (prop ==
"RegressionAccuracy")
262 else if (prop ==
"TruncatePrunedTree")
264 else if (prop ==
"Use1SERule")
266 else if (prop ==
"UseSurrogates")
270 "Unrecognized property %s", prop.
c_str());
274 "Unrecognized operation %s", method.
c_str());
virtual int getMaxDepth() const=0
virtual bool getUse1SERule() const=0
virtual bool isTrained() const=0
virtual void setMinSampleCount(int val)=0
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
virtual void setUse1SERule(bool val)=0
virtual void setRegressionAccuracy(float val)=0
virtual void setUseSurrogates(bool val)=0
virtual float getRegressionAccuracy() const=0
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
int last_id
Last object id to allocate.
virtual bool getUseSurrogates() const=0
MxArray toStruct(const std::vector< cv::ml::DTrees::Node > &nodes)
Convert tree nodes to struct array.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
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 const std::vector< Split > & getSplits() const=0
virtual String releaseAndGetString()
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 const std::vector< Node > & getNodes() 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 int getMaxCategories() const=0
virtual bool getTruncatePrunedTree() const=0
virtual cv::Mat getPriors() const=0
virtual const std::vector< int > & getSubsets() const=0
virtual int getMinSampleCount() const=0
virtual String getDefaultName() const
Global constant definitions.
virtual float predict(InputArray samples, OutputArray results=noArray(), int flags=0) const=0
virtual int getCVFolds() const=0
virtual bool empty() const
virtual void setTruncatePrunedTree(bool val)=0
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.
virtual void setMaxDepth(int val)=0
map< int, Ptr< DTrees > > obj_
Object container.
virtual const std::vector< int > & getRoots() const=0
virtual void setPriors(const cv::Mat &val)=0
void create(int arows, int acols, int atype, Target target=ARRAY_BUFFER, bool autoRelease=false)
Common definitions for the ml module.
virtual void setCVFolds(int val)=0
virtual int getVarCount() const=0
virtual void setMaxCategories(int val)=0