17 map<int,Ptr<ConjGradSolver> > obj_;
20 class MatlabFunction :
public cv::MinProblemSolver::Function
30 MatlabFunction(
int num_dims,
const string &func,
const string &grad_func =
"",
double h = 1e-3)
31 : dims(num_dims), fun_name(func), grad_fun_name(grad_func), gradeps(h)
50 double calc(
const double *x)
const 53 mxArray *lhs, *rhs[2];
55 rhs[1] =
MxArray(vector<double>(x, x + dims));
60 if (mexCallMATLAB(1, &lhs, 2, rhs,
"feval") == 0) {
62 CV_Assert(res.isDouble() && !res.isComplex() && res.numel() == 1);
63 val = res.at<
double>(0);
72 mxDestroyArray(rhs[0]);
73 mxDestroyArray(rhs[1]);
95 void getGradient(
const double* x,
double* grad)
99 if (grad_fun_name.empty()) {
100 cv::MinProblemSolver::Function::getGradient(x, grad);
105 mxArray *lhs, *rhs[2];
106 rhs[0] =
MxArray(grad_fun_name);
107 rhs[1] =
MxArray(vector<double>(x, x + dims));
111 if (mexCallMATLAB(1, &lhs, 2, rhs,
"feval") == 0) {
113 CV_Assert(res.isDouble() && !res.isComplex() && res.ndims() == 2);
114 vector<double> v(res.toVector<
double>());
115 CV_Assert(v.size() == dims);
116 std::copy(v.begin(), v.end(), grad);
120 std::fill(grad, grad + dims, 0.0);
125 mxDestroyArray(rhs[0]);
126 mxDestroyArray(rhs[1]);
132 double getGradientEps()
const 152 s.set(
"fun", fun_name);
153 s.set(
"gradfun", grad_fun_name);
154 s.set(
"gradeps", gradeps);
166 static Ptr<MatlabFunction> create(
const MxArray &s)
169 mexErrMsgIdAndTxt(
"mexopencv:error",
"Invalid objective function");
170 return makePtr<MatlabFunction>(
171 s.
at(
"dims").toInt(),
172 s.
at(
"fun").toString(),
173 s.
isField(
"gradfun") ? s.
at(
"gradfun").toString() :
"",
174 s.
isField(
"gradeps") ? s.
at(
"gradeps").toDouble() : 1e-3);
180 string grad_fun_name;
192 void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs,
const mxArray *prhs[])
198 vector<MxArray> rhs(prhs, prhs+nrhs);
199 int id = rhs[0].toInt();
200 string method(rhs[1].toString());
203 if (method ==
"new") {
204 nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
205 Ptr<MinProblemSolver::Function> f;
206 TermCriteria termcrit(TermCriteria::MAX_ITER+TermCriteria::EPS, 5000, 1e-6);
207 for (
int i=2; i<nrhs; i+=2) {
208 string key(rhs[i].toString());
210 f = MatlabFunction::create(rhs[i+1]);
211 else if (key==
"TermCriteria")
212 termcrit = rhs[i+1].toTermCriteria();
214 mexErrMsgIdAndTxt(
"mexopencv:error",
215 "Unrecognized option %s", key.c_str());
217 obj_[++last_id] = ConjGradSolver::create(f, termcrit);
223 Ptr<ConjGradSolver> obj = obj_[id];
224 if (method ==
"delete") {
228 else if (method ==
"clear") {
232 else if (method ==
"load") {
236 else if (method ==
"save") {
240 else if (method ==
"empty") {
242 plhs[0] =
MxArray(obj->empty());
244 else if (method ==
"getDefaultName") {
246 plhs[0] =
MxArray(obj->getDefaultName());
248 else if (method ==
"minimize") {
250 Mat x(rhs[2].toMat(CV_64F));
251 double fx = obj->minimize(x);
256 else if (method ==
"get") {
258 string prop(rhs[2].toString());
259 if (prop ==
"Function") {
260 Ptr<MinProblemSolver::Function> f(obj->getFunction());
262 (f.dynamicCast<MatlabFunction>())->toStruct();
264 else if (prop ==
"TermCriteria")
265 plhs[0] =
MxArray(obj->getTermCriteria());
267 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized property %s", prop.c_str());
269 else if (method ==
"set") {
271 string prop(rhs[2].toString());
272 if (prop ==
"Function")
273 obj->setFunction(MatlabFunction::create(rhs[3]));
274 else if (prop ==
"TermCriteria")
275 obj->setTermCriteria(rhs[3].toTermCriteria());
277 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized property %s", prop.c_str());
280 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.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
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.
bool isField(const std::string &fieldName) const
Determine whether a struct array has a specified field.
Global constant definitions.
mwSize numel() const
Number of elements in an array.
T at(mwIndex index) const
Template for numeric array element accessor.