17 map<int,Ptr<DownhillSolver> > obj_;
20 class MatlabFunction :
public cv::MinProblemSolver::Function
27 MatlabFunction(
int num_dims,
const string &func)
28 : dims(num_dims), fun_name(func)
47 double calc(
const double *x)
const 50 mxArray *lhs, *rhs[2];
52 rhs[1] =
MxArray(vector<double>(x, x + dims));
57 if (mexCallMATLAB(1, &lhs, 2, rhs,
"feval") == 0) {
59 CV_Assert(res.isDouble() && !res.isComplex() && res.numel() == 1);
60 val = res.at<
double>(0);
69 mxDestroyArray(rhs[0]);
70 mxDestroyArray(rhs[1]);
91 s.set(
"fun", fun_name);
101 static Ptr<MatlabFunction> create(
const MxArray &s)
104 mexErrMsgIdAndTxt(
"mexopencv:error",
"Invalid objective function");
105 return makePtr<MatlabFunction>(
106 s.
at(
"dims").toInt(),
107 s.
at(
"fun").toString());
123 void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs,
const mxArray *prhs[])
129 vector<MxArray> rhs(prhs, prhs+nrhs);
130 int id = rhs[0].toInt();
131 string method(rhs[1].toString());
134 if (method ==
"new") {
135 nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
136 Ptr<MinProblemSolver::Function> f;
137 Mat initStep(Mat_<double>(1, 1, 0.0));
138 TermCriteria termcrit(TermCriteria::MAX_ITER+TermCriteria::EPS, 5000, 1e-6);
139 for (
int i=2; i<nrhs; i+=2) {
140 string key(rhs[i].toString());
142 f = MatlabFunction::create(rhs[i+1]);
143 else if (key==
"InitStep")
144 initStep = rhs[i+1].toMat(CV_64F);
145 else if (key==
"TermCriteria")
146 termcrit = rhs[i+1].toTermCriteria();
148 mexErrMsgIdAndTxt(
"mexopencv:error",
149 "Unrecognized option %s", key.c_str());
151 obj_[++last_id] = DownhillSolver::create(f, initStep, termcrit);
157 Ptr<DownhillSolver> obj = obj_[id];
158 if (method ==
"delete") {
162 else if (method ==
"clear") {
166 else if (method ==
"load") {
170 else if (method ==
"save") {
174 else if (method ==
"empty") {
176 plhs[0] =
MxArray(obj->empty());
178 else if (method ==
"getDefaultName") {
180 plhs[0] =
MxArray(obj->getDefaultName());
182 else if (method ==
"minimize") {
184 Mat x(rhs[2].toMat(CV_64F));
185 double fx = obj->minimize(x);
190 else if (method ==
"get") {
192 string prop(rhs[2].toString());
193 if (prop ==
"Function") {
194 Ptr<MinProblemSolver::Function> f(obj->getFunction());
196 (f.dynamicCast<MatlabFunction>())->toStruct();
198 else if (prop ==
"InitStep") {
200 obj->getInitStep(initStep);
203 else if (prop ==
"TermCriteria")
204 plhs[0] =
MxArray(obj->getTermCriteria());
206 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized property %s", prop.c_str());
208 else if (method ==
"set") {
210 string prop(rhs[2].toString());
211 if (prop ==
"Function")
212 obj->setFunction(MatlabFunction::create(rhs[3]));
213 else if (prop ==
"InitStep")
214 obj->setInitStep(rhs[3].toMat(CV_64F));
215 else if (prop ==
"TermCriteria")
216 obj->setTermCriteria(rhs[3].toTermCriteria());
218 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized property %s", prop.c_str());
221 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized operation");
bool isStruct() const
Determine whether input is structure array.
MxArray toStruct(const std::vector< cv::ml::DTrees::Node > &nodes)
Convert tree nodes to struct array.
mxArray object wrapper for data conversion and manipulation.
void nargchk(bool cond)
Alias for input/ouput arguments number check.
static MxArray Struct(const char **fields=NULL, int nfields=0, mwSize m=1, mwSize n=1)
Create a new struct array.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Global constant definitions.
mwSize numel() const
Number of elements in an array.
T at(mwIndex index) const
Template for numeric array element accessor.