24 (
"BruteForce", KNearest::BRUTE_FORCE)
25 (
"KDTree", KNearest::KDTREE);
29 (KNearest::BRUTE_FORCE,
"BruteForce")
30 (KNearest::KDTREE,
"KDTree");
47 int id = rhs[0].toInt();
48 string method(rhs[1].toString());
51 if (method ==
"new") {
63 if (method ==
"delete") {
68 else if (method ==
"clear") {
72 else if (method ==
"load") {
73 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
75 bool loadFromString =
false;
76 for (
int i=3; i<nrhs; i+=2) {
77 string key(rhs[i].toString());
79 objname = rhs[i+1].toString();
80 else if (key ==
"FromString")
81 loadFromString = rhs[i+1].toBool();
84 "Unrecognized option %s", key.
c_str());
86 obj_[id] = (loadFromString ?
87 Algorithm::loadFromString<KNearest>(rhs[2].toString(), objname) :
88 Algorithm::load<KNearest>(rhs[2].toString(), objname));
90 else if (method ==
"save") {
92 string fname(rhs[2].toString());
95 FileStorage fs(fname, FileStorage::WRITE + FileStorage::MEMORY);
107 else if (method ==
"empty") {
111 else if (method ==
"getDefaultName") {
115 else if (method ==
"getVarCount") {
119 else if (method ==
"isClassifier") {
123 else if (method ==
"isTrained") {
127 else if (method ==
"train") {
128 nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=1);
131 for (
int i=4; i<nrhs; i+=2) {
132 string key(rhs[i].toString());
134 dataOptions = rhs[i+1].toVector<
MxArray>();
135 else if (key ==
"Flags")
136 flags = rhs[i+1].toInt();
137 else if (key ==
"UpdateModel")
138 UPDATE_FLAG(flags, rhs[i+1].toBool(), StatModel::UPDATE_MODEL);
141 "Unrecognized option %s", key.
c_str());
146 dataOptions.
begin(), dataOptions.
end());
151 dataOptions.
begin(), dataOptions.
end());
152 bool b = obj->
train(data, flags);
155 else if (method ==
"calcError") {
156 nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=2);
159 for (
int i=4; i<nrhs; i+=2) {
160 string key(rhs[i].toString());
162 dataOptions = rhs[i+1].toVector<
MxArray>();
163 else if (key ==
"TestError")
164 test = rhs[i+1].toBool();
167 "Unrecognized option %s", key.
c_str());
172 dataOptions.
begin(), dataOptions.
end());
177 dataOptions.
begin(), dataOptions.
end());
184 else if (method ==
"predict") {
185 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=2);
187 for (
int i=3; i<nrhs; i+=2) {
188 string key(rhs[i].toString());
190 flags = rhs[i+1].toInt();
193 "Unrecognized option %s", key.
c_str());
197 float f = obj->
predict(samples, results, flags);
202 else if (method ==
"findNearest") {
205 int k = rhs[3].toInt();
206 Mat results, neighborResponses, dist;
207 float f = obj->
findNearest(samples, k, results, neighborResponses, dist);
210 plhs[1] =
MxArray(neighborResponses);
216 else if (method ==
"get") {
218 string prop(rhs[2].toString());
219 if (prop ==
"AlgorithmType")
221 else if (prop ==
"DefaultK")
223 else if (prop ==
"Emax")
225 else if (prop ==
"IsClassifier")
229 "Unrecognized property %s", prop.
c_str());
231 else if (method ==
"set") {
233 string prop(rhs[2].toString());
234 if (prop ==
"AlgorithmType")
236 else if (prop ==
"DefaultK")
238 else if (prop ==
"Emax")
240 else if (prop ==
"IsClassifier")
244 "Unrecognized property %s", prop.
c_str());
248 "Unrecognized operation %s", method.
c_str());
virtual bool isTrained() const=0
map< int, Ptr< KNearest > > obj_
Object container.
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
const ConstMap< std::string, int > KNNAlgType
Option values for KNearest algorithm type.
virtual int getEmax() const=0
virtual void setAlgorithmType(int val)=0
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
int last_id
Last object id to allocate.
virtual void setDefaultK(int val)=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()
virtual int getDefaultK() const=0
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...
const ConstMap< int, std::string > InvKNNAlgType
Option values for inverse KNearest algorithm type.
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 void setIsClassifier(bool val)=0
virtual String getDefaultName() const
Global constant definitions.
virtual int getAlgorithmType() const=0
virtual void setEmax(int val)=0
virtual float predict(InputArray samples, OutputArray results=noArray(), int flags=0) const=0
virtual bool empty() const
virtual float findNearest(InputArray samples, int k, OutputArray results, OutputArray neighborResponses=noArray(), OutputArray dist=noArray()) const=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.
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.
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 bool getIsClassifier() const=0