Ask Your Question
2

Downhill Solver -- error due to pure virtual functions

asked 2017-03-30 03:29:52 -0600

guidovitale gravatar image

updated 2017-03-30 03:44:44 -0600

I'm familiarizing with Optimization Algorithm in OpenCV. I've found an interesting example to start with:

http://answers.opencv.org/question/33...

so, I developed some code based on that:

#include <cstdlib>
#include <cmath>
#include <algorithm>

using namespace cv;
using namespace std;


class DistanceToLines :public MinProblemSolver::Function
{
public:
    double calc(const double* x)const
    {

        return x[0] * x[0] + x[1] * x[1];
    }
};


int main()
{

    Ptr<DownhillSolver> DHS = DownhillSolver::create();


//    Ptr<MinProblemSolver::Function> ptr_F = makePtr<DistanceToLines>();  // NOT WORKING!!!
    Ptr<MinProblemSolver::Function> ptr_F(new DistanceToLines());                // NOT WORKING!!!

    return 0;

}

It gives me the following error, when building:

D:\ALGORITMI\minDownhill\main.cpp:50: error: invalid new-expression of abstract class type 'DistanceToLines'
     Ptr<MinProblemSolver::Function> ptr_F(new DistanceToLines());
D:\ALGORITMI\minDownhill\main.cpp:33: note:   because the following virtual functions are pure within 'DistanceToLines':
 class DistanceToLines :public MinProblemSolver::Function

any hint? I saw that there are not many examples on the web about these methods, so I cannot figure out what is happening.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
3

answered 2017-04-04 03:02:10 -0600

guidovitale gravatar image

thanks berak. I will post my small code as an hint for those, like me, are novice to opencv.

// Qt stuff
    #include "mainwindow.h"
    #include <QApplication>

// OpenCV libraries 
    #include <opencv2/opencv.hpp>
    #include "opencv2/core/utility.hpp"
    #include "opencv2/highgui.hpp"

    #include "opencv2/core/core_c.h"
    #include "opencv2/core/cvdef.h"
    #include "opencv2/core/optim.hpp"

    #include <iostream>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>

    using namespace cv;
    using namespace std;




    class DistanceToLines :public MinProblemSolver::Function
    {
    public:
// quadratic functional to minimize (f(x1,x2) = x1^2 + x2^2)
        double calc(const double* x)const
        {
            return x[0] * x[0] + x[1] * x[1];
        }
// nb of degrees of freedom (i.e. how many unknowns)
        int getDims() const
        {
            return 2;
        }
    };



    int main(int argc, char *argv[])
    {

        QApplication a(argc, argv);


        // create a nelder-mead simplex solver
        Ptr<DownhillSolver> DHS = DownhillSolver::create();

        // function to minimize
        Ptr<MinProblemSolver::Function> ptr_F(new DistanceToLines());

        // init guess
        Mat P = (Mat_<double>(1, 2) << 1.0, 1.0);
        DHS->setFunction(ptr_F);

        // init step
        Mat step = (Mat_<double>(2, 1) << -0.5, 0.5);
        DHS->setInitStep(step);

        // solve the preoblem
        double res = DHS->minimize(P);

        // output reidual
        cout << "res " << res << endl;


        return 0;

    }

I'm using opencv with qt on a win10 machine, compiling with mingw.

edit flag offensive delete link more
1

answered 2017-03-30 04:02:45 -0600

berak gravatar image

updated 2017-03-30 04:16:50 -0600

if you look at the MinProblemSolver::Function interface, you'll see, that there are actually two pure virtual functions to implement (yea, doc fail ., also it wasn't in the 3.0 version, your example is refering to),

so, you'll need an implementation for MinProblemSolver::Function::getDims() maybe like this:

class DistanceToLines :public MinProblemSolver::Function
{
public:
    double calc(const double* x)const
    {
        return x[0] * x[0] + x[1] * x[1];
    }
    int getDims() const
    {
         return 2;
    }
};
edit flag offensive delete link more

Comments

1

Thank you, Berak. the code is now working. couldnt post it as a comment since it's too long. btw, I'm using opencv 3.0.0 with qt and mingw as a compiler on a win10 machine

guidovitale gravatar imageguidovitale ( 2017-03-30 04:33:23 -0600 )edit

added some karma, so, if you want to (and think it's useful), you could write your own answer now.

berak gravatar imageberak ( 2017-03-30 04:39:30 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-03-30 03:29:52 -0600

Seen: 924 times

Last updated: Apr 04 '17