opencv solver c ++函数优化

时间:2018-02-28 23:04:58

标签: c++ opencv optimization makefile

我是opencv优化的新手,我做了一个使用这样的共轭梯度求解器的简单例子:

#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

class Rosenbrock: public MinProblemSolver::Function {
    int getDims() const {
        return 2;
    };
    double calc(const double* x) const {
        return 100*(x[1] - x[0]*x[0])*(x[1]-x[0]*x[0]) + (1 - x[0])*(1-x[0]);
    }
};

int main(int argc, char** argv) {
    Ptr<ConjGradSolver> solver = makePtr<ConjGradSolver>();
    solver->setFunction(Rosenbrock);
    Mat x = (Mat_<double>(2, 1) << 0.0, 0.0);
    double fval = solver->minimize(x);
    std::cout << fval << std::endl;
    return 0;
}

使用此makefile

CC = g++
CFLAGS = -g -Wall
SRCS = HelloWorld.cpp
PROG = HelloWorld

OPENCV = `pkg-config opencv --cflags --libs`
LIBS = $(OPENCV)

$(PROG):$(SRCS)
    $(CC) $(CFLAGS) -o $(PROG) $(SRCS) $(LIBS)

但是当我尝试编译时,会出现错误:

g++ -g -Wall -o HelloWorld HelloWorld.cpp `pkg-config opencv --cflags --libs`
HelloWorld.cpp: In function ‘int main(int, char**)’:
HelloWorld.cpp:17:35: error: expected primary-expression before ‘)’ token
     solver->setFunction(Rosenbrock);
                                   ^
HelloWorld.cpp:19:12: warning: unused variable ‘fval’ [-Wunused-variable]
     double fval = solver->minimize(x);
            ^
In file included from /usr/local/include/opencv2/core/cvstd.hpp:1037:0,
                 from /usr/local/include/opencv2/core/base.hpp:58,
                 from /usr/local/include/opencv2/core.hpp:54,
                 from /usr/local/include/opencv2/opencv.hpp:52,
                 from HelloWorld.cpp:1:
/usr/local/include/opencv2/core/ptr.inl.hpp: In instantiation of     ‘cv::Ptr<T> cv::makePtr() [with T = cv::ConjGradSolver]’:
HelloWorld.cpp:16:58:   required from here
/usr/local/include/opencv2/core/ptr.inl.hpp:301:26: error: invalid new-    expression of abstract class type ‘cv::ConjGradSolver’
     return Ptr<T>(new T());
                          ^
In file included from /usr/local/include/opencv2/core.hpp:3225:0,
                 from /usr/local/include/opencv2/opencv.hpp:52,
                 from HelloWorld.cpp:1:

我会感激任何建议, 真诚, 保罗。

1 个答案:

答案 0 :(得分:0)

现在它的工作。我从这个链接中的代码中获得灵感: https://github.com/opencv/opencv/blob/master/modules/core/test/test_conjugate_gradient.cpp#L107 我换了     Ptr<ConjGradSolver> solver = makePtr<ConjGradSolver>(); solver->setFunction(Rosenbrock);

Ptr<ConjGradSolver::Function> pokus(new Rosenbrock()); Ptr<ConjGradSolver> solver = ConjGradSolver::create(); solver->setFunction(pokus);